由于目标被视为被动,因此无法阻止被动事件侦听器内的Default-Chrome [英] Unable to preventDefault inside passive event listener due to target being treated as passive - Chrome

查看:1263
本文介绍了由于目标被视为被动,因此无法阻止被动事件侦听器内的Default-Chrome的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了以下代码,并开始得到以下提到的错误,该代码有什么问题以及对它的解决方法是什么.

I used the following code and started to get the below mention error what is wrong with the code and what is the fix for it.

由于目标被视为被动,因此无法阻止被动事件侦听器中的Default.请参见 https://www.chromestatus.com/features/6662647093133312

<script>
jQuery(window).scroll(function() {

  if (jQuery(this).scrollTop() > 400) {

    jQuery('.headerN').css("width", "100%");
    jQuery('.headerN').slideDown();
  } else {
    jQuery('.headerN').slideUp();
  }
});
</script>

推荐答案

在JQuery中,这仍然是一个未解决的问题:

In JQuery, it's still an open issue: https://github.com/jquery/jquery/issues/2871

您可以在事件中使用Vanilla js进行此操作:

You can do this with vanilla js on an event:

el.addEventListener('someEvent', someFn, { passive: false });

这是上面提到的github线程上的某人如何创建他们实施的解决方法:

this is how someone on the github thread mentioned above created a workaround they implemented:

jQuery.event.special.touchstart = {
    setup: function( _, ns, handle ){
        if ( ns.includes("noPreventDefault") ) {
            this.addEventListener("touchstart", handle, { passive: false });
        } else {
            this.addEventListener("touchstart", handle, { passive: true });
        }
    }
};

这篇关于由于目标被视为被动,因此无法阻止被动事件侦听器内的Default-Chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆