jQuery: Detecting pressed mouse button during mousemove event

前端 未结 7 1516
心在旅途
心在旅途 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:59

    As of the date, the following works on Firefox 47, Chrome 54 and Safari 10.

    $('#whichkey').bind('mousemove',function(e){
        if (e.buttons & 1 || (e.buttons === undefined && e.which == 1)) {
            $('#log').html('left button pressed');
        } else if (e.buttons & 2 || (e.buttons === undefined && e.which == 3)) {
            $('#log').html('right button pressed');
        } else {
            $('#log').html('no button pressed');
        }
    });
    

提交回复
热议问题