Javascript 如果发生touchmove,请停止触发任何touchend事件
如果我有一个div监听Javascript 如果发生touchmove,请停止触发任何touchend事件,javascript,jquery,mobile,Javascript,Jquery,Mobile,如果我有一个div监听touchend事件,如果用户触摸该div但随后滚动或触摸移动,如何停止触发touchend事件 目前我正在做: $('body').on('touchmove', function(){dragging = true}).on('touchend', function(){dragging = false}) 然后我拥有的每个touchend事件都会使用: if(dragging == false){ //do stuff} 但是我不想把这个if语句添加到我所有的to
touchend
事件,如果用户触摸该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
事件。一种方法是简单地在touchmove
处理程序中取消注册任何touchend
处理程序:
$('body').on('touchmove', function(event) {
$('body').off('touchend');
...
});
尽管您可能希望允许少量移动,但触摸输入不太可能保持完全静止
当然,每次获得
touchstart
事件时,您也必须重新注册touchend
事件处理程序,尽管在任何情况下(没有双关语)推迟注册touchmove
和touchend
处理程序,直到收到touchstart
事件,这样UI代码就不会处理不需要的事件,这是一种很好的做法,与通常延迟注册mousemove
处理程序的方式相同,直到收到mousedown
事件。我认为在touchmove
中执行的任何操作都不会阻止touchend
表单触发。基于标志的解决方案是imho唯一可能的解决方案。