jQuery:在mousemove事件期间检测按下的鼠标按钮

Seb*_*oli 69 jquery

我尝试检测哪个鼠标按钮 - 如果用户在jQuery下的mousemove事件期间按下了,但是我得到了模棱两可的结果:

no button pressed:      e.which = 1   e.button = 0
left button pressed:    e.which = 1   e.button = 0
middle button pressed:  e.which = 2   e.button = 1
right button pressed:   e.which = 3   e.button = 2
Run Code Online (Sandbox Code Playgroud)

码:

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>

<input id="whichkey" value="type something">
<div id="log"></div>
<script>$('#whichkey').bind('mousemove',function(e){ 
  $('#log').html(e.which + ' : ' +  e.button );
});  </script>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

如何判断按下鼠标左键和没有按钮之间的区别?

Dar*_*JDG 60

您可以编写一些代码来跟踪鼠标左键的状态,使用一个小函数可以预先处理事件中的事件变量mousemove.

要跟踪LMB的状态,请将事件绑定到文档级别mousedown,mouseup然后检查e.which以设置或清除该标志.

预处理由tweakMouseMoveEvent()我的代码中的函数完成.要支持IE版本<9,您必须检查窗口外是否释放鼠标按钮,如果是,则清除标记.然后,您可以更改传递的事件变量.如果e.which原来是1(没有按钮或LMB)并且没有按下左按钮的当前状态,只需设置e.which0,并在mousemove事件的其余部分使用它来检查没有按下按钮.

mousemove我的例子中的处理程序只调用tweak函数传递当前事件变量,然后输出值e.which.

$(function() {
    var leftButtonDown = false;
    $(document).mousedown(function(e){
        // Left mouse button was pressed, set flag
        if(e.which === 1) leftButtonDown = true;
    });
    $(document).mouseup(function(e){
        // Left mouse button was released, clear flag
        if(e.which === 1) leftButtonDown = false;
    });

    function tweakMouseMoveEvent(e){
        // Check from jQuery UI for IE versions < 9
        if ($.browser.msie && !e.button && !(document.documentMode >= 9)) {
            leftButtonDown = false;
        }

        // If left button is not set, set which to 0
        // This indicates no buttons pressed
        if(e.which === 1 && !leftButtonDown) e.which = 0;
    }

    $(document).mousemove(function(e) {
        // Call the tweak function to check for LMB and set correct e.which
        tweakMouseMoveEvent(e);

        $('body').text('which: ' + e.which);
    });
});
Run Code Online (Sandbox Code Playgroud)

在这里尝试现场演示:http://jsfiddle.net/G5Xr2/

  • 如果在浏览器上没有释放按钮,则不会触发mouseup. (5认同)
  • 请注意,最好将`mousedown`和`mouseup`监听器附加到`document`,而不一定是将`mousemove`处理程序附加到的相同元素.这是为了处理鼠标被拖离元素边缘的情况.但这假设事件总是传播,正如@Ariel所说,它不会处理在浏览器外发布的鼠标!我继续在'mousemove`事件中寻找一个准确的鼠标按钮状态,就像键盘修改器状态一样,无论鼠标向下移动.(我真的只是打字吗?) (4认同)
  • **警告:**`$ .browser`已被弃用,将与jQuery 1.9+打破.您需要使用[jquery migrate plugin](https://github.com/jquery/jquery-migrate/)让它再次运行.请参阅:http://stackoverflow.com/q/9638247/165673 (3认同)

Sph*_*xxx 19

大多数浏览器(Safari除外)*)现在支持MouseEvent.buttons属性(注意:复数"按钮s "),按下鼠标左键时为1:

$('#whichkey').bind('mousemove', function(e) { 
    $('#log').html('Pressed: ' + e.buttons);
});
Run Code Online (Sandbox Code Playgroud)

https://jsfiddle.net/pdz2nzon/2

*)世界正在向前发展:
https://webkit.org/blog/8016/release-notes-for-safari-technology-preview-43/
https://trac.webkit.org/changeset/223264/webkit/

  • 这个答案应该是公认的答案!来自懒惰供应商的浏览器可以从以下网址获得帮助:https://github.com/mikolalysenko/mouse-event (4认同)

Mot*_*tie 6

jQuery对which值进行规范化,使其适用于所有浏览器.我敢打赌,如果你运行你的脚本,你会发现不同的e.button值.

  • 如果它发生在浏览器窗口之外,你将不会得到mousedown事件,因此a)可以通过按下鼠标按钮,b)在浏览器窗口外移动鼠标,c)释放鼠标按钮,d)移动鼠标,将全局变量置于不一致状态进入窗口 - 所以这不是解决方案 (6认同)

mar*_*ero 6

出于某种原因,当绑定到mousemoveevent时,event.which属性设置为1if left button或none.

我改为mousedown活动,它运作正常:

  • 左:1
  • 中:2
  • 右:3

这是一个有效的例子:http://jsfiddle.net/marcosfromero/RrXbX/

jQuery代码是:

$('p').mousedown(function(event) {
    console.log(event.which);
    $('#button').text(event.which);
});
Run Code Online (Sandbox Code Playgroud)