//1. 滚动条到顶端的距离
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
//2. 滚动条到左边的距离
var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
//3. byClassName的兼容
function byClassName(obj,className){
//判断是否支持byClassName方法
if(obj.getElementsByClassName){
//直接使用
return obj.getElementsByClassName(className);
}else{ //不支持
//获取所有的标签
var eles = obj.getElementsByTagName('*');
var arr = [];
//获取每一个标签(对象)
for(var i = 0,len = eles.length;i < len;i ++){
//判断每一个对象是否具有指定的className
if(eles[i].className === className){
arr.push(eles[i]);
}
}
return arr;
}
}
4获取非行内样式的兼容
function getStyle(obj,attr){
return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj,1)[attr];
}
//5. 获取事件对象的兼容
evt = evt || window.event;
//6. 获取鼠标编码值的兼容
function getButton(evt){
var e = evt || window.event;
if(evt){
return e.button;
}else if(window.event){
switch(e.button){
case 1 : return 0;
case 4 : return 1;
case 2 : return 2;
}
}
}
//7. 阻止事件冒泡的兼容
event.stopPropagation ? event.stopPropagation() : event.cancelBubble = true;
//8. 获取键盘编码值onkeypress的兼容
event.keyCode || event.charCode || event.which
转载于:https://www.cnblogs.com/pppoe/p/10399075.html