android - touchmove事件如何禁止页面滚动
天蓬老师
天蓬老师 2017-05-16 13:35:17
[Android讨论组]

最近在H5页面上做了一个类似ios的 AssistiveTouch 的功能:一个悬浮的小挂件,可以在页面上移动,不用的时候自动贴边吸附。

出现的问题:

在Android手机上,移动小挂件的时候,页面会随之滚动。

因为 chrome浏览器为了提高页面滚动的流畅度,在新的chrome浏览器上,touchmove事件里不能用 event.preventDefault() 阻止页面滚动了。

新的绑定事件,需要这样处理 (加了一个 passive: false 属性)

document.addEventListener('click', onClick, {passive: false, capture: false});

但是我用的react,绑定监听直接用的

<p onTouchmove={::this.touchmove} >
</p>

touchmove 移动挂件的时候,怎么阻止页面滚动那?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(1)
PHP中文网

直接来。 资料在这里

Your event handlers will be passed instances of SyntheticEvent, a cross-browser wrapper around the browser's native event. It has the same interface as the browser's native event, including stopPropagation() and preventDefault(), except the events work identically across all browsers.

If you find that you need the underlying browser event for some reason, simply use the nativeEvent attribute to get it. Every SyntheticEvent object has the following attributes:

<p onTouchmove={this.touchmove.bind(this)} ></p>

touchmove(event){
  event.stopPropagation();
  event.nativeEvent.stopImmediatePropagation();
}

或者
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号