var mouseEvent = function(){
02. var arg = arguments[0],
03. el = arg.el || document,
04. leftfn = arg.left || function(){},
05. rightfn = arg.right || function(){},
06. middlefn = arg.middle || function(){},
07. buttons = {};
08. el.onmousedown = function(e){
09. e = e || window.event;
10. if(!+"\v1"){
11. switch(e.button){
12. case 1:buttons.left = true; break;
13. case 2:buttons.right = true; break;
14. case 4:buttons.middle = true; break;
15. }
16. }else{
17. switch(e.which){
18. case 1:buttons.left = true;break;
19. case 2:buttons.middle = true; break;
20. case 3:buttons.right = true;break;
21. }
22. }
23. if(buttons.left){
24. leftfn();
25. }else if(buttons.middle){
26. middlefn();
27. }else if(buttons.right){
28. rightfn();
29. }
30. buttons = {
31. "left":false,
32. "middle":false,
33. "right":false
34. };
35. }
36.}
获取鼠标位置
var getCoordInDocument = function(e) {
02. e = e || window.event;
03. var x = e.pageX || (e.clientX +
04. (document.documentElement.scrollLeft
05. || document.body.scrollLeft));
06. var y= e.pageY || (e.clientY +
07. (document.documentElement.scrollTop
08. || document.body.scrollTop));
09. return {'x':x,'y':y};
10.}
至于mouseover,mousemove,mouseout没有什么好说,并且无浏览器差异。我们来看鼠标滚轮事件,这个差异很严重。IE、 Safari、 Opera 、chrome是mousewheel事件,Firefox是DOMMouseScroll事件。事件属性方面,IE等是event. wheelDelta,Firefox是event. detail。IE等往上滚一圈为120,往下滚一圈为-120。Firefox往上滚一圈为-3,往下滚一圈为3。我们可以构造一个函数来削除它们的差异。
01.var mouseScroll = function(fn){
02. var roll = function(){
03. var delta = 0,
04. e = arguments[0] || window.event;
05. delta = (e.wheelDelta) ? e.wheelDelta / 120 : -(e.detail || 0) / 3;
06. fn(delta);//回调函数中的回调函数
07. }
08. if(/a/[-1]=='a'){
09. document.addEventListener('DOMMouseScroll', roll, false);
10. }else{
11. document.onmousewheel = roll;
12. }
13.}
此函数接受一函数作为参数,如:
1.mouseScroll(function(delta){
2. var obj = document.getElementById('scroll'),
3. current = parseInt(obj.offsetTop)+(delta*10);
4. obj.style.top = current+"px";
5.});
原文:http://www.cnblogs.com/rubylouvre/archive/2009/08/24/1552862.html