Javascript HTML 中 style.width 和 offsetwidth 的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8133146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between style.width and offsetwidth in HTML?
提问by yeeen
I notice that offsetwidth
is a slightly bigger number. Similarly for style.height
and offsetheight
.
我注意到这offsetwidth
是一个稍大的数字。style.height
和类似offsetheight
。
采纳答案by Tim S.
Typically, an element's offsetWidth is a measurement which includes the element borders, the element horizontal padding, the element vertical scrollbar (if present, if rendered) and the element CSS width.
通常,元素的 offsetWidth 是一个度量,包括元素边框、元素水平填充、元素垂直滚动条(如果存在,如果呈现)和元素 CSS 宽度。
Source: https://developer.mozilla.org/en/DOM/element.offsetWidth
来源:https: //developer.mozilla.org/en/DOM/element.offsetWidth
So, it's the width of your element with border and padding included. Same thing for height.
因此,它是包含边框和填充的元素的宽度。身高也是一样。
回答by sol0mka
offsetWidth
returns computed element's width, while el.style.width
just returns width property defined in element.styleby javascript
and does not reflect real element's dimensions.
offsetWidth
返回计算元素的宽度,而el.style.width
只返回element.styleby 中定义的宽度属性javascript
,并不反映实际元素的尺寸。
This means that if you will try to get a width of the element by accessing el.style
, you will probably get nothing (sample
), even if the width was defined in your CSS. You will get the number only if it was defined in el.style
by javascript
.
这意味着,如果您尝试通过访问来获取元素的宽度el.style
,您可能什么也得不到 ( sample
),即使宽度是在您的 CSS 中定义的。只有在el.style
由javascript
.
Furthermore, offsetWidth
gives you real width of your element, including padding
and border
if you use content-box
css box model
which is default value for box-sizing
. So you can think about that like you set width
of the contents of the element and padding/border
go on top of that (sample
).
此外,offsetWidth
给你你的元素的实际宽度,包括padding
和border
如果你使用content-box
css box model
它是默认值box-sizing
。因此,您可以像设置width
元素的内容一样考虑它并padding/border
在其之上 ( sample
)。
If you are using border-boxcss box model
, you set the total width of the element, including padding
and border
make the content area smaller (sample
). So, in this case, offsetWidth
and el.style.width
would return exactly the same numbers (if el.style.width
was previously set by javascript
).
如果您使用border-boxcss box model
,则设置元素的总宽度,包括padding
并border
缩小内容区域 ( sample
)。所以,在这种情况下,offsetWidth
并且el.style.width
将返回完全相同的号码(如果el.style.width
之前被设定javascript
)。
回答by LDS
offsetWidth is a measurement in pixels of the element's CSS width, including any borders, padding, and vertical scrollbars.
offsetWidth 是以像素为单位的元素 CSS 宽度的度量,包括任何边框、填充和垂直滚动条。
clientWidth is the inner width (ie. the space inside an element including padding but excluding borders and scrollbars)
clientWidth 是内部宽度(即元素内的空间,包括填充但不包括边框和滚动条)
with only return the css with defined
仅返回已定义的 css