在写一些特殊的页面时候可能会需要取消事件的默认行为,例如取消鼠标滚轮的默认滚动
window.addEventListener('mousewheel',function(e){
console.log('滚动了');
e.preventDefault()
}, { passive: false })
//火狐用DOMMouseScroll绑定滚轮事件
window.addEventListener('DOMMouseScroll', function(e){
console.log('滚动了');
e.preventDefault()
}, { passive: false });
先监听鼠标滚轮事件,mousewheel是鼠标滚轮滚动事件,scroll是页面滚动事件,这里监听鼠标滚轮滚动事件,用e.preventDefault()方法取消默认事件,但是运行时会报错,并且无法实现完美取消默认事件,经查阅资料得知由于浏览器需要先执行绑定事件看看有没有取消默认事件,这样导致在大部分没有取消默认事件行为的时候浏览器运行不流畅,所以在高版本的浏览器中,监听事件时会传递第三个参数{ passive: false },默认值为true,当为true时候不能取消默认事件,当值为false时候就可以取消默认事件了
touch-action
touch-action属性是为为移动端设置的css样式,可以控制在元素上的触屏事件
*{
touch-action: none;
}
这时候移动端就无法使用任何触屏事件了