使用js对鼠标滚轮事件进行控制,滚轮事件其中考虑浏览器兼容性问题
其中火狐浏览器事件为:"DOMMousescroll";其余浏览器是:"onmousewheel"
onmousewheel事件在火狐浏览器中为:"null";
滚轮事件绑定:
//事件绑定
function addEvent(obj,sEv,fn){
if(obj.addEventListener){
// addEventListener(高级浏览器)
obj.addEventListener(sEv,fn,false);
}else{
// attachEvent(iE8-使用)
obj.attachEvent('on'+sEv,fn);
}
}
//阻止默认行为
function preventDefault(ev){
var oEvent = ev || event;
if(ev.preventDefault){
ev.preventDefault();
}else{
ev.returnValue = false;
}
//滚轮事件
function addWheel(obj,fn){
if(obj.onmousewheel === null){
addEvent(obj,'DOMMouseScroll',wheel);
}else{
addEvent(obj,'mousewheel',wheel);
}
function wheel(ev){
var oEvent = ev || event;
var bDown = oEvent.wheelDelta ? oEvent.wheelDelta<0 : oEvent.detail>0;fn && fn(bDown);
preventDefault(ev);
}
}