2011-01-29 8 views
1

私のサイトには、CTRL + +(zoom-in)のボタンがあります。だから私は2つのkeypressesまたは何かを作成する必要があります。jQueryキーボードショートカットby clickevent

<html> 
<head> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script> 
    <title>Stackoverflow</title> 
</head> 
<body> 
<script type="text/javascript"> 
    function simulateKeyPress(character) { 
     jQuery.event.trigger({ type: 'keypress', which: character.charCodeAt(0) }); 
    } 

    function pressCtrlPlus() { 
     simulateKeyPress("="); 
    } 

    $(function() { 
     $('body').keypress(function (e) { 
      alert(e.which); 
     }); 
    }); 
</script> 

<input type="button" value="Ctrl +" ONCLICK="pressCtrlPlus();"> 

</body> 
</html> 

誰かがボタンをクリックすることでCTRL ++イベントを作成する方法を知ってい:私は1つのキーの押下を発射folowingがありますか?

+0

の可能性のある重複:http://stackoverflow.com/questions/2445613/how-can-i-check-if-key-is-pressed-during-click-event-with-jquery – mVChr

+0

可能重複[ブラウザのズームインとズームアウト機能の起動](http://stackoverflow.com/questions/4539955)と[その他](http://stackoverflow.com/search?q=javascript+zoom+in)を参照してください。投稿する前に検索してください。 –

+0

[ブラウザのズームインとズームアウトの機能をトリガする]の複製が可能です(http://stackoverflow.com/questions/4539955/triggering-browser-zoom-in-and-zoom-out-funtions) –

答えて

0

は、このトピックSO: Calling keyevent from mouse によると、私は次のテスト作成:

<html> 
<head> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script> 
    <title>Stackoverflow</title> 
</head> 
<body> 
<script type="text/javascript"> 
    function zoomin(zlevel) { 
     var _body = parent.parent.document.body; 

     if (jQuery.browser.msie) { 
      _body.style.zoom = zlevel;    
     } 
     else 
     { 
      if (typeof _body.style.MozTransform == "string")  
       _body.style.MozTransform = "scale(" + (zlevel) + ")"; 
      else if (typeof _body.style.zoom == "string") 
       _body.style.zoom = (zlevel*100) + "%";    
     } 
    } 
</script> 

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> 

<input type="button" value="Ctrl0" ONCLICK="zoomin(1.0);"> 
<input type="button" value="Ctrl+" ONCLICK="zoomin(1.15);"> 
<input type="button" value="Ctrl++" ONCLICK="zoomin(1.3);"> 
</body> 
</html> 

をしかし、それはまだ変な動作します。私はまだキーボードショートカットを使ってズームするような動作はしていません。CTRL + +これはできない?

0

ズーム機能を維持するには、現在のズームレベルを覚えておく必要があります。

<html> 
<head> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script> 
    <title>Stackoverflow</title> 
</head> 
<body> 
<script type="text/javascript"> 
    function zoomin(zlevel) { 
     zlevel = $('#zlevel').val() * zlevel; 
     $('#zlevel').val(zlevel); 

     var _body = parent.parent.document.body; 

     if (jQuery.browser.msie) { 
      _body.style.zoom = zlevel;    
     } 
     else 
     { 
      if (typeof _body.style.MozTransform == "string")  
       _body.style.MozTransform = "scale(" + (zlevel) + ")"; 
      else if (typeof _body.style.zoom == "string") 
       _body.style.zoom = (zlevel*100) + "%";    
     } 
    } 
</script> 

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> 

<input type="button" value="Ctrl0" ONCLICK="zoomin(0.9);"> 
<input type="button" value="Ctrl+" ONCLICK="zoomin(1.15);"> 
<input type="button" value="Ctrl++" ONCLICK="zoomin(1.3);"> 
<input type="hidden" value="1" id="zlevel"> 
</body> 
</html> 
関連する問題