当前位置: 代码迷 >> Web前端 >> 怎么判断鼠标滚轮在动以及滚动方向
  详细解决方案

怎么判断鼠标滚轮在动以及滚动方向

热度:182   发布时间:2012-10-28 09:54:44.0
如何判断鼠标滚轮在动以及滚动方向

<html>
<head>
<script type="text/javascript">
function handle(delta) {
??? var s = delta + ": ";
??? if (delta <0)
??????? s += "您在向下滚……";
??? else
??????? s += "您在向上滚……";
??? document.getElementById('delta').innerHTML = s;
}
?
function wheel(event){
??? var delta = 0;
??? if (!event) event = window.event;
??? if (event.wheelDelta) {
??????? delta = event.wheelDelta/120;
??????? if (window.opera) delta = -delta;
??? } else if (event.detail) {
??????? delta = -event.detail/3;
??? }
??? if (delta)
??????? handle(delta);
}
?

if (window.addEventListener)
window.addEventListener('DOMMouseScroll', wheel, false);
window.onmousewheel = document.onmousewheel = wheel;
</script>
</head>
<body>
<div id="delta">滚动中轮试试~请选按着中轮滚动,激活后可以不按,直接滚动。</div>
</body>
</html>

  相关解决方案