Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery - How to marke touch and wheel event listeners as `passive`

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?

enter image description here

like image 616
jixodoj Avatar asked Oct 24 '20 15:10

jixodoj


People also ask

How do I make a passive event listener?

you should use the passive option in . addEventListener(), which doesn't block the event while your listener executes. document. addEventListener("mousewheel", somefunction(), { passive: false });

What are touch and wheel passive event listeners?

By marking a touch or wheel listener as passive, the developer is promising the handler won't call preventDefault to disable scrolling. This frees the browser up to respond to scrolling immediately without waiting for JavaScript, thus ensuring a reliably smooth scrolling experience for the user.

Can I use passive event listeners?

Event listeners created with the passive: true option cannot cancel ( preventDefault() ) the events they receive. Primarily intended to be used with touch events and wheel events. Since they cannot prevent scrolls, passive event listeners allow the browser to perform optimizations that result in smoother scrolling.

How do you fix using passive listeners to improve scrolling performance?

To fix this audit, add a passive: true flag to every event listener flagged by GTmetrix. For example, document. addEventListener('touchstart', onTouchStart, {passive: true});

Should jQuery be able to mark event listeners as passive?

Ideally jquery users would be able to mark event listeners as passive, so they can get the same performance benefits. Also @scottgonzalez mentions it would be good for jquery to polyfill the capturing API as well. Thoughts? Feel free to file any issues / questions with the spec here. What kind of support are you looking for?

How to make event listeners passive to improve scrolling performance?

How to make event listeners passive to improve scrolling performance. To solve this issue-: Go to the theme core file. Look for header.php or you can use a plugin to insert code into the header. Copy the code and paste it into the header.php file.

What are passive event listeners and how do they work?

What Passive Event Listeners basically does is try to eliminate the need for waiting for the work of touch and wheel event listeners to scroll, allowing users to have a much better and optimized scrolling experience. What Does Passive Event Listeners Do?

Is it possible to use jQuery with Blink event listeners?

Sign in to your account Blink has shipped support for EventListenerOptions and we expect to soon ship support for the passive option. Ideally jquery users would be able to mark event listeners as passive, so they can get the same performance benefits. Also @scottgonzalez mentions it would be good for jquery to polyfill the capturing API as well.


1 Answers

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 accomplish 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 });
    }
};
like image 58
Riad N Haddad Avatar answered Oct 21 '22 04:10

Riad N Haddad