天天看點

Android 4.0.x 浏覽器不觸發 ontouchend 事件的解決

先強烈吐槽一下,不知道 Android Wekbit 開發人員怎麼想的,如此糟糕 bug 從 4.0 到當下的 4.4 仍然如故,好像沒有像修複的意思?難道這是改進不是 bug?

問題是這樣的,使用 touch* 事件時,在 Android 4.0 上面的浏覽器手指在 a 元素(或者其他任何元素)上做滑動操作,然後手指離開,結果不會觸發 touchend 事件。同樣的操作在 Android 2.x / ios 卻會會正常觸發 touchend 事件。到 touchmove 事件之後就終止掉,簡單說,就是 touchend 事件丢失。天呐~無疑這是一個非常嚴重的bug——因為這是一個極其基礎的事件,不可或缺!~My God~

已經有許多人把該問題 report 了,詳見:

<a target="_blank" href="https://code.google.com/p/android/issues/detail?id=19827">WebView touchevents are not fired propperly if e.preventDefault() is not used on touchstart and touchmove</a>

<a target="_blank" href="https://code.google.com/p/android/issues/detail?id=4549">In webkit/webview touchmove/touchstart/touchend events get queued and don't fire until touch ends</a>

怎麼破?在 touchmove 事件中 e.preventDetault() 居然就可以。

但是簡單調用 e.preventDetault() 會導緻另外一個問題,就是阻止了螢幕上下滾動的 scorll 事件。

這又怎麼破?

加入到 touchmove 事件中。

另外一個方法個人原創(但是也是受了幾個前輩的啟發),見下面代碼:

在這裡抛磚引玉了,如有更好的方法請告知。

繼續閱讀