[Intervention] Unable to preventdefault inside passive event listener due to target being treated as passive. See <URL>

Source: Internet
Author: User
Tags event listener

This article is about the error of the question.

So what's the next thing to happen to this mistake?

由于浏览器必须要在执行事件处理函数之后,才能知道有没有掉用过 preventDefault() ,这就导致了浏览器不能及时响应滚动,略有延迟。 所以为了让页面滚动的效果如丝般顺滑,从 chrome56 开始,在 window、document 和 body 上注册的 touchstart 和 touchmove 事件处理函数,会默认为是 passive: true。浏览器忽略 preventDefault() 就可以第一时间滚动了。 举例: wnidow.addEventListener(‘touchmove‘, func) 效果和下面一句一样 wnidow.addEventListener(‘touchmove‘, func, { passive: true });
这样的处理就导致了一个问题:

如果在以上这 3 个元素的 touchstart 和 touchmove 事件处理函数中调用 e.preventDefault() ,会被浏览器忽略掉,并不会阻止默认行为。

一个有效的解决方法是
注册处理函数时,用如下方式,明确声明为不是被动的
window.addEventListener(‘touchmove‘, func, { passive: false });

This will not be an error in the console when you start the touch event.

[Intervention] Unable to preventdefault inside passive event listener due to target being treated as passive. See <URL>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.