Javascript:scrollWidth,clientWidth,offsetWidth的區別

網頁可見區域寬:document.body.clientWidth;
網頁可見區域高:document.body.clientHeight;
網頁可見區域高:document.body.offsetWeight:
網頁可見區域高:document.body.offsetHeight;
網頁正文全文寬:document.body.scrollWidth;
網頁正文全文高:document.body.scrollHeight;
網頁被捲去的高:document.body.scrollTop;
網頁被捲去的左:document.body.scrollLeft;
網頁正文部分上:window.screenTop;
網頁正文部分左:window.screenLeft;
屏幕分辨率的高:window.screen.height;
屏幕分辨率的寬:window.screen.width;
屏幕可用工作區高度:window.screen.availHeight;
屏幕可用工作區寬度:window.screen.availWidth;

scrollWidth
是對象的實際內容的寬,不包邊線寬度,會隨對象中內容的多少改變(內容多了可能會改變對象的實際寬度)

clientWidth
是對象可見的寬度,不包滾動條等邊線,會隨窗口的顯示大小改變。

offsetWidth
是對象的可見寬度,包滾動條等邊線,會隨窗口的顯示大小改變。


------------------------------------------------
一個scrollWidth和clientWidth的例子:
<html>
<head>
<title>77.htm文件</title>
</head>

<body>
<textarea wrap="off" onfocus="alert('scrollWidth:'+this.scrollWidth+'\n clientWidth:'+this.clientWidth);"></textarea>
</body>
</html>
在文本框內輸入內容,當橫向滾動條沒出來前scrollWidth和clientWidth的值是一樣的。
當一行內容超出文本框的寬度,就有橫向滾動條出來了,scrollWidth的值就變了。
scrollWidth是對象實際內容的寬度。
clientWidth是對象看到的寬度(不含邊線),這個例子裏不會改變。


-----------------------------------------------
一個clientWidth和offsetWidth的例子:
<html>
<head>
<title>77.htm文件</title>
</head>

<body>
<textarea wrap="off" onfocus="alert('offsetWidth:'+this.offsetWidth+'\n clientWidth:'+this.clientWidth);"></textarea>
</body>
</html>

offsetWidth的值總是比clientWidth的值打
clientWidth是對象看到的寬度(不含邊線)
offsetWidth是對象看到的寬度(含邊線,如滾動條的佔用的寬)

top、postop、scrolltop、scrollHeight、offsetHeight

1. top

此屬性僅僅在對象的定位(position)屬性被設置時可用。否則,此屬性設置會被忽略。

<div style="background-color:red; position:absolute; width:100px; height:100px;">

<p style="background-color:silver; position:absolute; top:-5px;">測試top</p>

</div>


上面是一個段落P包含在一個DIV內,可以看到P的top設置爲-5px後,它的上邊距超過了容器DIV的上邊距,超過的這段距離就是設置的5px。

需要注意的是,DIV和P這一對包含元素,都需要設置position爲absolute才能得到想要的結果,假如父元素不設置,則子元素的參照將是更上層定義過position的元素,直到整個文檔;

2. posTop

posTop的數值其實和top是一樣的,但區別在於,top固定了元素單位爲px,而posTop只是一個數值(這一點可以通過alert("top="+id.style.top)和alert("posTop="+id.style.posTop)來證明),因此一般使用posTop來進行運算。

<div style="background-color:red; position:absolute; width:100px; height:100px;">

<p id="test" style="background-color:silver; position:absolute;">測試posTop</p>

</div>

<script>
test.style.posTop = 15+8;
alert("top="+test.style.top);
alert("posTop="+test.style.posTop);
</script>

無論你使用top或posTop來賦值,最後的結果都是一致的

3. scrollTop

<div id="container" style="background-color:silver; width:100px; height:100px; overflow:auto;">
<p style="background-color:red;">
別再做情人 做只貓 做只狗 不做情人 做只寵物至少可愛迷人 和你相交不淺無謂明日會被你憎</p>
</div>

<script>
container.scrollTop = 12;
</script>


這一段文本在這個100*100的DIV內無法完全顯示,所以設置了overflow爲auto,它會出現一個上下方向的滑動框,假如沒有設置id.scrollTop屬性的話,默認情況下滑塊位置在頂端。而設置了scrollTop值爲12後,滑塊的位置改變了,默認顯示是捲過了12個象素的文本。如果設置overflow爲hidden,則將會無法顯示頂部12個象素的文本。

注意設置方式是id.scrollTop,而不是id.style.scrollTop。

4. scrollHeight 與 offsetHeight

offsetHeight是自身元素的高度,scrollHeight是 自身元素的高度+隱藏元素的高度。

<div id="container" style="background-color:silver; width:100px; height:100px; overflow:auto;">
<p style="background-color:red; height:250px; ">
別再做情人 做只貓 做只狗 不做情人 做只寵物至少可愛迷人 和你相交不淺無謂明日會被你憎</p>
</div>

<script>
alert(container.offsetHeight);
alert(container.scrollHeight);
</script>


將依次輸出100,250。因爲已經指定了元素的height爲100px,所以offsetHeight始終爲100px;內部元素爲250px,而容器元素只有100px,那麼還有150px的內容它無法顯示出來,但它卻是實際存在的,所以scrollHeight值爲100+150=250。


另外document.body.clientWidth和document.documentElement.clientWidth有如下區別:


如果在頁面中添加W3C標準標記:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">


在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高)


而如果沒有定義W3C的標準,則
IE爲:
document.documentElement.clientWidth ==> 0
document.documentElement.clientHeight ==> 0
FireFox爲:
document.documentElement.clientWidth ==> 頁面對象寬度(即BODY對象寬度加上Margin寬)

document.documentElement.clientHeight ==> 頁面對象高度(即BODY對象高度加上Margin高)
Opera爲:
document.documentElement.clientWidth ==> 頁面對象寬度(即BODY對象寬度加上Margin寬)

document.documentElement.clientHeight ==> 頁面對象高度(即BODY對象高度加上Margin高)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章