Javascript:scrollWidth,clientWidth,offsetWidth的區別

來源:互聯網
上載者:User

網頁可見地區寬: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高)

 

原文地址:http://thinktank.iteye.com/blog/354667

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.