跨浏览器用javascript获取窗口的位置和大小

来源:互联网 发布:软件多开器电脑版 编辑:程序博客网 时间:2024/05/17 03:18

跨浏览器获取位置

var leftX = typeof window.screenLeft == 'number' ? window.screenLeft : window.screenX;var topY = typeof window.screenTop == 'number' ? window.screenTop : window.screenY;

firefox浏览器不支持screenLeft和scrennTop,但是支持screenX和screenY;ie浏览器支持screenLeft和scrennTop,但是不支持screenX和screenY
跨浏览器获取大小

var width = window.innerWidth;      //window.必须有,因为IE不支持var height = window.innerHeight;        //如果支持inner的,那么就使用它,//不支持的就是用document对象的方法if (typeof width != 'number') {    if (document.compatMode == 'CSS1Compat') {        width = document.documentElement.clientWidth; //标准ie        height = document.documentElement.clientHeight;    } else {   //非标准ie        width = document.body.clientWidth;            height = document.body.clientHeight;    }}
1 0