了解 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. jodd cache实现缓存超时

    public class JoddCache { private static final int CACHE_SIZE = 2; private final static Cache<Obje ...

  2. go语言系列--golang在windows上的安装和开发环境goland的配置

    在windows上安装golang软件 golang中国网址为:https://studygolang.com/dl 我的学习选择版本:1.12.5 golang 1.12.5版本更新的内容:gola ...

  3. Linux vi/vim and linux yum 命令

    Linux vi/vim 所有的 Unix Like 系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在. 但是目前我们使用比较多的是 vim 编辑器. vim 具有程序编辑的能力,可以主 ...

  4. 【Mark】博弈类题目小结(HDU,POJ,ZOJ)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 首先当然要献上一些非常好的学习资料: 基础博弈的小 ...

  5. swiper实现滑动到某页锁住不让滑动

    var swiper = new Swiper('.swiper-container', { pagination: '.swiper-pagination', onTouchStart: funct ...

  6. ali之mtl平台学习

    摩天轮平台可以进行无线测试.设备借用.打包发布.线上监控等功能. 无线测试包括:mock测试.真机适配.代码审查.验收报告等. mock测试类似于fiddler,主要用于接口查看,可以查看请求,返回串 ...

  7. 用命令行编译运行java文件的乱码问题

    之前在写的时候没有遇到过这个问题,用惯了eclipse之后突然用Notepad++就出现乱码了 我在编写的时候 指定Noepad++的编码是 UTF-8编码,然后进入命令行,编译的时候就出现了乱码 然 ...

  8. 冲刺周三The Third Day

    一.ThirdDay照片 二.项目分工 三.今日份燃尽图 四.项目进展 码云团队协同环境构建完毕 利用Leangoo制作任务分工及生成燃尽图 完成AES加解密部分代码 用代码实现对文件的新建.移动.复 ...

  9. 尚硅谷Docker---docker安装及简介

    尚硅谷Docker---docker安装及简介 一.总结 一句话总结: docker就相当于是一个极微型的linux系统,独立 1.使用Docker的步骤? 1).安装Docker 2).去Docke ...

  10. CentOS 6.7与CentOS 7.3内存查看命令free、top的差异对比

    https://www.centos.bz/2017/08/centos-6-7-centos-7-3-free-top/