Javascript 向阻止滚动的“touchstart”事件添加了非被动事件侦听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46094912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Added non-passive event listener to a scroll-blocking 'touchstart' event
提问by torbenrudgaard
Suddenly today out of nowhere I started getting this one on every page on our website
今天突然间我突然开始在我们网站的每一页上都有这个
Added non-passive event listener to a scroll-blocking 'touchstart' event.
Consider marking event handler as 'passive' to make the page more responsive
And its not just once or twice.. its like thousands of them....
它不仅仅是一次或两次......它就像成千上万......
They are running amok.
他们正在疯狂地奔跑。
The only way to stop the flood of violations is to comment out this line
阻止违规泛滥的唯一方法是注释掉这一行
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js" type="text/javascript"></script>
I read the other posts on what this violation means, but I really cannot see that I did anything different between two hours ago and now (I did a full rollback just to see if it helped)
我阅读了有关此违规行为的含义的其他帖子,但我真的看不出我在两个小时前和现在之间做了什么不同的事情(我做了一个完整的回滚只是为了看看它是否有帮助)
Its almost like someone put a bug into jquery.min.js but I seriously doubt that cause then everyone would get it.
这几乎就像有人在 jquery.min.js 中放入了一个错误,但我严重怀疑这是因为每个人都会得到它。
Any ideas? I tried debugging everything I could, and I still have no idea what causes this?!?
有任何想法吗?我尝试了我所能调试的一切,但我仍然不知道是什么原因造成的?!?
UPDATE
更新
I replaced all <button><md-tooltip>text</md-tooltip></button>width <button data-toggle="tooltip" title="text"></button>This removed 99% of all the violations.
我替换了所有<button><md-tooltip>text</md-tooltip></button>宽度<button data-toggle="tooltip" title="text"></button>这消除了所有违规的 99%。
采纳答案by Salketer
Ok digging this up a little more, this is not a new behavior, it has been reported a while ago and jQuery still hasn't fixed it.
好吧,再深入研究一下,这不是一个新行为,它已经在不久前被报道过了,jQuery 仍然没有修复它。
The problem lies in the fact that for an handler to be passiveit has to be certain of never calling preventDefault()but jQuery doesn't know in advance...
问题在于,对于一个处理程序来说,passive它必须确定从不调用,preventDefault()但 jQuery 不知道......
The only tip I could give you is change your console logging level and remove "Verbose". Follow up on this issuefor ideas on solving this.
回答by Sergio
This solve the problem to me:
这解决了我的问题:
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 });
}
}
};
回答by neel singh
I am using various events and this seems to solve my use case
我正在使用各种事件,这似乎解决了我的用例
(function () {
if (typeof EventTarget !== "undefined") {
let func = EventTarget.prototype.addEventListener;
EventTarget.prototype.addEventListener = function (type, fn, capture) {
this.func = func;
if(typeof capture !== "boolean"){
capture = capture || {};
capture.passive = false;
}
this.func(type, fn, capture);
};
};
}());
回答by Hans Ganteng
The answer from Sergio is correct, add it at the bottom jquery script. If there is issue touchstart and touchmove, just add same code and replace touchstart with touchmove, like this:
Sergio 的答案是正确的,将其添加到底部 jquery 脚本中。如果 touchstart 和 touchmove 出现问题,只需添加相同的代码并将 touchstart 替换为 touchmove,如下所示:
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 });
}
}
};
jQuery.event.special.touchmove = {
setup: function( _, ns, handle ){
if ( ns.includes("noPreventDefault") ) {
this.addEventListener("touchmove", handle, { passive: false });
} else {
this.addEventListener("touchmove", handle, { passive: true });
}
}
};


