警告:添加非被動事件偵聽器到滾動阻塞'touchstart'事件 [英] Warning: Added non-passive event listener to a scroll-blocking 'touchstart' event

查看:28851
本文介紹了警告:添加非被動事件偵聽器到滾動阻塞'touchstart'事件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述


我在chrome中打開應用程序時遇到了奇怪的警告。我不知道如何擺脫此警告。 [違規]向滾動阻止的
'mousewheel'事件添加了非被動事件偵聽器??紤]將事件處理程序標記為'被動'到
使頁面響應更快。


任何人都可以幫我把這個。提前感謝

解決方案

有一個事件監聽器API的更新。



簡而言之:

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

變成這樣:

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

因為在您的情況下,您將觸發器的事件監聽器附加到它應該是這樣的:

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

通過這種方式,您可以事先設置確切的事件以及被動接口是否被支持:

  var passiveEvent = false; 
var opts = Object.defineProperty({},'passive',{
get:function(){
passiveEvent = true;
}
});
window.addEventListener(test,null,opts);
} catch(e){}

//在我的情況下,我需要將passive和capture都設置為true,根據需要進行更改。
passiveEvent = passiveEvent? {capture:true,passive:true}:true;

//如果您需要在HTMLDivElement.prototype中處理鼠標滾輪
var supportedWheelEvent:string =onwheel? wheel:
document.onmousewheel!== undefined? mousewheel:DOMMouseScroll;

并且像這樣使用:

  elementRef.addEventListener(touchstart,handler,passiveEvent); 

有關被動事件偵聽器的更多詳細信息:
https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md

I am getting a weird warning while opening the application in chrome.I don't know how to get rid of this warning

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

any one pls help me put on this.Thanks in advance

解決方案

There is an update of the API of event listeners.

In short this:

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

becomes this:

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

Since in your case you attach event listener to touchstart it should be like that:

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

This way you can setup in advance which exact event and if the passive interface is supported:

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";

And use like this:

elementRef.addEventListener("touchstart", handler, passiveEvent);

More details on passive event listeners here: https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md

這篇關于警告:添加非被動事件偵聽器到滾動阻塞'touchstart'事件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持IT屋!

查看全文
登錄 關閉
掃碼關注1秒登錄
發送“驗證碼”獲取 | 15天全站免登陸
全免费A级毛片免费看无码播放