获取屏幕的宽度和高度

  在js中,特别是在一些功能,比如底部返回哪些按钮等,经常需要用到,这里综合运用以下,以便可以以后快速获取:

/**
 * 获取屏幕的宽度和高度
 * @returns {*}
 */
function client() {
    if(window.innerWidth){ // ie9+ 最新的浏览器
        return {
            width: window.innerWidth,
            height: window.innerHeight
        }
    }else if(document.compatMode === "CSS1Compat"){ // W3C
        return {
            width: document.documentElement.clientWidth,
            height: document.documentElement.clientHeight
        }
    }

    return {
        width: document.body.clientWidth,
        height: document.body.clientHeight
    }
}

  好了,至于怎么引入这个文件,想必大家都很熟悉了,这里就不一一介绍了。

猜你喜欢

转载自www.cnblogs.com/songtianfa/p/12187819.html