offsetwidth\clientwidth\scrollwidth

关于js中的offsetwidth、clientwidth、scrollwidth等一系列属性及其方法一直都傻傻分不清,这里就来总结一下这些方法的用法和含义。

目录

一、clientwidth和clientheigh 、 clienttop和clientleft

二、offsetwidth和offsethight 、 offsettop和offsetleft

三、scrollwidth和scrollheight 、 scrolltop和scrollleft


一、clientwidth和clientheigh 、 clienttop和clientleft

        1,clientwidth的实际宽度  clientwidth = width+左右padding

        2,clientheigh的实际度   clientheigh = height + 上下padding 

        3,clienttop的实际宽度   clienttop = boder.top(上边框的宽度)

        4,clientleft的实际宽度   clientleft = boder.left(左边框的宽度)

二、offsetwidth和offsethight 、 offsettop和offsetleft

        1,offsetwidth的实际宽度
                offsetwidth = width + 左右padding + 左右boder

        2,offsetheith的实际高度
                offsetheith = height + 上下padding + 上下boder

        3,offsettop实际宽度
                offsettop:当前元素 上边框 外边缘 到 最近的已定位父级(offsetparent) 上边框 内边缘的 距离。如果父级都没有定位,则分别是到body 顶部 和左边的距离

        4,offsetleft实际宽度
                offsetleft:当前元素 左边框 外边缘 到 最近的已定位父级(offsetparent) 左边框 内边缘的 距离。如果父级都没有定位,则分别是到body 顶部 和左边的距离

三、scrollwidth和scrollheight 、 scrolltop和scrollleft

        1,scrollwidth实际宽度
                scrollwidth:获取指定标签内容层的真实宽度(可视区域宽度+被隐藏区域宽度)。

        2,scrollheight的实际高度
                scrollheight:获取指定标签内容层的真实高度(可视区域高度+被隐藏区域高度)

        3,scrolltop
                scrolltop :内容层顶部 到 可视区域顶部的距离。

        4,scrollleft

                scrollleft:内容层左端 到 可视区域左端的距离.

注意: 下面元素属性和元素方法都通过 elem.属性 或 elem.方法 的方式使用,window属性通过 window.属性 的方式使用,document属性则通过document调用。

这三个属性 `scrollWidth`、`offsetWidth` 和 `clientWidth` 都是用于获取 DOM 元素的宽度,但它们的用途和计算方式有所不同。下面将详细解释它们的区别,并给出代码示例帮助你理解。 --- ### ✅ `clientWidth` - **定义**:元素内部的可视区域宽度(不包括边框、滚动条、外边距)。 - **组成**:`width + padding`(不包括 border 和 margin)。 - **是否包含滚动条**:不包含。 ```javascript const element = document.getElementById('myDiv'); console.log(element.clientWidth); // clientWidth ``` --- ### ✅ `offsetWidth` - **定义**:元素的可见区域总宽度,包括内容、内边距、边框。 - **组成**:`width + padding + border`。 - **是否包含滚动条**:包含滚动条(如果存在)。 - **注意**:这个值是只读的。 ```javascript console.log(element.offsetWidth); // offsetWidth ``` --- ### ✅ `scrollWidth` - **定义**:元素内容的实际宽度,包括不可见的部分(即被隐藏的部分)。 - **组成**:内容的实际宽度(即使内容被滚动条隐藏了,也计算在内)。 - **是否包含滚动条**:不包含。 ```javascript console.log(element.scrollWidth); // scrollWidth ``` --- ### ✅ 总结对比表格 | 属性名 | 包含内容 | 是否包括滚动条 | 是否包括边框 | 是否包括隐藏内容 | |---------------|------------------|----------------|----------------|------------------| | `clientWidth` | width + padding | ❌ | ❌ | ❌ | | `offsetWidth` | width + padding + border | ✅ | ✅ | ❌ | | `scrollWidth` | 实际内容宽度 | ❌ | ❌ | ✅ | --- ### ✅ 使用场景举例 - **判断文字是否被截断(省略号显示)**: ```javascript function isTruncated(element) { return element.scrollWidth > element.offsetWidth; } ``` - **获取元素的可见区域大小(包括边框)**: ```javascript const visibleWidth = element.offsetWidth; ``` - **获取元素内部完整内容宽度(即使被隐藏)**: ```javascript const fullContentWidth = element.scrollWidth; ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值