通过将触摸或轮子监听器标记为被动,开发人员承诺处理程序不会调用 preventDefault 来禁用滚动 . This frees the browser up to respond to scrolling immediately without waiting for JavaScript, thus ensuring a reliably smooth scrolling experience for the user .
addEventListener(document, "touchstart", function(e) {
console.log(e.defaultPrevented); // will be false
e.preventDefault(); // does nothing since the listener is passive
console.log(e.defaultPrevented); // still false
}, Modernizr.passiveeventlisteners ? {passive: true} : false);
1 回答
它使开发人员能够通过消除在触摸和滚轮事件监听器上滚动阻止的需要来选择更好地滚动性能 .
Problem: 所有现代浏览器都有一个线程滚动功能,即使在运行昂贵的JavaScript时也允许滚动顺利运行,但是这种优化部分地被等待任何
touchstart
和touchmove
处理程序的结果所取代,这可能会完全阻止滚动在事件上调用preventDefault()
.Solution:-
通过将触摸或轮子监听器标记为被动,开发人员承诺处理程序不会调用
preventDefault
来禁用滚动 .This frees the browser up to respond to scrolling immediately without waiting for JavaScript, thus ensuring a reliably smooth scrolling experience for the user
.DOM Spec,Demo Video,Explainer Doc