2014-01-13 150 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事件相同。