了解 offsetWidth clientWidth scrollWidth 的区别

最近需要清除区分开元素的width,height及相应的坐标等,当前这篇用来区分offsetWidth clientWidth scrollWidth的区别

各自的概念

假设有一个元素,width有以下几个进行组合

  • content
  • padding-left
  • padding-right
  • scrollbar 垂直的滚动条宽度(假设有,没有便为0)
  • border-left
  • border-right

clientWidth = content + padding-left + padding-right

offsetWidth = content + padding-left + padding-right + border-left + border-right + scrollbar

scrollWidth = content + padding-left + padding-right + scrollbar + border-left + border-right +滚动进入不可见的内容

示例

在上述中,我们可以计算出 scrollbar

const scrollbar = el.offsetWidth - (border-left + border-right) - clientWidth

html

<section class="client-xyz">
<p>我是很长很长的内容我是很长很长的内容我是很长很长的内容我是很长很长的内容我是很长很长的内容我是很长很长的内容</p>
</section>

css

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;
}

js

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

总结

offsetWidth clientWidth scrollWidth 的区别的更多相关文章

  1. offsetWidth clientWidth scrollWidth 三者之间的区别和联系

    scrollWidth:对象的实际内容的宽度,不包边线宽度,会随对象中内容超过可视区后而变大. clientWidth:对象内容的可视区的宽度,不包滚动条等边线,会随对象显示大小的变化而改变. off ...

  2. js中clientWidth, scrollWidth, innerWidth, outerWidth,offsetWidth的区别

    js中clientWidth, scrollWidth, innerWidth, outerWidth,offsetWidth的属性汇总,测试浏览器:ie7~ie11.chrome 和 firefox ...

  3. 四种浏览器对 clientHeight、offsetHeight、scrollHeight、clientWidth、offsetWidth 和 scrollWidth 的解释差异

    网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetWid ...

  4. JS 获取浏览器窗口大小clientWidth、offsetWidth、scrollWidth

    常用: JS 获取浏览器窗口大小   // 获取窗口宽度 if (windows.innerWidth) winWidth = windows.innerWidth; else if ((docume ...

  5. JS:body元素对象的clientWidth、offsetWidth、scrollWidth、clientLeft、offsetLeft、scrollLeft

    document.body.clientWidth 获取body元素对象的内容可视区域的宽度,即clientWidth=width+padding,不包括滚动条. document.body.clie ...

  6. clientWidth、offsetWidth、scrollWidth……

    1.元素视图属性 clientWidth:元素内容可视区宽度(水平方向 width + 左右 padding). clientHeight:元素内容可视高度(垂直方向 height + 上下paddi ...

  7. js的offsetWidth,clientWidth

    js元素的offsetWidth与clientWidth很相似,因此放在一起记录. clientWidth与offsetWidth clientWidth=元素内容区域宽度+水平内边距padding. ...

  8. JS中关于clientWidth offsetWidth scrollWidth 的区别及意义

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  9. scrollWidth、clientWidth、offsetWidth、width的区别

    scrollWidth:对象的实际内容的宽度,不包边线宽度,会随对象中内容超过可视区后而变大. clientWidth:对象内容的可视区的宽度,不包滚动条等边线,会随对象显示大小的变化而改变. off ...

随机推荐

  1. 【BZOJ4565】 [Haoi2016]字符合并

    Description 有一个长度为 n 的 01 串,你可以每次将相邻的 k 个字符合并,得到一个新的字符并获得一定分数.得到的新字 符和分数由这 k 个字符确定.你需要求出你能获得的最大分数. I ...

  2. Vue项目开发,nprogress进度条加载之超详细讲解及实战案例

    Nprogress的默认进度条很细,它的设计灵感主要来源于 谷歌,YouTube 他的安装方式也很简单,你可以有两种使用方式: 直接引入js和css文件 使用npm安装的的方式 直接引入: Npm安装 ...

  3. Windows10 pro & ent 禁用自动更新

    为了节约可怜的一点点流量,这个设置还是很重要的. Step 1: 通过在命令提示符中执行gpedit.msc命令,打开组策略 Step 2: 打开管理模板->windows组件->wind ...

  4. Linux whatis命令失效 nothing appropriate

    在虚拟机中安装Linux的时候,可能没有生成whatis的数据库,所以whatis的命令无法使用. 如果出现无法使用whatis命令失效,那就生成whatis数据库. 命令: /usr/sbin/ma ...

  5. Bug管理工具MantisBT-2.18.0安装教程

    Bug管理工具MantisBT安装教程 MantisBT官网下载地址:https://sourceforge.net/projects/mantisbt/# 写于:2018.12.1 如上传博客资料图 ...

  6. vscode-php代码提升及函数跳转

    安装插件,php intellisense 安装后还要配置一下PHP的运行路径 打开扩展     输入 PHP IntelliSense     安装     文件 - 首选项 - 设置 - 扩展 - ...

  7. 关于如何解决TeamViewer限制时间问题

    最近在弄一个项目,我们是乙方,甲方离我们比较远,所以需要用到远程操作软件.也就是TeamViewer. 这个软件一开始运行还行,后来时间用久了,很容易被限制时间.在网上查了大部分资料,都是一些修改MA ...

  8. JavaScript 事件不触发

    在class上绑定的事件,点击图片的时候无法触发 <div id="files" class="files"> <div> <p& ...

  9. WebApi系列知识总结

    WebApi系列知识 一.webApi项目搭建 1.新建WebApi项目 (1) (2) (3) (4) Areas – HelpPage – App_Start – HelpPageConfig.c ...

  10. 十、Springboot之thymeleaf与jsp共存

    : pom.xml添加依赖 <!--thymeleaf整合JSP需要用到下面的依赖--> <dependency> <groupId>org.thymeleaf&l ...