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