jQuery: Detecting pressed mouse button during mousemove event

前端 未结 7 1517
心在旅途
心在旅途 2020-11-30 05:22

I tried to detect which mouse button -if any- is the user pressing during a mousemove event under jQuery, but I\'m getting ambiguous results:

no button press         


        
7条回答
  •  攒了一身酷
    2020-11-30 05:51

    Most browsers (except Safari) *) now support the MouseEvent.buttons property (note: plural "buttons"), which is 1 when the left mouse button is pressed:

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

    https://jsfiddle.net/pdz2nzon/2

    *) The world is moving forward:
    https://webkit.org/blog/8016/release-notes-for-safari-technology-preview-43/
    https://trac.webkit.org/changeset/223264/webkit/
    https://bugs.webkit.org/show_bug.cgi?id=178214

提交回复
热议问题