問題
當使用fastClick.js設置點擊事件時,控制台報錯:
[Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. See https://www.chromestatus.com/features/5093566007214080
意思是 無法被動監聽事件中的默認事件.
preventDefault就是阻止事件的默認事件,如果設置了preventDefault,則form表單不會提交,a鏈接不會跳轉
原因產生
AddEventListenerOptions defaults passive to false. With this change touchstart and touchmove listeners added to the document will default to passive:true (so that calls to preventDefault will be ignored)..
If the value is explicitly provided in the AddEventListenerOptions it will continue having the value specified by the page.
This is behind a flag starting in Chrome 54, and enabled by default in Chrome 56. See https://developers.google.com/web/updates/2017/01/scrolling-intervention
AddEventListenerOptions默認為被動為false。通過此更改,添加到文檔中的touchstart和touchmove偵聽器將默認為passive:true(以便忽略對preventDefault的調用)。。
如果AddEventListenerOptions中顯式提供了該值,則它將繼續具有頁面指定的值。
這在Chrome 54開始的標志后面,在Chrome 56中默認啟用。參見https://developers.google.com/web/updates/2017/01/scrolling-intervention
解決方案
- 添加監聽事件選項{passive: false}
window.addEventListener('touch事件',[callback],{passive: false}); - 使用css屬性
touch-action: none; 這樣任何觸摸事件都不會產生默認行為,而且touch事件還會觸發