Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: Added non-passive event listener to a scroll-blocking 'touchstart' event [duplicate]

I am getting a weird warning while opening the application in chrome.I don't know how to get rid of this warning

[Violation] Added non-passive event listener to a scroll-blocking 'mousewheel' event. Consider marking event handler as 'passive' to make the page more responsive.

any one pls help me put on this.Thanks in advance

like image 711
yasarui Avatar asked Oct 03 '17 10:10

yasarui


People also ask

How do you fix does not use 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});

What is a passive event listener?

Passive event listeners are an emerging web standard, new feature shipped in Chrome 51 that provide a major potential boost to scroll performance. Chrome Release Notes. It enables developers to opt-in to better scroll performance by eliminating the need for scrolling to block on touch and wheel event listeners.

How do you make an event handler passive?

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 is wheel event listeners on the root?

Touch and wheel event listeners are useful for tracking user interactions and creating custom scrolling experiences, but they can also delay page scrolling. Currently, browsers can't know if an event listener will prevent scrolling, so they always wait for the listener to finish executing before scrolling the page.


1 Answers

There is an update of the API of event listeners.

In short this:

document.addEventListener('touchstart', handler, true); 

becomes this:

document.addEventListener('touchstart', handler, {capture: true}); 

Since in your case you attach event listener to touchstart it should be like that:

document.addEventListener('touchstart', handler, {passive: true}); 

This way you can setup in advance which exact event and if the passive interface is supported:

var passiveEvent = false; try {     var opts = Object.defineProperty({}, 'passive', {         get: function () {             passiveEvent = true;         }     });     window.addEventListener("test", null, opts); } catch (e) { }  // in my case I need both passive and capture set to true, change as you need it.     passiveEvent = passiveEvent ? { capture: true, passive: true } : true;  //if you need to handle mouse wheel scroll var supportedWheelEvent: string = "onwheel" in HTMLDivElement.prototype ? "wheel" :     document.onmousewheel !== undefined ? "mousewheel" : "DOMMouseScroll"; 

And use like this:

elementRef.addEventListener("touchstart", handler, passiveEvent); 

More details on passive event listeners here: https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md

like image 57
Martin Chaov Avatar answered Oct 01 '22 23:10

Martin Chaov