我正在使用Google Page Speed Insights来优化我的页面速度。它告诉我不要使用被动监听器来提高滚动性能。我知道如何使用vanilla javascript来做到这一点。
window.addEventListener(
'scroll',
() => {
handleScroll();
},
{ passive: true }
);
如何在JQuery中做到这一点?
发布于 2021-04-10 14:21:18
这对我很有效。只需在加载jQuery后直接添加即可
//被动事件监听器//在设置标志时有两个细微的变化,但似乎完成了相同的事情
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 });
}
};
https://stackoverflow.com/questions/64515007
复制相似问题