2014-01-13 149 views
0

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

目前即时通讯做:

$('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事件滚动或touchmove后射击。

+0

我不认为你在'touchmove'中做的任何事情都可以让你防止'touchend'形式发射。基于标志的解决方案是唯一可行的解​​决方案。 – techfoobar

回答

0

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

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

虽然你可能希望允许移动少量 - 这是不太可能的触摸输入将保持正好仍然。

当然,你也会有重新每次 -register你touchend事件处理程序,你得到touchstart事件,但在任何情况下(没有双关语意)它是一种推迟注册您touchmovetouchend处理的好习惯直到您收到touchstart事件,以便您的UI代码不会处理不需要的事件,与您通常会延迟注册mousemove处理程序直到收到mousedown事件相同。