这是一个很特殊的事件,w3c默认是承认为“mousewheel”,但我在它上面找到的资料比较少,只发现一个。号称最标准的FF,用一个私有实现DOMMouseScroll。总之实现很混乱。我们先看各浏览器对它的支持程度吧。
IEfirefoxsafarichromeopera
window对象falsetruetruetruetrue
文档对象truetruetruetruetrue
元素节点truetruetruetruetrue
先看FF的事件分派
window.addEventListener("DOMMouseScroll",function(event){
alert(event.type)
alert(event.clientY)
},false);
var event = document.createEvent("MouseEvent");
//为了证明分派成功,特意将其clientY设为90
event.initMouseEvent("DOMMouseScroll",true, null, window,0,0,0,0,90,false,false,false,false,0,null);
window.dispatchEvent(event)
<!doctype html>
<html lang="zh-ch">
<head>
<meta charset="utf-8" />
<meta content="IE=8" http-equiv="X-UA-Compatible"/>
<title>mousewheel的事件分派 by 司徒正美</title>
<script type="text/javascript">
window.onload = function(){
window.addEventListener("DOMMouseScroll",function(event){
alert(event.type)
alert(event.clientY)
},false);
var event = document.createEvent("MouseEvent");
//为了证明分派成功,特意将其clientY设为90
event.initMouseEvent("DOMMouseScroll",true, null, window,0,0,0,0,90,false,false,false,false,0,null);
window.dispatchEvent(event)
}
</script>
<style title="text/css">
</style>
</head>
<body>
</body>
</html>
运行代码
我们可以看到虽然其他标准浏览器也支持这个名为DOMMouseScroll的事件发派,但当我们手动滚动鼠标滑轮时,也只有FF有发应,弹出两个alert。
对于其他标准浏览器,我们用mousewheel