在Chrome浏览器中打开应用程序时,我遇到了一个奇怪的警告。 我不知道如何摆脱这个警告。
[Violation] 添加了非被动事件监听器到阻止滚动的'mousewheel'事件。 考虑将事件处理程序标记为“被动”,以使页面更具响应性。
请有人帮我解决这个问题。提前致谢。
在Chrome浏览器中打开应用程序时,我遇到了一个奇怪的警告。 我不知道如何摆脱这个警告。
[Violation] 添加了非被动事件监听器到阻止滚动的'mousewheel'事件。 考虑将事件处理程序标记为“被动”,以使页面更具响应性。
请有人帮我解决这个问题。提前致谢。
API的事件监听器有更新。
简单来说:
document.addEventListener('touchstart', handler, true);
变成这样:
document.addEventListener('touchstart', handler, {capture: true});
因为在您的情况下,您将事件监听器附加到touchstart事件,所以应该是这样的:
document.addEventListener('touchstart', handler, {passive: true});
通过这种方式,您可以提前设置确切的事件以及是否支持被动接口:
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";
并且像这样使用:
elementRef.addEventListener("touchstart", handler, passiveEvent);
有关被动事件侦听器的更多细节,请参见:https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md