//1.滚动条到顶端的距离(滚动高度) var scrollTop = document.documentElement.scrollTop || document.body.scrollTop; //2.滚动条到左端的距离 var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft; //3. IE9以下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 ++){ //找出与我指定class名相同的对象 if(eles[i].className === className){ arr.push(eles[i]); //存入数组 } } return arr; //返回 } } //4. 获取非行内样式兼容 function getStyle(obj,attr){ return window.getComputedStyle ? getComputedStyle(obj,true)[attr] : obj.currentStyle[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. 获取键盘按键编码值的兼容 var key = evt.keyCode || evt.charCode || evt.which; //8. 阻止事件冒泡的兼容 非IE IE e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true; //9. 阻止超链接的默认行为的兼容 evt.preventDefault ? evt.preventDefault() : evt.returnValue = false; //10. 添加事件监听器的兼容 function addEventListener(obj,event,fn,boo){ if(obj.addEventListener){ obj.addEventListener(event,fn,boo); }else if(obj.attachEvent){ obj.attachEvent('on' + event,fn); } } //11. 移除事件监听器的兼容 function removeEventListener(obj,event,fn,boo){ if(obj.removeEventListener){ obj.removeEventListener(event,fn,boo); }else if(obj.detachEvent){ obj.detachEvent('on' + event,fn); } } //12. 获取事件源的兼容 var target = event.target || event.srcElement; //13.防止默认拖拽行为 document.ondragstart = function(){ return false; } //14.创建Ajax对象的兼容(IE6) var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP')