轉自:https://segmentfault.com/a/1190000008512184
?
測試:
body {margin: 0;height: 2000px;background: linear-gradient(to bottom, red, green);
}// 在 chrome56 中,照樣滾動,而且控制臺會有提示,blablabla
window.addEventListener('touchmove', e => e.preventDefault())
那么如何解決這個問題呢?不讓控制臺提示,而且 preventDefault() 有效果呢?
兩個方案:
1、注冊處理函數時,用如下方式,明確聲明為不是被動的
var?func = function(e){
e.preventDefault();//firefox等
e.returnValue = false;
}
window.addEventListener('touchmove', func, { passive: false })
2、應用 CSS 屬性?touch-action: none;
?這樣任何觸摸事件都不會產生默認行為,但是 touch 事件照樣觸發。
touch-action 還有很多選項,詳細請參考touch-action
[注]未來可能所有的元素的 touchstart touchmove 事件處理函數都會默認為 passive: true