获取浏览器可视屏幕宽度

1.各浏览器

// 360
innerWidth=outerWidth    //窗口宽度
innerHeight=outerHeight  //窗口高度  (仅白色body区域,不包顶部和尾部状态栏)

// chrom
innerWidth=outerWidth    //窗口宽度
innerHeight!=outerHeight  //窗口高度  
innerHeight 是白色body区域
outerHeight 是body+顶部地址栏等的高度

//ie8
alert(innerWidth)  // ie 无法识别该属性,报错,应写成window.innerWidth; 但也无法得出宽度,返回undefined


//Firefox

innerWinth=outerWinth  //基本相等,但有10几像素误差 ?
innerHeight  //body的高度
outerHeight  //body +顶部地址栏等高度+状态栏, 有几像素误差 ?

2.获取body的宽、高方法

var width = window.innerWidth; //这里要加window,因为IE 会无效;window.innerWidth在ie浏览器返回undefined
var height = window.innerHeight;

if (typeof width != 'number') { //如果是IE,就使用document
    if (document.compatMode == 'CSS1Compat') {  //标准模式
        width = document.documentElement.clientWidth;
        height = document.documentElement.clientHeight;
    } else {
        width = document.body.clientWidth;  //非标准模式使用body
        height = document.body.clientHeight;
    }
}
alert(width);  
alert(height);      
//是ie则进入if语句,再判断是标准还是非标准  ,其他浏览器直接根据变量得出宽高

 3.窗口的位置

//ie支持,火狐不支持:
alert(screenLeft); 
alert(screenTop);
alert(typeof screenLeft); //ie支持,返回number;不支持的返回undefined

//火狐支持,ie不支持:
alert(screenX);
alert(screenY);


//跨浏览器兼容:
var leftX=(typeof screenLeft=="number")?screenLeft:screenX;
var topY=(typeof screenTop=="number")?screenTop:screenY;

  

转载于:https://www.cnblogs.com/luhailin/p/6595224.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值