Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to preventDefault inside passive event listener

I'm using Framework7 sortable list and it works well, just that it doesn't trigger an event when the list is changed.

So I'm trying a few built-in events:

$('.sortable-handler').on('touchstart', function (e) {
    e.preventDefault();
    alert('touchstart');
});

$('.sortable-handler').on('touchmove', function (e) {
    e.preventDefault();
    console.log('touchmove');
});

$('.sortable-handler').on('touchcancel', function (e) {
    e.preventDefault();
    console.log('touchcancel');
});

$('.sortable-handler').mouseleave(function (e) {
    e.preventDefault();
    console.log('mouseleave');
});

.. but all I get is:

Unable to preventDefault inside passive event listener due to target being treated as passive. See https://www.chromestatus.com/features/5093566007214080

Which event should I look for to get the updated list on every sort?

like image 755
eozzy Avatar asked Feb 07 '17 23:02

eozzy


People also ask

How do I fix unable preventDefault inside passive event listener invocation?

Remove the need for preventDefault() in the event listener The correct way to fix this issue is by finding the event listener responsible for generating the intervention violation and removing the preventDefault() call in that event listener. The preventDefault() call can't be used in a passive event listener.

What are passive event listeners?

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 to remove the need for preventDefault() in the event listener?

Remove the need for preventDefault () in the event listener The correct way to fix this issue is by finding the event listener responsible for generating the intervention violation and removing the preventDefault () call in that event listener. The preventDefault () call can't be used in a passive event listener.

How to fix touch event listeners that are passive by default?

Since Chrome 56, all touch event listeners are passive by default. Check this in-depth video if you want to learn more about passive event listeners and scrolling smoothness. There are two ways to solve this violation. We recommend the first option. 1. Remove the need for preventDefault () in the event listener

What are passive event listeners in JavaScript?

Developer-defined “passive event listeners” solve this. When you add a touch event with a {passive: true} object as the third parameter in your event handler then you are telling the browser that the touchstart listener will not call preventDefault () and the browser can safely perform the scroll without blocking on the listener.

What are the JavaScript event listeners violations?

This violation is about the javascript Event Listeners. Since Chrome 51, an event listener can be set as "passive". Passive event listeners were introduced to optimize scrolling performance on a device. When you use a passive event listener on your site, you promise not to use a preventDefault () in that listener to disable scrolling.


3 Answers

See this blog post. If you call preventDefault on every touchstart then you should also have a CSS rule to disable touch scrolling like

.sortable-handler {
  touch-action: none;
}
like image 85
Rick Byers Avatar answered Oct 19 '22 10:10

Rick Byers


For me

document.addEventListener("mousewheel", this.mousewheel.bind(this), { passive: false });

did the trick (the { passive: false } part).

like image 53
ranbuch Avatar answered Oct 19 '22 11:10

ranbuch


In plain JS add { passive: false } as third argument

document.addEventListener('wheel', function(e) {
    e.preventDefault();
    doStuff(e);
}, { passive: false });
like image 26
Halfacht Avatar answered Oct 19 '22 11:10

Halfacht