令人頭疼的clientTop、scrollTop、offsetTop


1、網絡上流傳的圖片

2、稍微容易理解點的示意圖

參考鏈接:http://blog.csdn.net/lidiansheng/article/details/7950751

3、言簡意賅的示意圖

4、最完善的一張圖!!!

5、文字總結

1. 基本概念

  • offsetWidth/offsetHeight

對象的可見寬度

  • clientWidth/clientHeight

內容的可見寬度

  • scrollWidth/scrollHeight

元素完整的高度和寬度,overflow:hidden的部分也計算在內。

  • offsetLeft/offsetTop

當前元素距瀏覽器邊界的偏移量,以像素為單位。

  • clientTop/clientLeft

這個屬性測試下來的結果就是border。

  • scrollLeft/scrollTop

設置或返回已經滾動到元素的左邊界或上邊界的像素數。

2. 推斷計算

     等式①:內容寬度clientWidth=元素寬度elementWidth+內邊距padding-滾動條的寬度(如果有滾動條)(不考慮邊界border)

                  比如下方例子中:clientWidth=300+20-17=303

     等式②:可見寬度offsetWidth=元素寬度elementWidth+內邊距padding+邊界border(滾動條包含在邊界內部了,沒有產生額外距離,不用計算)

比如下方例子中:offsetWidth=300+20+16=336

3.xxxTop區別  

等式③:clientTop=border-top屬性值(‘-’不是減號,是連字符)

等式④:offsetTop=元素距上邊界高度+margin

比如下方例子中:offsetTop=150+15=165

scrollTop:元素的滾動值 (可用來做滾動效果)

4、采用scrollTop做滾動效果

 1 <html>
 2 <head>
 3     <title>測試滾動效果</title>
 4     <meta charset="utf-8">
 5     <style type="text/css">
 6         #viewBox
 7         {
 8             width: 500px;
 9             overflow: hidden;
10             border: 1px solid pink;
11         }
12 
13         #scrollBox
14         {
15             float: left;
16             width: 2500px; /*必須足夠大才能放下所有滾動內容*/
17         }
18 
19         #A1, #A2, ul li
20         {
21             float: left;
22             list-style: none;
23         }
24     </style>
25     <script type="text/javascript">
26         var viewBox, As1, As2, Atimer;
27         function init() {
28             viewBox = getid("viewBox");
29             As1 = getid("A1");
30             As2 = getid("A2");
31             As2.innerHTML = As1.innerHTML; //復制一份相同的放在ul后面做銜接
32             Atimer = setInterval(Amar, 20);
33         }
34         function Amar() {
35             if (As1.offsetWidth <= viewBox.scrollLeft) {
36                 viewBox.scrollLeft -= As1.offsetWidth;
37             } else {
38                 viewBox.scrollLeft++;
39             }
40         }
41         function getid(id) {
42             return document.getElementById(id);
43         }
44         window.onload = init;
45     </script>
46 
47 </head>
48 <body>
49 
50     <div id="viewBox" onmouseover="clearInterval(Atimer)" onmouseout="Atimer=setInterval(Amar,20)">
51         <div id="scrollBox">
52             <ul id="A1">
53                 <li><a href="#">公告1</a></li>
54                 <li><a href="#">公告2</a></li>
55                 <li><a href="#">公告3</a></li>
56             </ul>
57             <!-- 用來做無縫銜接 -->
58             <ul id="A2"></ul>
59         </div>
60     </div>
61 </body>
62 </html>

顯示效果:

6、一個小例子

 1 <html>
 2 <head>
 3     <title>測試</title>
 4     <meta charset="utf-8">
 5     <style type="text/css">
 6         *
 7         {
 8             margin: 0px;
 9             padding: 0px;
10         }
11 
12         #container
13         {
14             width: 300px;
15             height: 200px;
16             position: absolute;
17             left: 200px;
18             top: 150px;
19             margin: 15px;
20             padding: 10px;
21             overflow: auto;
22             background-color: #555;
23             border: 8px solid green;
24         }
25 
26             #container p
27             {
28                 background-color: pink;
29                 width: 200px;
30                 height: 500px;
31             }
32     </style>
33 </head>
34 <body>
35     <div id="container">
36         <p>
37             文字內容
38         </p>
39     </div>
40     <script type="text/javascript">
41         /*在Chrome或Firefox下查看輸出*/
42         console.log('元素內樣式屬性(不指定則為空)style.top →→→→ ' + container.style.top)
43         console.log('元素內樣式屬性(不指定則為空)style.left →→→→ ' + container.style.left)
44         console.log('元素內樣式屬性(不指定則為空)style.width →→→→ ' + container.style.width)
45         console.log('元素內樣式屬性(不指定則為空)style.height →→→→ ' + container.style.height)
46 
47         console.log('可見區域上邊框(border屬性指定) clientTop →→→→ ' + container.clientTop)
48         console.log('可見區域左邊框(border屬性指定) clientLeft →→→→ ' + container.clientLeft)
49 
50         console.log('內容區域寬度(包括padding 20px,不包括滾動條17px,即300+20-17=303) clientWidth →→→→ ' + container.clientWidth)
51         console.log('可見區域寬度(包括padding 20px 和border 16px 滾動條在其內部,沒有產生額外長度)offsetWidth →→→→ ' + container.offsetWidth)
52         console.log('內容區域高度(包括padding 20px) clientHeight →→→→ ' + container.clientHeight)
53         console.log('可見區域高度度(包括padding 20px和border 16px 滾動條在其內部,沒有產生額外長度) offsetHeight →→→→ ' + container.offsetHeight)
54 
55         console.log('與上層或外層偏移(包括margin 15px) offsetTop →→→→ ' + container.offsetTop)
56         console.log('與左層或外層偏移(包括margin 15px)  offsetLeft →→→→ ' + container.offsetLeft)
57 
58         console.log('已經滾動的距離(只有出現滾動條才有效,否則均為0)scrollTop →→→→ ' + container.scrollTop)
59         console.log('已經滾動的距離(只有出現滾動條才有效,否則均為0)scrollLeft →→→→ ' + container.scrollLeft)
60         console.log('滾動條最大滾動距離(即隱藏的部分的高度) scrollTopMax →→→→ ' + container.scrollTopMax)
61         console.log('滾動對象的完整寬度(至少是元素寬度) scrollWidth →→→→ ' + container.scrollWidth)
62         console.log('滾動對象的完整高度(至少是元素高度) scrollHeight →→→→ ' + container.scrollHeight)
63 
64         console.log('screen.top →→→→ ' + window.screen.top);
65         console.log('screen.left →→→→ ' + window.screen.left);
66         console.log('screen.height →→→→ ' + window.screen.height);
67         console.log('screen.width →→→→ ' + window.screen.width);
68         console.log('screen.availHeight →→→→ ' + window.screen.availHeight);
69         console.log('screen.availWidth →→→→ ' + window.screen.availWidth);
70     </script>
71 </body>
72 </html>

界面顯示:

后台輸出:

 1 "元素內樣式屬性(不指定則為空)style.top →→→→ " 
 2 "元素內樣式屬性(不指定則為空)style.left →→→→ " 
 3 "元素內樣式屬性(不指定則為空)style.width →→→→ " 
 4 "元素內樣式屬性(不指定則為空)style.height →→→→ " 
5 "可見區域上邊框(border屬性指定) clientTop →→→→ 8" /*就是border-top寬度*/ 6 "可見區域左邊框(border屬性指定) clientLeft →→→→ 8" /*就是border-left寬度*/
7 "內容區域寬度(包括padding 20px,不包括滾動條17px,即300+20-17=303) clientWidth →→→→ 303" /*參見等式①*/ 8 "可見區域寬度(包括padding 20px 和border 16px 滾動條在其內部,沒有產生額外長度)offsetWidth →→→→ 336" /*300+20+16=336,參見等式②*/ 9 "內容區域高度(包括padding 20px) clientHeight →→→→ 220" /*元素200+padding20-滾動條0=220*/ 10 "可見區域高度度(包括padding 20px和border 16px 滾動條在其內部,沒有產生額外長度) offsetHeight →→→→ 236" /*200+20+16=236,類似等式②*/
11 "與上層或外層偏移(包括margin 15px) offsetTop →→→→ 165" /*150+15(margin-top),參見等式④*/
12
"與左層或外層偏移(包括margin 15px) offsetLeft →→→→ 215"/*200+15(margin-left),類似等式④*/
13 "已經滾動的距離(只有出現滾動條才有效,否則均為0)scrollTop →→→→ 0" 14 "已經滾動的距離(只有出現滾動條才有效,否則均為0)scrollLeft →→→→ 0" 15 "滾動條最大滾動距離(即隱藏的部分的高度scrollTopMax →→→→ 290" 16 "滾動對象的完整寬度(至少是元素寬度) scrollWidth →→→→ 303" 17 "滾動對象的完整高度(至少是元素高度) scrollHeight →→→→ 510" 18 "screen.top →→→→ 0" 19 "screen.left →→→→ 0" 20 "screen.height →→→→ 900" 21 "screen.width →→→→ 1600" 22 "screen.availHeight →→→→ 860" 23 "screen.availWidth →→→→ 1600"

 隱藏部分的高度scrollTopMax:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM