Weird behavior of long presses in Chrome and Firefox for Android

我怕爱的太早我们不能终老 提交于 2019-12-13 00:18:10

问题


So, I found some weird behavior of long presses in Chrome and Firefox for Android. I'm trying to avoid scrolling, context menus, vibration and selecting text. Also, I need the touch not to be canceled. I set a simple page like this:

var logger = document.getElementById("logger")

function touchHandler(e) {
  e.preventDefault()
  logger.innerText += e.type + "\n"
}
function contextMenuHandler(e) {
  e.preventDefault()
  logger.innerText += e.type + "\n"
}

window.addEventListener("touchstart", touchHandler)
window.addEventListener("touchmove", touchHandler)
window.addEventListener("touchend", touchHandler)
window.addEventListener("touchcancel", touchHandler)
window.addEventListener("contextmenu", contextMenuHandler)
<pre id="logger"></pre>

And I got these results, when pressing for a bit without moving the finger:

  1. When I default prevent both touch events and the contextmenu event:
    • Chrome logs:
      touchstart
      contextmenu
      touchend
      Note that Chrome vibrates at the same time contextmenu is fired.
    • Firefox logs:
      touchstart
      contextmenu
      touchcancel
      Firefox cancels the touch event as soon as contextmenu is fired, which is a problem for me.
  2. When I default prevent only touch events:
    • Chrome logs:
      touchstart
      contextmenu
      touchend
      Nice. It dosen't vibrate.
    • Firefox logs the same thing. This time, it didn't cancel the touch, but it selected some text.
    • When I default prevent only the contextmenu event:
    • Chrome logs:
      touchstart
      contextmenu
      touchend
      It vibrates and scrolls, however.
    • Firefox logs:
      touchstart
      contextmenu
      touchcancel
      Again, it's canceling the touch, but no text is selected.
  3. Finally, when no events are default prevented:
    • Chrome logs:
      touchstart
      contextmenu
      touchend
      And still vibrates and scrolls.
    • Firefox logs:
      touchstart
      contextmenu
      touchcancel
      Same thing :(

So, is there any way to prevent scrolling, context menus, vibration and selecting text, and to avoid Firefox canceling the touch?

来源:https://stackoverflow.com/questions/55560929/weird-behavior-of-long-presses-in-chrome-and-firefox-for-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!