我們總結下與HTML元素的樣式息息相關的屬性。
偏移量
元素的可見大小由寬度高度決定,其中還要包括內邊距、滾動條、邊寬大?。ú话ㄍ膺吘啵?,通過下面四個屬性可以獲得。
offsetWidth、offsetHeigh、offsetLeft、offsetTop
offsetHeight/offsetWidth: 表述元素的外尺寸:
元素內容 + 內邊距 + 邊框(不包括外邊距),給出元素在頁面中占據的寬度和高度的總計。
注意:把元素的邊框和滾動條計算在內。
offsetWidth = border-left-width + padding-left + width + padding-right + border-right-width; offsetHeight = border-top-width + padding-top + height + padding-bottom + border-bottom-width;
包含關系:
offsetLeft/offsetTop: 表示該元素的左上角(邊框外邊緣)與已定位的父容器(offsetParent對象)左上角的距離。
offsetParent元素是指元素最近的定位(relative,absolute)祖先元素,可遞歸上溯。
客戶區域大小
clientWidth、clientHeight
clientWidth/clientHeight: 用于描述元素的內尺寸:元素內容 + 兩邊內邊距。
clientWidth = width+padding(left、right) clientHeight = height+padding(top、bottom)
滾動大小
scrollWidth、scrollHeight、scrollLeft、scrollTop
scrollHeight/scrollWidth: 元素內容的總高度或寬度
scrollLeft/scrollTop:是指元素滾動條位置,它們是可寫的(被隱藏的內容區域左側/上方的像素)
scrollHeight:是元素的padding加元素內容的高度。這個高度與滾動條無關,是內容的實際高度。
計算方式 :scrollHeight = topPadding + bottomPadding + 內容margix box的高度。
在瀏覽器中的區別在于:
IE6、IE7 認為scrollHeight 是網頁內容實際高度,可以小于clientHeight。
FF、Chrome 認為scrollHeight 是網頁內容高度,不過最小值是clientHeight。
瀏覽器窗口的滾動條位置:window對象的 pageXoffset 和 pageYoffset , IE 8及更早版本可以通過scrollLeft和scrollTop屬性獲得滾動條位置。
請驗證,完成請求
由于請求次數過多,請先驗證,完成再次請求
打開微信掃碼自動綁定
綁定后可得到
使用 Ctrl+D 可將課程添加到書簽
舉報