您能检测到“拖曳"吗?在jQuery中? [英] Can you detect "dragging" in jQuery?

查看:70
本文介绍了您能检测到“拖曳"吗?在jQuery中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击链接时,我会出现一个th动.

I have a throbber that is to appear when a user clicks a link.

问题是可以单击并拖动相同的链接来重新排列.在这种情况下,我不需要出现跳动的人.仅当它实际上等待去某个地方时才需要出现.

The problem is is that that same link can be clicked and dragged to be rearranged. In this case, I wouldn't need the throbber to appear. It only needs to appear if its actually waiting to go somewhere.

我该如何使用jQuery创建一个事件监听器,该事件监听器仅在单击链接时显示跳动,而不是单击并拖动?

How can I, with jQuery, create an event listener that would only allow a throbber to appear if its a click through to a link, and not a click and drag?

推荐答案

在mousedown上,开始设置状态,如果触发了mousemove事件,则将其记录下来,最后在mouseup上,检查鼠标是否移动.如果它移动了,我们一直在拖.如果我们没有移动,请单击.

On mousedown, start set the state, if the mousemove event is fired record it, finally on mouseup, check if the mouse moved. If it moved, we've been dragging. If we've not moved, it's a click.

var isDragging = false;
$("a")
.mousedown(function() {
    isDragging = false;
})
.mousemove(function() {
    isDragging = true;
 })
.mouseup(function() {
    var wasDragging = isDragging;
    isDragging = false;
    if (!wasDragging) {
        $("#throbble").toggle();
    }
});

这是一个演示: http://jsfiddle.net/W7tvD/1399/

这篇关于您能检测到“拖曳"吗?在jQuery中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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