浏览器兼容性问题:典例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zlq_CSDN/article/details/88947284
/*
 * 首先来判断各大浏览器的类型
 */
function getOs() {
    var OsObject = "";
    if (isIE = navigator.userAgent.indexOf("MSIE") != -1) {
        return "MSIE";
    }
    if (isFirefox = navigator.userAgent.indexOf("Firefox") != -1) {
        return "Firefox";
    }
    if (isChrome = navigator.userAgent.indexOf("Chrome") != -1) {
        return "Chrome";
    }
    if (isSafari = navigator.userAgent.indexOf("Safari") != -1) {
        return "Safari";
    }
    if (isOpera = navigator.userAgent.indexOf("Opera") != -1) {
        return "Opera";
    }
}

//获取各浏览器的窗口高度
function scroll() {
    if(window.pageYOffset){ // IE9+ 和最新的浏览器
        return {
            top: window.pageYOffset,
            left: window.pageXOffset
        }
    }else if(document.compatMode === 'CSS1Compat'){ //  标准兼容模式开启(严格模式)
        return {
            top: document.documentElement.scrollTop,
            left: document.documentElement.scrollLeft
        }
    }
    return {
        top: document.body.scrollTop,
        left: document.body.scrollLeft
    }
}

//获取内容的可视高度
function client() {
    if(window.innerWidth){ // IE9+ 和 最新的浏览器
        return {
            width: window.innerWidth,
            height: window.innerHeight
        }
    }else if(document.compatMode === 'CSS1Compat'){
        return {
            width: document.documentElement.clientWidth,
            height: document.documentElement.clientHeight
        }
    }
    return {
        width: document.body.clientWidth,
        height: document.body.clientHeight
    }
}

//定义一个类似于jQuery的方法,通过id来获取dom元素
function $(id) {
    return typeof id === 'string' ? document.getElementById(id) : null;
}

猜你喜欢

转载自blog.csdn.net/zlq_CSDN/article/details/88947284