scrollWidth,offsetWidth,clientWidth的本质区别

8 篇文章 0 订阅
scrollWidth,offsetWidth,clientWidth的本质区别

1).element.scrollWidth (只读)

总结
scrollWidth是指元素内容的宽度或元素本身的宽度(元素本身的宽度比元素内容的宽度更大).若元素比它的内容更宽,如出现水平滚动条时),scrollWidth > clientWidth.
scrollWidth is a read–only property that returns either the width in pixels of the content of an element or the width of the element itself, whichever is greater. If the element is wider than its content area (for example, if there are scroll bars for scrolling through the content), the scrollWidth is larger than the clientWidth.

语法
var xScrollWidth = element.scrollWidth;

例子
<div id="aDiv" style="width: 100px; height: 200px; overflow: auto;">-FooBar-FooBar-FooBar</div>
<br />
<input type="button" value="Show scrollWidth" οnclick="alert(document.getElementById('aDiv').scrollWidth);">

详述
The CSSOM View Module defines scrollWidth

2).element.offsetWidth (只读)

总结
返回元素的布局宽度(the layout width of an element)
Returns the layout width of an element.

语法
var offsetWidth = element.offsetWidth;

描述
一个元素的offsetWidth包含元素的borders,元素的水平padding,若出现垂直滚动条,则也包含元素的垂直滚动条的宽度,以及元素的样式width.
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.

例子


padding-top

Gentle, individualistic and very loyal, Birman cats fall between Siamese and Persian in character. If you admire cats that are non aggressive, that enjoy being with humans and tend to be on the quiet side, you may well find that Birman cats are just the felines for you.

Image:BirmanCat.jpgAll Birmans have colorpointed features, dark coloration of the face, ears, legs and tail.

Cat image and text coming fromwww.best-cat-art.com

padding-bottom

Left Top Right Bottom margin-top margin-bottom border-top border-bottom

Image:offsetWidth.png

详述
offsetWidth是MSIE的DHTML模型的一部分,并不是W3C草案或技术推荐.

3).element.clientWidth (只读)

总结
clientWidth是元素的内宽度,它包括元素的内边距,但是不包括垂直滚动条的宽度(即使出现滚动条), 也不包括元素的边框和外边距.
clientWidth is the inner width of an element in pixels. It includes padding but not the vertical scrollbar (if present, if rendered), border or margin.

语法
var intElemClientWidth = element.clientWidth;

例子

Example

padding-top

Gentle, individualistic and very loyal, Birman cats fall between Siamese and Persian in character. If you admire cats that are non aggressive, that enjoy being with humans and tend to be on the quiet side, you may well find that Birman cats are just the felines for you.

Image:BirmanCat.jpgAll Birmans have colorpointed features, dark coloration of the face, ears, legs and tail.

Cat image and text coming fromwww.best-cat-art.com

padding-bottom

Left Top Right Bottom margin-top margin-bottom border-top border-bottom

Image:clientWidth.png

详述
clientWidth并不是W3C草案的内容,它是由MSIE DHTML 推荐的.

原文:
https://developer.mozilla.org/en/DOM/element.scrollWidth
https://developer.mozilla.org/en/DOM/element.offsetWidth
https://developer.mozilla.org/en/DOM/element.clientWidth


参考及相关阅读
MSDN: scrollWidth Property
MSDN: offsetWidth Property
MSDN: clientWidth definition
MSDN: Measuring Element Dimension and Location
DOM:element.scrollWidth
DOM:element.offsetWidth
DOM:element.clientWidth
Determining the dimensions of elements
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值