【问题标题】:JQuery - How to marke touch and wheel event listeners as `passive`JQuery - 如何将触摸和滚轮事件侦听器设置为“被动”
【发布时间】:2021-04-10 14:21:17
【问题描述】:

我正在使用Google Page Speed Insights 来优化我的页面速度。它告诉我不要使用被动侦听器来提高滚动性能。我知道如何使用 vanilla javascript 来做到这一点。

window.addEventListener(
  'scroll',
  () => {
    handleScroll();
  },
  { passive: true }
);

如何用 JQuery 做到这一点?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    这对我有用。 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 });
        }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-09
      • 2018-02-04
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-28
      • 1970-01-01
      • 2018-10-11
      相关资源
      最近更新 更多