最近須要清除區分開元素的width,height及相應的座標等,當前這篇用來區分offsetWidth
clientWidth
scrollWidth
的區別css
假設有一個元素,width有如下幾個進行組合html
clientWidth = content + padding-left + padding-rightcode
offsetWidth = content + padding-left + padding-right + border-left + border-right + scrollbarhtm
scrollWidth = content + padding-left + padding-right + scrollbar + border-left + border-right +滾動進入不可見的內容blog
在上述中,咱們能夠計算出
scrollbar
爲get
const scrollbar = el.offsetWidth - (border-left + border-right) - clientWidth
htmlio
<section class="client-xyz"> <p>我是很長很長的內容我是很長很長的內容我是很長很長的內容我是很長很長的內容我是很長很長的內容我是很長很長的內容</p> </section>
cssclass
p { margin: 20px; padding: 20px; /* border: 30px solid #333; */ /* border: 10vw solid #333; */ /* border: calc(100px - 70px) solid #333; */ border: 30px solid #333; word-break: keep-all; overflow-y: scroll; }
jscli
const Box = document.querySelector('p') let border = 0 // 獲取元素的style信息 const style = window.getComputedStyle(Box, null) // border無論設置的單位如何,最終都會轉爲 px border = parseFloat(style['borderRightWidth'].replace('px', '')) + parseFloat(style['borderLeftWidth'].replace('px', '')) const scollbar = Box.offsetWidth - Box.clientWidth - border
el.offsetWidth - el.clientWidth
而是還須要減去border
的寬度