【问题标题】:Android browsers not handling touchmove events correctlyAndroid 浏览器无法正确处理 touchmove 事件
【发布时间】:2014-02-13 17:38:46
【问题描述】:

当我尝试检查 this jsbin demo 中的 touchmove 事件时,它只会在 Chrome 和 Opera for Android 中触发一次,然后立即触发 touchcancel 事件,而不是继续触发 touchmove 事件?

基于the W3C specs,以及 Firefox for Android 和 Android 的默认浏览器中 touchmove 事件的行为,在我看来,触摸事件的工作方式应该是 touchmove 事件触摸仍在页面上时不断触发。在trying to test in this 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)时发生的情况。按照上面提到的specs,touchcancel 事件应该在某些干扰时运行,例如浏览器界面(如果我理解正确的话)。

由于我只是在身体上滑动手指,根本没有离开窗户,我对此感到非常困惑,所以有人知道为什么会这样吗?

我在 Chrome 32Opera 19 for Android 中得到了这个意想不到的结果。

【问题讨论】:

    标签: javascript android touch touchmove


    【解决方案1】:

    事实证明这里的问题只是事件处理程序中没有event.preventDefault(),因此原始操作仍然执行,这显然中断了触摸事件。要解决此问题,只需在当前事件处理函数中添加 e.preventDefault() 即可取消当前事件,并使其在 Chrome 和 Opera 中也能正常工作。

    Working demo.

    【讨论】:

    • 我刚刚使用 onpointerdown、onpointermove 和 onpointerup 解决了这个问题。最后通过添加ontouchstart = (e) => e.preventDefault() 修复它感谢指针!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    相关资源
    最近更新 更多