top、postop、scrolltop、scrollHeight、offsetHeight
網頁可見區域寬: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" οnfοcus="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" οnfοcus="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。
轉載于:https://www.cnblogs.com/aaa6818162/archive/2009/10/30/1592855.html
總結
以上是生活随笔為你收集整理的top、postop、scrolltop、scrollHeight、offsetHeight的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 背景和弹出 Panel 都带有动画效果的
- 下一篇: linux-03-Vim使用+账号用户管