admin管理员组

文章数量:1313737

I'm using Google Page Speed Insights to optimize my page speed. It tells me to not use passive listeners to improve scrolling performance. I know how to do it with vanilla javascript.

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

How to do this with JQuery?

I'm using Google Page Speed Insights to optimize my page speed. It tells me to not use passive listeners to improve scrolling performance. I know how to do it with vanilla javascript.

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

How to do this with JQuery?

Share Improve this question asked Oct 24, 2020 at 15:26 jixodojjixodoj 3294 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

This worked for me. Just add it directly after jQuery has been loaded

// Passive event listeners //Two slight variations in setting the flag but seems to acplish same thing

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 });
    }
};

本文标签: javascriptJQueryHow to marke touch and wheel event listeners as passiveStack Overflow