offsetHeight, clientHeight與scrollHeight的區別

clientHeight

大部分瀏覽器對 clientHeight 都沒有什麼異議,都認爲是內容可視區域的高度,也就是說頁面瀏覽器中可以看到內容的這個區域的高度,即然是指可看到內容的區域,滾動條不算在內。但要注意padding是算在內。其計算方式爲clientHeight = topPadding + bottomPadding+ height - scrollbar.height。


offsetHeight

在IE6,IE7,IE8以及最新的的FF, Chrome中,在元素上都是offsetHeight = clientHeight + 滾動條 + 邊框。 


scrollHeight

scrollHeight是元素的padding加元素內容的高度。這個高度與滾動條無關,是內容的實際高度。

計算方式 :scrollHeight = topPadding + bottomPadding + 內容margix box的高度。

在瀏覽器中的區別在於:

IE6、IE7 認爲scrollHeight 是網頁內容實際高度,可以小於clientHeight。

FF、Chrome 認爲scrollHeight 是網頁內容高度,不過最小值是clientHeight。


注: 以上都是對於一般元素而方言的,body和documentElement的clientHeight, offsetHeight和scrollHeight在各個瀏覽器中的計算方式又不同。在所有的瀏覽器中,如果你想獲取整個視窗的高度,你得用documentElement.clientHeight,因爲body.clientHeight是由它的內容決定的。 關於body和documentElement的這些屬性,則完全是另外一回事:


FF19

在body上設置overflow:scroll就是設置瀏覽器的滾動條,導致body.clientHeight永遠都等於body.scrollHeight。


body

clientHeight:body.padding+ body.height(css設置或內容撐的);

offsetHeight:clientHeight+ body.border;

scrollHeight== clientHeight。


documentElement

clientHeight= window視窗高度 -scrollbar.width。

offsetHeight= body.offsetHeight  + body.margin。

scrollHeight= 內容的高度(與body的height樣式無關),但最小值是documentElement.clientHeight。


元素上

offsetHeight= padding + border + height。

clientHeight= padding + height - scrollbar.width。

scrollHeight>= clientHeight

從結果分析,FF認爲scrollHeight的最小高度是clientHeight。

offsetLeft = 元素border左上角到window視窗原點的距離 或 到offsetParent的borderbox頂部的距離。



Chrome

body

clientHeight= body.padding + body.height(css設置或內容撐大);

offsetHeight = body.clientHeight + body.border

scrollHeight= body.padding + 內容的高度(與height樣式無關),但最小值是documentElement.clientHeight。


documentElement

clientHeight= window視窗高度 – 滾動條高度。

offsetHeight  = body.offsetHeight + body.margin;

scrollHeight  = 內容的高度(與body上的height無關),但最小值是documentElement.offsetHeight。


元素上

offsetHeight = padding + border + height。

clientHeight = padding + height - scrollbar.width。

scrollHeight >= clientHeight



Safari 5

body

clientHeight= body.padding + body.height(CSS或內容撐的);

offsetHeight= clientHeight + border;

scrollHeight= body.padding + 內容的高度(與height樣式無關),但最小值是documentElement.clientHeight。


documentElement

clientHeight = window窗口大小 – 滾動條大小

offsetHeight = body.offsetHeight + body.margin

scrollHeight= 內容的高度(與body上的height無關),但最小值是documentElement.offsetHeight。



IE8

在IE8下,滾動條的大小是17個像素。


body

clientHeight= body.padding + body.height(css設置或內容撐大)

offsetHeight = clientHeight + body.border

scrollHeight =內容的高度(與body上的height無關),但最小值是clientHeight。


documentElement

clientHeight = 窗口大小(除去滾動條後)

offsetHeight = clientHeight + 滾動條大小 + body.border

scrollHeight=內容的高度(與body上的height無關),但最小值是body.offsetHeight+ margin。


元素上

offsetHeight = padding + border + height。

clientHeight = padding + height - scrollbar.width。

scrollHeight >= clientHeight

從結果分析,FF認爲scrollHeight的最小高度是clientHeight。

offsetLeft = 元素border左上角到畫布原點的距離 或 到offsetParent的borderbox頂部的距離。



IE7

在IE7中,body上設置的滾動條並不是窗口的滾動條,這個需要注意。


body

clientHeight= body.padding + height(css設置或內容撐大)– body上的滾動條。

offsetHeight= clientHeight + 滾動條的大小+body.border。

scrollHeight=   內容的高度(與body上的height無關)。


documentElement

clientHeight = window視窗大小(與滾動條的有無無關)

offsetHeight = clientHeight。

scrollHeight = body.offsetHeight + border.margin


元素

offsetHeight = padding + border + height。

clientHeight = padding + height - scrollbar.width。

scrollHeight = padding + 內容marginbox的高度

從結果分析,IE7認爲scrollHeight可以小於clientHeight。

offsetLeft = 元素border box左上角到父容器(不是offsetParent)的borderbox左上角的距離。



IE6

在IE6中,與IE7類似, body上設置的滾動條並不是窗口的滾動條。


body

clientHeight = body.padding + body.height

offsetHeight = body.clientHeight + body.border + body上滾動條大小。

scrollHeight =內容的高度(與body上的height無關)。


documentElement

在IE6中,與IE7類似,雖然body上設置的滾動條並不是窗口的滾動條,但它的clientHeight和offsetHeight還算與其它瀏覽器想統一。

clientHeight = 窗口大小(除去窗口滾動條大小後)

offsetHeight =clientHeight + body.border

scrollHeight = body.offsetHeight + body.margin


元素

offsetHeight = padding + border + height。

clientHeight = padding + height - scrollbar.width。

scrollHeight = padding + 內容margin box的高度

從結果分析,IE6認爲scrollHeight可以小於clientHeight。


 

同理

clientWidth、offsetWidth 和scrollWidth 的解釋與上面相同,只是把高度換成寬度即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章