安卓浏览器无法正确处理touchmove事件

3
当我尝试检查此jsbin演示中的touchmove事件时,在Chrome和Opera for Android上它只触发一次,紧接着立即触发touchcancel事件,而不是继续触发touchmove事件?基于W3C规范和Firefox for Android以及Android默认浏览器中touchmove事件的行为,我认为触摸事件应该在触摸仍在页面上时持续触发touchmove事件。然而,在尝试在此jsbin中进行测试后,我得到了以下日志消息:
touchstart event; starting on (140,197) on the screen, or (381,536) on the page.
touchend event; starting on (undefined,undefined) on the screen, or (undefined,undefined) on the page.
touchstart event; starting on (181,137) on the screen, or (492,372) on the page.
touchmove event; starting on (182,153) on the screen, or (495,416) on the page.
touchcancel event; starting on (undefined,undefined) on the screen, or (undefined,undefined) on the page.

这是我第一次点击屏幕时发生的情况(通过touchstarttouchend显示),然后拖动屏幕(touchstarttouchmovetouchcancel)。根据上面提到的规格,当有干扰时,例如浏览器界面(如果我理解正确),触摸取消事件应该运行。
由于我只是在身体上滑动手指,根本没有离开窗口,所以我真的很困惑,所以有人知道为什么会发生这种情况吗?
我在Android的Chrome 32Opera 19中得到了这个意外的结果。
1个回答

7
原来这里的问题很简单,事件处理程序中没有使用event.preventDefault(),因此原始操作仍然执行,这显然会中断触摸事件。为了解决这个问题,在当前事件处理程序函数中添加e.preventDefault()以取消当前事件,并使其在Chrome和Opera中按预期工作即可。

工作演示.


1
我在使用onpointerdown、onpointermove和onpointerup时遇到了这个问题。最终通过添加ontouchstart = (e) => e.preventDefault()解决了它。感谢指点! - Joshua

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接