How to tell if mouse moved during .click()?

前端 未结 4 1124
半阙折子戏
半阙折子戏 2021-01-23 22:53

According to the jQuery documentation for .click(), the event is triggered only after this series of events:

  • The mouse button is depressed while the pointer is ins
4条回答
  •  说谎
    说谎 (楼主)
    2021-01-23 23:54

    Keep track of the mouse position between mousedown and mouseup to see it the mouse pointer moved. You should probably add a little margin of error, like a few pixels every way, but below is a simplified example :

    var left  = 0,
        top   = 0;    
    
    $(element).on({
        mousedown: function(e) {
            left  = e.pageX;
            top   = e.pageY;
        },
        mouseup: function(e) {
            if (left != e.pageX || top != e.pageY) {
                alert(' OMG, it moved ! ');
            }
        }
    });
    

提交回复
热议问题