如何判断在.click()期间鼠标是否移动? [英] How to tell if mouse moved during .click()?

查看:404
本文介绍了如何判断在.click()期间鼠标是否移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 .click()的jQuery文档,仅在此系列之后才触发事件事件数:

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

  • 当指针位于元素内部时,按下鼠标按钮.
  • 当指针位于元素内部时,释放鼠标按钮.

我面临的问题是我在项目网格上使用单击拖动"功能,我想为每个项目注册click事件.这意味着每当我单击拖动时,在项目上按下鼠标,然后在发生拖动后在同一项目上释放鼠标.这会触发点击事件.

The problem I'm facing is that I'm using "click to drag" functionality on a grid of items, each of which I'd like to register the click event for. This means each time I click to drag, the mouse is pressed on an item, then released on that same item after the drag has occurred. This fires the click event.

有什么方法可以仅在从按下鼠标到释放鼠标之间没有移动鼠标的情况下触发该事件?尽管这似乎微不足道,但作为jQuery初学者,我有些沮丧.

Is there any way to only fire the event if the mouse has not moved between when the mouse has been depressed to when the mouse has been released? Though this seems trivial, as a jQuery beginner I'm sort of stumped.

推荐答案

跟踪鼠标在mousedown和mouseup之间的位置,以查看鼠标指针是否移动.您可能应该增加一点误差,如每种方式都增加几个像素,但是下面是一个简化的示例:

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 ! ');
        }
    }
});

这篇关于如何判断在.click()期间鼠标是否移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆