<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<style>
.wrap{
width: 200px;
height: 200px;
background: #ff6600;
position: absolute;
left: 50%;
top: 50%;
margin-top: -100px;
margin-left: -100px;
}
</style>
<script>
window.onload=function () {
var textNode=document.querySelector(".wrap");
//火狐
if (textNode.addEventListener){
textNode.addEventListener("DOMMouseScroll",fn)
}
textNode.onmousewheel=fn;
function fn(ev) {
ev=ev||event;
var dir="";
if (ev.wheelDelta){
dir=ev.wheelDelta>0?"up":"down";
}
if (ev.detail){
dir=ev.detail<0?"up":"down";
}
console.log(dir);
//禁止事件默认行为(火狐)
if(ev.preventDefault()){
ev.preventDefault()
}
//禁止事件默认行为
return false;
}
}
</script>
<body>
<div class="wrap">
</div>
</body>
</html>
滚轮方向兼容
最新推荐文章于 2022-09-03 10:48:26 发布