[英]Does not use passive listeners to improve scrolling performance (Lighthouse Report)
A recent Lighthouse Report flagged the following issue.最近的 Lighthouse 报告指出了以下问题。
Does not use passive listeners to improve scrolling performance
不使用被动侦听器来提高滚动性能
It also mentions...它还提到...
Consider marking your touch and wheel event listeners as
passive
to improve your page's scroll performance.考虑将您的触摸和滚轮事件侦听器标记为
passive
以提高页面的滚动性能。
How do I resolve this issue?我该如何解决这个问题? It appears to be related to jQuery.
它似乎与jQuery有关。
There was a long thread on this topic in https://github.com/jquery/jquery/issues/2871 in 2016 2016 年在https://github.com/jquery/jquery/issues/2871中有一个关于这个主题的长线程
In short:简而言之:
jQuery.event.special.touchstart = {
setup: function( _, ns, handle ){
this.addEventListener("touchstart", handle, { passive: true });
}
};
UPDATE 2021: Add the following code after jquery. 2021 年更新:在 jquery 之后添加以下代码。 This will fix it and removes the Pagespeed warning
这将修复它并删除 Pagespeed 警告
jQuery.event.special.touchstart = {
setup: function( _, ns, handle ) {
this.addEventListener("touchstart", handle, { passive: !ns.includes("noPreventDefault") });
}
};
jQuery.event.special.touchmove = {
setup: function( _, ns, handle ) {
this.addEventListener("touchmove", handle, { passive: !ns.includes("noPreventDefault") });
}
};
jQuery.event.special.wheel = {
setup: function( _, ns, handle ){
this.addEventListener("wheel", handle, { passive: true });
}
};
jQuery.event.special.mousewheel = {
setup: function( _, ns, handle ){
this.addEventListener("mousewheel", handle, { passive: true });
}
};
This has done the trick !这已经成功了!
// Passive event listeners
jQuery.event.special.touchstart = {
setup: function( _, ns, handle ) {
this.addEventListener("touchstart", handle, { passive: !ns.includes("noPreventDefault") });
}
};
jQuery.event.special.touchmove = {
setup: function( _, ns, handle ) {
this.addEventListener("touchmove", handle, { passive: !ns.includes("noPreventDefault") });
}
};
How to make event listeners passive to improve scrolling performance.如何使事件侦听器被动以提高滚动性能。 To solve this issue-:
要解决此问题-:
<script>!function(e){"function"==typeof define&&define.amd?define(e):e()}(function(){var e,t=["scroll","wheel","touchstart","touchmove","touchenter","touchend","touchleave","mouseout","mouseleave","mouseup","mousedown","mousemove","mouseenter","mousewheel","mouseover"];if(function(){var e=!1;try{var t=Object.defineProperty({},"passive",{get:function(){e=!0}});window.addEventListener("test",null,t),window.removeEventListener("test",null,t)}catch(e){}return e}()){var n=EventTarget.prototype.addEventListener;e=n,EventTarget.prototype.addEventListener=function(n,o,r){var i,s="object"==typeof r&&null!==r,u=s?r.capture:r;(r=s?function(e){var t=Object.getOwnPropertyDescriptor(e,"passive");return t&&!0!==t.writable&&void 0===t.set?Object.assign({},e):e}(r):{}).passive=void 0!==(i=r.passive)?i:-1!==t.indexOf(n)&&!0,r.capture=void 0!==u&&u,e.call(this,n,o,r)},EventTarget.prototype.addEventListener._original=e}});</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.