问题
A recent Lighthouse Report flagged the following issue.
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.
How do I resolve this issue? It appears to be related to jQuery.
回答1:
There was a long thread on this topic in https://github.com/jquery/jquery/issues/2871 in 2016
In short:
- jQuery can't add support to passive listeners.
Is expected that this is added in jQuery 4 (4 years and still in
- 5.x)
- The proposed fix is to add this code right after jQuery load:
jQuery.event.special.touchstart = {
setup: function( _, ns, handle ){
this.addEventListener("touchstart", handle, { passive: true });
}
};
回答2:
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") });
}
};
来源:https://stackoverflow.com/questions/60357083/does-not-use-passive-listeners-to-improve-scrolling-performance-lighthouse-repo