首页 > 编程语言 > 详细

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

时间:2015-07-06 12:15:00      阅读:284      评论:0      收藏:0      [点我收藏+]

跨浏览器获取位置

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;
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

原文:http://blog.csdn.net/lfcss/article/details/46772275

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!