Can't prevent `touchmove` from scrolling window on iOS

六月ゝ 毕业季﹏ 提交于 2019-11-27 20:06:16

I recently ran into this same problem. You'll need to pass { passive: false } when registering the touchmove event listener. e.g.

document.addEventListener('touchmove', function(e) {
    e.preventDefault();
}, { passive: false });

This is because document touch event listeners are now passive by default in Safari 11.1, which is bundled with iOS 11.3. This change is documented in the Safari 11.1 release notes:

Web APIs

  • [...]
  • Updated root document touch event listeners to use passive mode improving scrolling performance and reducing crashes.

You need to bind preventDefault to two events: touchmove and touchforcechange to make it work in ios 11, e.g.

document.addEventListener('touchmove', this.preventDefault, {passive: false});
document.addEventListener('touchforcechange', this.preventDefault, {passive: false});

And you should bind them before touchstart

If you bind them inside your touchstart or dragStart handler, they can only prevent scroll in the next dragging.

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