DOM元素尺寸和位置

2022-08-26 19:42:12 字數 989 閱讀 5968

offsetwidth和offsetheight:可以獲取元素實際大小(width+padding+border)。

可以獲取元素的大小,但是不能設定。

box.offsetwidth;

box.offsetheight;

offsettop和offsetleft

可以獲取當前元素相對于父元素的位置。

box.offsetleft;

box.offsetheight;

ps:如果父盒子中沒有設定相對定位的,那麼元素就相對於視窗定邊的距離

scrollwidth和scrollheight

可以獲取滾動內容的元素大小

box.scrollwidth;

box.scrollheight;

scrolltop和scrollleft

獲取頁面被滾動條卷去的高度和左側的寬度

box.scrolltop;

box.scrollleft;

clientwidth和clientheight

這組屬性可以獲取元素可視區的大小,可以得到元素內容及內邊距所佔據的空間大小。(width+padding)

box.clientwidth;

box.clientheight;

clientleft和clienttop(完全沒用):

就是bordertop與borderleft

三個家族的區別

如果內容沒有超出盒子範圍:clientwidth與scrollwidth相同

DOM尺寸,DOM元素位置

dom尺寸 doctype html en utf 8 viewport content width device width,initial scale 1.0 document title utils.js script box style head box div 以乙個方向為例,每一種都有寬...

22 DOM元素尺寸和位置

1.通過 style內聯獲取元素的大小var box document.getelementbyid box 獲取元素 box.style.width 200px 空 box.style.height 200px 空ps style 獲取只能獲取到行內 style 屬性的 css 樣式中的寬和高,如...

DOM元素尺寸

offsetleft 獲取的是相對于父物件的左邊距,相當於margin left,從本元素的最外面border到父元素的最內border left 獲取或設定相對於 具有定位屬性 position定義為relative 的父物件 的左邊距 如果父div的position定義為relative,子di...