最近写react项目的时候,引用了antd-mobile,在使用滚动组件的时候,发现谷歌浏览器会报以下警告

最初我以为是antd-mobile的问题导致的,然后我就无查看了之前的vue的项目,发现了类似的问题,这是为什么呢?

更具这篇文章https://developers.google.cn/web/updates/2017/01/scrolling-intervention找到了答案

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

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

 举例:
wnidow.addEventListener('touchmove', func) 效果和下面一句一样
wnidow.addEventListener('touchmove', func, { passive: true })
这就导致了一个问题:

如果在以上这 3 个元素的 touchstart 和 touchmove 事件处理函数中调用 e.preventDefault() ,会被浏览器忽略掉,并不会阻止默认行为。
 那么如何解决这个问题呢?不让控制台提示,而且 preventDefault() 有效果呢?
两个方案:
1、注册处理函数时,用如下方式,明确声明为不是被动的
window.addEventListener('touchmove', func, { passive: false }) 2、应用 CSS 属性 touch-action: none; 这样任何触摸事件都不会产生默认行为,但是 touch 事件照样触发。

这里是touch-action 的详细解释https://w3c.github.io/pointerevents/#the-touch-action-css-property

关于Google浏览器Unable to preventDefault inside passive event listener due to target being treated as passive.的解决方案的更多相关文章

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

    Unable to preventDefault inside passive event listener due to target being treated as passive 今天在做项目 ...

  2. 移动端页面滑动时候警告:Unable to preventDefault inside passive event listener due to target being treated as passive.

    移动端项目中,在滚动的时候,会报出以下提示: [Intervention] Unable to preventDefault inside passive event listener due to ...

  3. [Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive.

    1.滑动时候警告[Intervention] Unable to preventDefault inside passive event listener due to target being tr ...

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

    最近两天企业微信下IScroll突然无法滚动了,特别慢,之前好好的,发现主要是有红色的Unable to preventDefault inside passive event listener du ...

  5. Unable to preventDefault inside passive event listener due to target being treated as passive 怎么办?

    本篇为转载,原文链接:https://blog.csdn.net/lijingshan34/article/details/88350456 翻译一下:chrome 监听touch类事件报错:无法被动 ...

  6. [Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. See https://www.chromestatus.com/features/5093566007214080

    相信如果用谷歌浏览器做移动端页面的时候 用touch事件的时候应该遇到过这个东东吧 documet.addEventListener("touchstart",function() ...

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

    使用滚动时候,新版google浏览器,会弹出如下的警告. 解决方法,可以加上* { touch-action: none; } 这句样式去掉. 其原因:https://developers.googl ...

  8. 滑动报 Unable to preventDefault inside passive event listener due to target being treated as passive 的解决方法

    google浏览器滑动出现以下问题: 解决办法如下:在html元素下添加样式 touch-action: none; html{ touch-action:none; }

  9. Unable to preventDefault inside passive event listener due to target being treated as passive. See https://www.chromestatus.com/features/5093566007214080

    解决办法: 两个方案:1.注册处理函数时,用如下方式,明确声明为不是被动的window.addEventListener('touchmove', func, { passive: false }) ...

随机推荐

  1. 结对测试vs随机测试

    在接口测试过程中,最关键的是对参数的各种情况进行测试. 随机测试是指随机选择一些参数值来测. 结对测试是指parewise算法生成较高"性价比"的组合情况来测. 随机测试存在的问题 ...

  2. Coprime Conundrum 容斥原理

    https://www.hackerrank.com/contests/hourrank-13/challenges/arthur-and-coprimes 我们可以枚举每一个p在[2, sqrt(n ...

  3. js点击修改按钮后修改

    <button id="click">改变内容</button> <div id="t">要改变的内容</div> ...

  4. 支付宝-API接口解析-转账到银行

    支付宝-API接口解析-转账到银行 扫码转账 测试地址 解析内容: alipays://platformapi/startapp?appId=09999988&actionType=toCar ...

  5. [BZOJ2190][SDOI2008]仪仗队 数学

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2190 看到这道题首先想到了NOI2010的能量采集,这不就是赤裸裸的弱化版吗?直接上莫比乌 ...

  6. Toast解析

    课程Demo public class MainActivity extends AppCompatActivity { private Button bt1; private Button bt2; ...

  7. 简述SVN服务器配置和客户端操作

    有关SVN服务器的搭建见:http://www.cnblogs.com/DwyaneTalk/p/3997688.html 搭建好环境之后,服务器端需要完成如下操作: 1.需要新建Repository ...

  8. MySQL——sql注入

    https://blog.csdn.net/lin_tuer/article/details/54809330 https://github.com/mysqljs/mysql#escaping-qu ...

  9. Bash Template

    #/bin/bash #set -x set -e usage() { cat <<EOF Usage: `basename $` [OPTIONS] <non-option arg ...

  10. vue-cli中使用rem,vue自适应

    2018年04月27日 09:25:34 董小礼 阅读数:4196    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_39588818/ar ...