javascript - 警告 : Added non-passive event listener to a scroll-blocking 'touchstart' event

标签 javascript google-chrome

<分区>

我在 chrome 中打开应用程序时收到一个奇怪的警告。我不知道如何摆脱这个警告

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

任何人都可以帮我穿上这个。提前致谢

最佳答案

事件监听器API有更新。

简而言之:

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

变成这样:

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

因为在你的情况下你将事件监听器附加到 touchstart 它应该是这样的:

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

通过这种方式,您可以提前设置确切的事件以及是否支持被动接口(interface):

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

关于javascript - 警告 : Added non-passive event listener to a scroll-blocking 'touchstart' event,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46542428/

相关文章:

javascript - 无论如何,Jquery 在 Google Chrome 中都不会动画

android - 在不发布应用程序的情况下在 Android 手机上获取 chrome 应用程序

javascript - 如何使用 chrome 扩展在用户的文件系统上创建用户可访问的文件

html - chrome 单选按钮上的白色背景

javascript - chrome 浏览器不显示单选按钮或选择框

javascript - AngularJS 未捕获引用错误 : response is not defined

javascript - 在 api 请求解析之前,componentWillMount 检查未定义

javascript - Angular 嵌套类别和 *ngFor 循环

javascript - 在 Jinja 中模板化 JavaScript 函数

javascript - 这是在 Angular 2 中实现 Pipe 的正确方法吗