如果发生触摸移动,则停止触发任何触发事件

时间:2014-01-13 10:10:17

标签: javascript jquery mobile

如果我有一个侦听touchend事件的div,如果用户触摸div然后滚动或触摸移动,如何停止触发touchend事件?

目前正在做:

$('body').on('touchmove', function(){dragging = true}).on('touchend', function(){dragging = false})

然后我使用的每个touchend事件:

if(dragging == false){ //do stuff}

但我不想将此if语句添加到我拥有的每个touchend事件中(其中有很多)。

我也试过了:

$('body').on('touchmove', function(event){
    event.preventDefault()
    event.stopPropagation()
    event.stopImmediatePropagation()
    return false;
})

但这并没有阻止在滚动或触摸移动后触发的任何touchend事件。

1 个答案:

答案 0 :(得分:0)

一种方法是简单地取消注册touchend处理程序中的任何touchmove处理程序:

$('body').on('touchmove', function(event) {
    $('body').off('touchend');
    ...
});

虽然您可能希望允许少量移动 - 但触摸输入不太可能完全

当你每次收到touchend事件时,你当然也必须重新注册你的touchstart事件处理程序,尽管在任何情况下(没有双关语)它都是延迟注册touchmovetouchend处理程序,直到您收到touchstart事件,以便您的UI代码不会处理不需要的事件,就像您一样通常会推迟注册mousemove处理程序,直到收到mousedown事件。

相关问题