滚轮事件封装的优化,更符合面向对象思想.跟js原生保持一致写法

// 鼠标滚轮滚

Object.prototype.onmousewheelhandle =function(handle){

var info = navigator.userAgent;

var down = null;

if(info.indexOf("Firefox") != -1){

this.addEventListener("DOMMouseScroll",function(event){

var ev = event || window.event;

if(ev.Detail > 0){

down  = true;

}else{

down = false;

}

handle.apply(this,[down,ev]);

},false)

}else{

this.onmousewheel = function(event){

var ev = event || window.event;

if(ev.wheelDelta > 0){

down = false;

}else{

down = true;

}

handle.call(this,down,ev);

}

}

}

优化之后,只要是对象都可以去调用此方法,更加方便.

var box = document.getElementById("box"); //获取元素

box.onmousewheelhandle(function(down){  // 调用方法

console.log(down)

})

 

posted on 2016-11-19 16:15  BlindingSunlight  阅读(245)  评论(0编辑  收藏  举报