跳到主要内容

滑动错误提示:Unable to preventDefault inside passive event listener due to target being treated as passive

· 阅读需 1 分钟
lzw.

关于这个错误提示网上的文章很多,根据搜索和自己测试的结果情况,利用touch-action方法解决比较稳的一种:

html {
touch-action: none;
}

还有另外一种方式使用passive:false,如下代码:

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

这段代码本来只是解决错误提示问题,有没有跟我一样,以为是解决方案,就直接复制了,然后页面不动了,又到处搜索... ?

其实这个 passive:false 就是告诉浏览器,要马上使用 e.preventDefault() 来阻止了 touchmove 默认行为(既滑动行为),所以,出现页面无法滑动属于正常