我使用了以下代码并开始收到下面提到的错误,代码有什么问题以及修复方法是什么。
Unable to preventDefault inside passive event listener due to target being treated as passive. See 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 中,这仍然是一个悬而未决的问题:https://github.com/jquery/jquery/issues/2871
您可以在事件中使用 vanilla js 来执行此操作:
el.addEventListener('someEvent', someFn, { passive: false });
这就是上面提到的 github 线程上的某人创建他们实现的解决方法的方式:
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 });
}
}
};
关于javascript - 由于目标被视为被动,因此无法阻止被动事件监听器内的默认设置 - Chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55461449/