您的位置:首页 > 运维架构

看图理解scrollTop,scrollLeft,clientWidth,clientHeight,offsetWidth,offsetHeight

2012-06-06 22:01 696 查看



 

每次用JS获取页面的高宽时总都是相当的揪心,同一个属性在不同的浏览器或不同的W3C标准下所表示的意思都不尽相同。以下就针对页面的实际高宽和可见区域做个总结,以便大家查阅!

1.
在W3C标准的情况下


W3C标准页面,即在HTML代码头部加入

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
在IE中:

document.body.clientWidth ==> BODY对象宽度

document.body.clientHeight ==> BODY对象高度

document.documentElement.clientWidth ==> 可见区域宽度

document.documentElement.clientHeight ==> 可见区域高度
 
在FireFox中:

document.body.clientWidth ==> BODY对象宽度

document.body.clientHeight ==> BODY对象高度

document.documentElement.clientWidth ==> 可见区域宽度

document.documentElement.clientHeight ==> 可见区域高度
 
在Opera中:

document.body.clientWidth ==> 可见区域宽度

document.body.clientHeight ==> 可见区域高度

document.documentElement.clientWidth ==> 页面对象宽度(即BODY对象宽度加上Margin宽)

document.documentElement.clientHeight ==> 页面对象高度(即BODY对象高度加上Margin高)

2.
在无W3C标准的情况下


即在HTML代码头部无

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
在IE中:

document.body.clientWidth ==> 可见区域宽度

document.body.clientHeight ==> 可见区域高度

document.documentElement.clientWidth ==> 0

document.documentElement.clientHeight ==> 0
 
在FireFox中:

document.body.clientWidth ==> 可见区域宽度

document.body.clientHeight ==> 可见区域高度

document.documentElement.clientWidth ==> 页面对象宽度(即BODY对象宽度加上Margin宽)

document.documentElement.clientHeight ==> 页面对象高度(即BODY对象高度加上Margin高)
 
在Opera中:

document.body.clientWidth ==> 可见区域宽度

document.body.clientHeight ==> 可见区域高度

document.documentElement.clientWidth ==> 页面对象宽度(即BODY对象宽度加上Margin宽)

document.documentElement.clientHeight ==> 页面对象高度(即BODY对象高度加上Margin高)

3.
附录 -
页面高宽的其他相关属性

<script>
var s = '';

s += "\r\n 网页可见区域宽:"+ document.body.clientWidth;

s += "\r\n 网页可见区域高:"+ document.body.clientHeight;

s += "\r\n 网页可见区域宽:"+ document.body.offsetWidth + " (包括边线和滚动条的宽)";

s += "\r\n 网页可见区域高:"+ document.body.offsetHeight + " (包括边线的宽)";

s += "\r\n 网页正文全文宽:"+ document.body.scrollWidth;

s += "\r\n 网页正文全文高:"+ document.body.scrollHeight;

s += "\r\n 网页被卷去的高(ff):"+
document.body.scrollTop;

s += "\r\n 网页被卷去的高(ie):"+
document.documentElement.scrollTop;

s += "\r\n 网页被卷去的左:"+ document.body.scrollLeft;

s += "\r\n 网页正文部分上:"+ window.screenTop;

s += "\r\n 网页正文部分左:"+ window.screenLeft;

s += "\r\n 屏幕分辨率的高:"+ window.screen.height;

s += "\r\n 屏幕分辨率的宽:"+ window.screen.width;

s += "\r\n 屏幕可用工作区高度:"+ window.screen.availHeight;

s += "\r\n 屏幕可用工作区宽度:"+ window.screen.availWidth;

s += "\r\n 你的屏幕设置是 "+ window.screen.colorDepth +"
位彩色";

s += "\r\n 你的屏幕设置 "+ window.screen.deviceXDPI +"
像素/英寸";

alert(s);
</script>
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐