js之可移植的查询窗口大小和位置

来源:互联网 发布:淘宝上新频率 编辑:程序博客网 时间:2024/03/29 01:36
var Geometry = {};
if(window.screenLeft){ //IE and others
Geometry.getWindowX = function(){return window.screenLeft;};
Geometry.getWindowY = function(){return window.screenTop;};
}
else if{ //Firefox and others
Geometry.getWindowX = function(){return window.screenX;};
Geometry.getWindowY = function(){return window.screenY;};
}

if(windows.innerWidth){ //All browsers but IE
Geometry.getViewportWidth = function(){return windows.innerWidth;};
Geometry.getViewportHeight = function(){return windows.innerHeight ;};
Geometry.getHorizontalScroll = function(){return window.pageXOffset;};
Geometry.getVerticalScroll = function(){return window.pageYOffset;};
}
else if(document.documentElement && document.documentElement.clientWidth){
//These functions are for IE6 when there is a DOCTYPE
Geometry.getViewportWidth = function(){return document.documentElement.clientWidth};
Geometry.getViewportHeight = function(){return document.documentElement.clientHeight};
Geometry.getHorizontalScroll = function(){return document.documentElement.scrollLeft;};
Geometry.getVerticalScroll = function(){return document.documentElement.scrollTop;};
}
else if(document.body.clientWidth){
//These are for IE4,IE5,and IE6 without a DOCTYPE
Geometry.getViewportWidth = function(){return document.body.clientWidth};
Geometry.getViewportHeight = function(){return document.body.clientHeight};
Geometry.getHorizontalScroll = function(){return document.body.scrollLeft;};
Geometry.getVerticalScroll = function(){return document.body.scrollTop;};
}

//These functions return the size of the document.They are not window related,but they are 
//useful to have here anyway
if(document.documentElement && document.documentElement.scrollWidth){
Geometry.getDocumentWidth = function(){return document.documentElement.scrollWidth;};
Geometry.getDocumentHeight  = function(){return document.documentElement.scrollHeight;};
}
else if(document.body.scrollWidth){
//适用于IE4,IE5,and IE6 without a DOCTYPE,以及chrome,firefox等其他浏览器
Geometry.getDocumentWidth = function(){return document.body.scrollWidth;};
Geometry.getDocumentHeight  = function(){return document.body.scrollHeight;};
}
0 0
原创粉丝点击