近期用react+antd-mobile写一个react案例,使用Carousel组件时,意外发现滑动切换时控制台出现如下提示

Unable to preventDefault inside passive event listener due to target being treated as passive.

 

百度了下,原因简述如下:

由于浏览器必须要在执行事件处理函数之后,才能知道有没有掉用过 preventDefault() ,这就导致了浏览器不能及时响应滚动,略有延迟。

所以为了让页面滚动的效果如丝般顺滑,从 chrome56 开始,在 window、document 和 body 上注册的 touchstart 和 touchmove 事件处理函数,会默认为是 passive: true。浏览器忽略 preventDefault() 就可以第一时间滚动了。

举例:
wnidow.addEventListener('touchmove', func) 效果和下面一句一样
wnidow.addEventListener('touchmove', func, { passive: true })

解决方法有以下两种:

1. 注册处理函数时,用如下方式,明确声明为不是被动的

window.addEventListener('touchmove', func, { passive: false })

2. 为容器设置css属性touch-action,去除滑动时默认现象产生,但不影响事件触发

touch-action: none;

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-04
  • 2021-08-08
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-11
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案