JS:document.documentElement物件的clientWidth、offsetWidth、scrollWidth、clientLeft、offsetLeft、scrollLeft
阿新 • • 發佈:2019-02-09
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.onload=function (){
var con=document.getElementById("con");
var green=document.getElementById("green");
var body=document.getElementById("body");
document.onclick=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>