M87星云

导航

js自定义获取浏览器宽高

/**
* @description js自定义获取浏览器宽高
*
* IE8 和 IE8 以下的浏览器不兼容
* window.innerWidth
* window.innerHeight
*
* html 头文件部加 <!doctype html> 表示启用标准模式
* 标准模式 兼容所有浏览器
*
* document.body.clientWidth
* document.body.clientHeight
*
* document.documentElement.clientWidth
* document.documentElement.clientHeight
*
* @returns {width:0,height:0}
*/
function getViewPortOffset(){
if(window.innerWidth){
return {
width:window.innerWidth,
height:window.innerHeight
}
}else{
//混杂模式(兼容IE8以下)
if(document.compatMode == 'BackCompat'){
return{
width:document.body.clientWidth,
height:document.body.clientHeight
}
}else{
return {
width:document.documentElement.clientWidth,
height:document.documentElement.clientHeight
}
}
}
}

posted on 2019-07-10 20:55  挽留匆匆的美丽  阅读(481)  评论(0编辑  收藏  举报