【相關(guān)學習推薦:javascript學習教程】
HTML
尺寸
所謂元素的HTML尺寸,就是指在HTML
標簽中設(shè)置的尺寸樣式。
例如:
<p class="box" style="width: 200px; height: 200px;"></p>復(fù)制代碼
頁面效果如下圖所示:

這種尺寸可以通過elem.style.width
或elem.style.height
獲取
例如:
let box = document.querySelector('.box');console.log(box.style.width); // 200pxconsole.log(box.style.heihgt); // 200px復(fù)制代碼
但是對于CSS
尺寸,它們是無法獲取的。
例如:
.box { width: 200px; height: 200px; background: lightpink; }復(fù)制代碼
如下圖所示:

為了讓樣式和結(jié)構(gòu)分離,我們會將樣式單獨寫進CSS
文件中,如果上述方法無法獲取元素的尺寸,那我們該通過什么方法如何獲取呢?
接著往下看。
在JavaScript
的element
對象中,提供了三種只讀屬性,可以用于獲取元素的尺寸。
它們分別是:
offsetHeight
和offsetWidth
clientHeight
和clientWidth
scrollHeight
和scrollWidth
我們先從第一種開始
offsetHeight
和offsetWidth
offsetHeight
用于獲取元素的真實高度(border-box
),它包含該元素的垂直內(nèi)邊距和邊框,如果有水平滾動條的話(水平滾動條高度為17px
,一般會被計入內(nèi)容高度height
中),還需要加上水平滾動條的高度。
offsetWidth
用于獲取元素的真實寬度(border-box
),它包含該元素的水平內(nèi)邊距和邊框,如果有垂直滾動條的話(水平滾動條高度為17px
,一般會被計入內(nèi)容寬度width
中),還需要加上垂直滾動條的寬度。
沒有滾動條時
一個p
元素有如下樣式
.box { margin: 10px auto; padding: 10px; width: 200px; height: 200px; border: 1px solid #000; background: lightpink; }復(fù)制代碼
頁面效果如下:

其盒模型如下所示:

由于offsetHeight
獲取的是元素的真實高度,那么其高度為height
+ padding * 2
+ border * 2
,即200px
+10px * 2
+1px * 2
,為222px
offsetWidth
獲取的是元素的真實寬度,那么其寬度為width
+padding * 2
+border * 2
,即200px
+10px * 2
+1px * 2
,為222px
let box = document.querySelector('.box');let height = box.offsetHeight;let width = box.offsetWidth;console.log(height); // 222pxconsoel.log(width); // 222px復(fù)制代碼
含有滾動條時
當含有滾動條時,由于水平滾動條的高度為17px
,一般會被計入內(nèi)容高度height
中,即內(nèi)容高度的實際值要比設(shè)置的值要少17px
。
兩個p
為父子關(guān)系,它們有如下樣式:
.father { margin: 10px auto; padding: 10px; width: 200px; height: 200px; border: 1px solid; background: lightsalmon; /* 滾動條高度和寬度被計算到content中 */ overflow: auto; }.son { width: 220px; height: 220px; background: plum; }復(fù)制代碼
頁面效果如下:

其盒模型如下所示:

可以看到,內(nèi)容區(qū)域的寬度實際有效值為183px
,是設(shè)置的width
值減去了垂直滾動條的寬度17px
后的值。內(nèi)容區(qū)域的高度亦是如此。
但當有滾動條時,由于offsetHeight
和offsetWidth
的值除了內(nèi)邊距和邊框值外,還需要包含滾動條的高度和寬度。雖然滾動條占據(jù)了內(nèi)容區(qū)域的width
和height
部分空間,但是,最終計算時,又加上了。
所以真實寬度還是相當于原來設(shè)置的width
+ padding * 2
+ border * 2
,即200px + 10px * 2
+1px * 2
,為222px
。高度亦然。
let f_box = document.querySelector('.father');let f_height = f_box.offsetHeight;let f_width = f_box.offsetWidth;console.log(f_height); // 220pxconsole.log(f_width); // 220px復(fù)制代碼
clientHeight
和clientWidth
clientHeight
和clientWidth
表示可視區(qū)域的高度和寬度,包括元素內(nèi)容本身的寬度和高度以及padding
。但是,如果有滾動條的話,需要減去滾動條的寬度和高度。
沒有滾動條時
一個p
有如下樣式:
.box { margin: 10px auto; padding: 10px; width: 200px; height: 200px; border: 1px solid #000; background: lightpink; }復(fù)制代碼
頁面效果如下:

其盒模型如下:

該元素的clientHeight
為width
+padding * 2
,即200px
+10px * 2
,為220px
,高度亦然。
let box = document.querySelector('.box');let height = box.clientHeight;let width = box.clientWidth;console.log(height); // 220pxconsoel.log(width); // 220px復(fù)制代碼
含有滾動條時
當含有滾動條時,需要減去滾動條的寬度和高度。
父子p
有如下樣式:
.father { margin: 10px auto; padding: 10px; width: 200px; height: 200px; border: 1px solid; background: lightsalmon; /* 滾動條高度和寬度被計算到content中 */ overflow: auto; }.son { width: 220px; height: 220px; background: plum; }復(fù)制代碼
頁面效果如下:

其盒模型如下:

那么,clientWidth
的值為width
+padding * 2
–17px
,即200px
+10px * 2
–17px
,為203px
所謂可視區(qū)域,就是我們最終能看到的部分。就像下圖一樣,原來的元素如果沒有滾動條,它的尺寸應(yīng)該是紅色框線所包裹的尺寸。

但是,由于多了滾動條,可視區(qū)域就減小了,如下所示。在原有尺寸基礎(chǔ)上減去滾動條的寬度和高度就是可視區(qū)域的寬度和高度了。

scrollHeight
與scrollWidth
scrollHeight
用于獲取一個元素的內(nèi)容高度,包括溢出的部分。scrollWidth
用于獲取一個元素的內(nèi)容寬度,包括溢出的部分。當然,在沒有溢出,即沒有滾動條的情況下,這兩個值等同于clientHeight
和clientWidth
,也是包括元素本身的尺寸以及padding
,但不包括border
和margin
父子p
有如下樣式:
.father { margin: 10px auto; padding: 10px; /* 父元素的內(nèi)容寬度:320px + 10px = 330px */ width: 200px; /* 父元素的內(nèi)容高度:200px - 17px = 203px */ height: 200px; border: 1px solid #000; overflow: auto; }.son { padding: 10px; /* 子元素的真實寬度:300px + 10px * 2 = 320px */ width: 300px; height: 100px; background: plum; }復(fù)制代碼
頁面效果如下:

由于子元素的高度只有100px
,沒有發(fā)生溢出,因此,父元素的scrollHeight
就等同于clientHeight
:width
+padding
-水平滾動條高度17px
,即200px
+10px*2
–17px
=203px
子元素真實占據(jù)的寬度有300px
+10px*2
= 320px
,外加父元素設(shè)置的左側(cè)內(nèi)邊距還是10px
,右側(cè)內(nèi)邊距失效。因此父元素的scrollWidth
的值為320px
+10px
,為330px
let f_box = document.querySelector('.father');let height = f_box.scrollHeight;let width = f_box.scrollWidth;console.log(height); // 203pxconsole.log(width); // 330px復(fù)制代碼
父元素設(shè)置overflow
造成右內(nèi)邊距失效的問題
關(guān)于父元素設(shè)置overflow: auto
時,造成的右內(nèi)邊距失效,有以下圖片可以佐證。

如上所示:父元素的左側(cè)和頂部都有10px
的內(nèi)邊距,但是右側(cè)就沒有。

如上所示:因為子元素沒有設(shè)置overflow
,所以可以看到子元素的右內(nèi)邊距依然是生效的。
當子元素的寬度大于父元素的寬度時,子元素的margin-right
或者父元素的padding-right
是被計算為0
的。這里不具體展開。
想了解