document.documentElement.clientWidth
获取浏览器窗口文档显示区域的宽度,不包括滚动条。
document.documentElement.clientHeight
获取浏览器窗口文档显示区域的高度,不包括滚动条。
浏览器兼容性
所有浏览器解释都一样。
document.documentElement.offsetWidth
获取DOM文档的根节点html元素对象的宽度,即offsetWidth=width+padding+border,不包括margin。
document.documentElement.offsetHeight
获取DOM文档的根节点html元素对象的高度,即offsetHeight=height+padding+border,不包括margin。
浏览器兼容性
- 在IE9、10中,offsetWidth和offsetHeight指的是浏览器窗口文档显示区域的宽度和高度,包括滚动条。
- 在IE8中,offsetWidth和offsetHeight指的是浏览器窗口文档显示区域的宽度和高度,包括滚动条和文档显示区域边缘2px的灰色边框。
- 在IE7中,offsetWidth和offsetHeight的值等于clientWidth和clientHeight,即不包括滚动条和文档显示区域边缘2px的灰色边框。
document.documentElement.scrollWidth
获取html元素对象内容的实际宽度,即html元素对象的滚动宽度。
document.documentElement.scrollHeight
获取html元素对象内容的实际高度,即html元素对象的滚动高度。
浏览器兼容性
- 在FireFox、IE8、IE9和IE10中,scrollWidth和scrollHeight指的是整个页面文档的滚动宽度和高度。但是在IE8、9、10中,如果给html元素设置margin,则上下左右都有margin;而在Chrome、Safari、Opera、FireFox中,margin-right和margin-bottom是没有的。所以在IE8、9、10中,如果html元素上下左右都有margin,scrollWidth和scrollHeight的值要大一些。
- 在IE7中,scrollWidth的值=body的width+body的padding+body的border+body的margin+html的padding+html的border+html的margin-left。同理可得scrollHeight的值。下图中用红框框出了scrollWidth和scrollHeight的范围。左图是上半部分,右图是下半部分。
document.documentElement.clientLeft
获取html元素对象的左边框的宽度。
document.documentElement.clientTop
获取html元素对象的上边框的宽度。
浏览器兼容性
- 在FireFox中,clientLeft和clientTop的值永远为0。
- 在IE7中,clientLeft和clientTop的值永远为2。
document.doucmentElement.offsetLeft
获取html元素对象相对于整个页面文档的位置,也就是html元素的margin。
document.documentElement.offsetTop
获取html元素对象相对于整个页面文档的位置,也就是html元素的margin。
浏览器兼容性
- 在FireFox中,offsetLeft和offsetTop的值就是负的html元素的border-width。
- 在IE中,offsetLeft和offsetTop的值始终为0。
document.documentElement.scrollLeft
设置或获取页面文档向右滚动过的像素数。
document.documentElement.scrollTop
设置或获取页面文档向下滚动过的像素数。
浏览器兼容性
- 在Chrome、Opera、Safari中,scrollLeft和scrollTop的值始终为0,其使用document.body.scrollLeft/scrollTop发挥同样的作用。
- 在FireFox和IE中,正常。
本文所用到的测试代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>鬼眼邪神的博客</title>
<style>
*{
margin:0;
padding:0;
}
html {
margin:20px;
padding:20px;
width:600px;
border:10px solid #000;
}
body {
margin:0 0 0 100px;
width:400px;
height:800px;
border:5px solid #000;
background:yellow;
overflow:scroll;
}
.green {
position:relative;
margin:50px auto;
padding:20px;
width:80px;
height:80px;
border:10px solid #000;
background:rgb(0,255,0);
}
.con {
margin:0 auto;
width:380px;
}
</style>
<script>
(function(){
window.οnlοad=function (){
var con=document.getElementById("con");
var green=document.getElementById("green");
var body=document.getElementById("body");
document.οnclick=function (event){
var event=window.event||event;
con.innerHTML=
"document.documentElement.clientWidth="+document.documentElement.clientWidth+","+
"document.documentElement.clientHeight="+document.documentElement.clientHeight+"<br/>"+
"document.documentElement.offsetWidth="+document.documentElement.offsetWidth+","+
"document.documentElement.offsetHeight="+document.documentElement.offsetHeight+"<br/>"+
"document.documentElement.scrollWidth="+document.documentElement.scrollWidth+","+
"document.documentElement.scrollHeight="+document.documentElement.scrollHeight+"<br/>"+
"document.documentElement.clientLeft="+document.documentElement.clientLeft+","+
"document.documentElement.clientTop="+document.documentElement.clientTop+"<br/>"+
"document.documentElement.offsetLeft="+document.documentElement.offsetLeft+","+
"document.documentElement.offsetTop="+document.documentElement.offsetTop+"<br/>"+
"document.documentElement.scrollLeft="+document.documentElement.scrollLeft+","+
"document.documentElement.scrollTop="+document.documentElement.scrollTop+"<br/>";
}
}
})();
</script>
</head>
<body>
<div class="green" id="green">
<div class="red"></div>
</div>
<div class="con" id="con"></div>
</body>
</html>