简体   繁体   English

不使用被动侦听器来提高滚动性能(灯塔报告)

[英]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 can't add support to passive listeners. jQuery 无法添加对被动侦听器的支持。
  • Is expected that this is added in jQuery 4 (4 years and still in预计这是在 jQuery 4 中添加的(4 年并且仍在
  1. 5.x) 5.x)
  • The proposed fix is to add this code right after jQuery load:建议的修复是在 jQuery 加载后立即添加此代码:

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-:要解决此问题-:

  1. Go to the theme core file.转到主题核心文件。
  2. Look for header.php or you can use a plugin to insert code into the header.查找 header.php 或者您可以使用插件将代码插入到标题中。
  3. Copy the code and paste it into the header.php file.复制代码并将其粘贴到 header.php 文件中。
  4. If you are going to use the code into header.php, I would highly recommend you to use the PHP opening and closing tag.如果您打算在 header.php 中使用代码,我强烈建议您使用 PHP 开始和结束标记。

在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM