Does not use passive listeners to improve scrolling performance (Lighthouse Report)

社会主义新天地 提交于 2021-02-17 08:48:13

问题


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

    1. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!