js封装方法和浏览器内核

 浏览器内核

1    五大主流浏览器
2 1.IE      内核trident
3 2.Chrome  webkit/blink
4 3.Safari  webkit
5 4.firefox  Gecko
6 5.Opera    presto

js封装方法

//查看滚动条滚动的距离
function getScrollOffset(){
    if(window.pageXOffset){
        return{
            x:window.pageXOffset,
            y:window.pageYOffset
        }
    }else{
        return{
            x:document.body.scrollLeft+docuemnt.docmentElement.scrollLeft,
            y:document.body.scrollTop+docuemnt.docmentElement.scrollTop,
        }
    }
}
//查看视口的尺寸
function getViewportOffset(){
    if(window.innerWidth){
        return{
            w:window.innerWidth,
            h:windw.innerHeight
        }
    }else{
        if(docuemnt.compatMode=="BackCompat"){
            return{
               w:document.body.clientWidth,
               h:document.body.clientHeight
            }
        }
    
    }else{
        return{
            w:document.docuemntElement.clientWidth,
            h:document.docuemntElement.clientHeight
        }
    }
}
//判断是否是数组
function isArray(arr){
    return Object.prototype.toString.call(arr)=='[object Array]';
}
//封装兼容性的addEvent(elem,type,haddle);
function addEvent(elem,type,haddle){
    if(elem.addEventListener){
        elem.addEventListener(type,haddle,false);
    }else if(elem.attachEvent){
        elem.attachEvent('on'+type,funvtion (){
            handle.call(elem);
        })
    }else{
        elem['on'+type]=haddle;
    }
}
//封装取消冒泡
function stopBubble(event){
    if(event.stopPropagation){
        event.stopPropagation();
        }else{
            event.cancelBublle=true;
        }        
        }
//封装阻止默认事件的函数:cancelHanddler(event)
 function cancelHanddler(event){
     if(event.preventDefault){
        event.preventDefault();
     }else{
        event.returnValue=false; 
     }
 }
 //封装样式
 function getStyle(elem,prop){
  if(window.getComputedStyle){
      return window.getComputedStyle(elem,null)[prop];
  }esle{
      return elem.currentStyle[prop];
  }

 }

 

上一篇:25、静态查找表(顺序表、索引顺序表、静态树表、折半查找)


下一篇:程序中出现list assignment index out of range的解决方法