JavaScript中clientWidth,offsetWidth,scrollWidth的区别


Posted in Javascript onJanuary 25, 2021

一、概念

它们都是Element的属性,表示元素的宽度:

Element.clientWidth    内容+内边距-滚动条-----不包括边框和外边距  == 可视内容
Element.scrollWidth    内容+内边距+溢出尺寸-----不包括边框和外边距  ==实际内容
Element.offsetWidth   元素的宽度(内容+内边距+边框+滚动条)==整体,整个控件

二、举例

1、仅有横向滚动条的情况

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>测试scrollWidth、clientWidth、offsetWidth</title>
 <style type="text/css">
  body, html {
   margin: 0px;
   padding: 0px;
  }

  #father {
   width: 300px;
   overflow: auto;
   padding: 10px;
   background: rebeccapurple;
   border: 10px solid red;
   margin: 20px;
  }

  #child {
   height: 100px;
   width: 1000px;
   padding: 10px;
   border: 20px solid #ffcc99;
   margin: 30px;
  }
 </style>
</head>
<body>

<div id="father">
 <div id="child"></div>
</div>

<script type="text/javascript">
 var child = document.getElementById("child");
 console.log("child.width:", window.getComputedStyle(child).width); //内容的宽度:1000px
 console.log("child.clientWidth:", child.clientWidth); //内容+内边距-滚动条-----不包括边框和外边距 == 可视内容 1020px
 console.log("child.scrollWidth:", child.scrollWidth); //内容+内边距+溢出尺寸-----不包括边框和外边距 ==实际内容 1020px
 console.log("child.offsetWidth:", child.offsetWidth); //元素的宽度(内容+内边距+边框+滚动条)==整体,整个控件  1060px

 var father = document.getElementById("father");
 console.log("father.width:", window.getComputedStyle(father).width); //内容的宽度:300px
 console.log("father.clientWidth:", father.clientWidth); //内容+内边距-滚动条-----不包括边框和外边距 == 可视内容 320px
 console.log("father.scrollWidth:", father.scrollWidth); //内容+内边距+溢出尺寸-----不包括边框和外边距 ==实际内容 1100px
 console.log("father.offsetWidth:", father.offsetWidth); //元素的宽度(内容+内边距+边框+滚动条)==整体,整个控件  340px
</script>
</body>
</html>

仅有横向滚动条的情况时,父元素收受到子元素宽度的影响,其他比较特别的是scrollWidth。

父元素的scrollWidth是:子元素的content+padding+border+子元素一边的margin+父元素一边的padding。

2、有横向滚动条和竖向滚动条的情况

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>测试scrollWidth、clientWidth、offsetWidth</title>
 <style type="text/css">
  body, html {
   margin: 0px;
   padding: 0px;
  }

  #father {
   height: 50px;
   width: 300px;
   overflow: auto;
   padding: 10px;
   background: rebeccapurple;
   border: 10px solid red;
   margin: 20px;
  }

  #child {
   height: 100px;
   width: 1000px;
   padding: 10px;
   border: 20px solid #ffcc99;
   margin: 30px;
  }
 </style>
</head>
<body>

<div id="father">
 <div id="child"></div>
</div>

<script type="text/javascript">
 var child = document.getElementById("child");
 console.log("child.width:", window.getComputedStyle(child).width); //内容的宽度:1000px
 console.log("child.clientWidth:", child.clientWidth); //内容+内边距-滚动条-----不包括边框和外边距 == 可视内容 1020px
 console.log("child.scrollWidth:", child.scrollWidth); //内容+内边距+溢出尺寸-----不包括边框和外边距 ==实际内容 1020px
 console.log("child.offsetWidth:", child.offsetWidth); //元素的宽度(内容+内边距+边框+滚动条)==整体,整个控件  1060px

 var father = document.getElementById("father");
 console.log("father.width:", window.getComputedStyle(father).width); //内容的宽度:285px
 console.log("father.clientWidth:", father.clientWidth); //内容+内边距-滚动条-----不包括边框和外边距 == 可视内容 305px
 console.log("father.scrollWidth:", father.scrollWidth); //内容+内边距+溢出尺寸-----不包括边框和外边距 ==实际内容 1100px
 console.log("father.offsetWidth:", father.offsetWidth); //元素的宽度(内容+内边距+边框+滚动条)==整体,整个控件  340px
</script>
</body>
</html>

父元素的width为:父元素的content宽度-滚动条的宽度(大约为15px)

父元素的clientWidth为:父元素的content宽度+父元素padding宽度-滚动条宽度(大约为15px)

以上就是Element中clientWidth,offsetWidth,scrollWidth的区别的详细内容,更多关于clientWidth,offsetWidth,scrollWidth的区别的资料请关注三水点靠木其它相关文章!

Javascript 相关文章推荐
javascript 数组的方法集合
Jun 05 Javascript
JS求平均值的小例子
Nov 29 Javascript
iframe窗口高度自适应的实现方法
Jan 08 Javascript
JSONP跨域GET请求解决Ajax跨域访问问题
Dec 31 Javascript
javascript关于open.window子页面执行完成后刷新父页面的问题分析
Apr 27 Javascript
js剪切板应用clipboardData实例解析
May 29 Javascript
基于JavaScript实现屏幕滚动效果
Jan 18 Javascript
vue2.0 自定义 饼状图 (Echarts)组件的方法
Mar 02 Javascript
vue translate peoject实现在线翻译功能【新手必看】
Jun 07 Javascript
Typescript 中的 interface 和 type 到底有什么区别详解
Jun 18 Javascript
javascript(基于jQuery)实现鼠标获取选中的文字示例【测试可用】
Oct 26 jQuery
微信小程序实现拨打电话功能的示例代码
Jun 28 Javascript
vue keep-alive的简单总结
Jan 25 #Vue.js
JS实现简易日历效果
Jan 25 #Javascript
javascript代码实现简易计算器
Jan 25 #Javascript
js简单粗暴的发布订阅示例代码
Jan 23 #Javascript
JS中循环遍历数组的四种方式总结
Jan 23 #Javascript
vue form表单post请求结合Servlet实现文件上传功能
Jan 22 #Vue.js
原生js实现自定义难度的扫雷游戏
Jan 22 #Javascript
You might like
PHP5中MVC结构学习
2006/10/09 PHP
PHP网站备份程序代码分享
2011/06/10 PHP
使用php验证复选框有效性的示例
2013/11/13 PHP
PHP和Mysql中转UTF8编码问题汇总
2015/10/10 PHP
Zend Framework教程之Bootstrap类用法概述
2016/03/14 PHP
PHP实现基于PDO扩展连接PostgreSQL对象关系数据库示例
2018/03/31 PHP
IE浏览器PNG图片透明效果代码
2008/09/02 Javascript
Jquery中getJSON在asp.net中的使用说明
2011/03/10 Javascript
删除select中所有option选项jquery代码
2013/08/12 Javascript
JavaScript实现节点的删除与序号重建实例
2015/08/05 Javascript
实例代码详解javascript实现窗口抖动及qq窗口抖动
2016/01/04 Javascript
js窗口震动小程序分享
2016/11/28 Javascript
详解js数组的完全随机排列算法
2016/12/16 Javascript
easyUI实现类似搜索框关键词自动提示功能示例代码
2016/12/27 Javascript
移动设备手势事件库Touch.js使用详解
2017/08/18 Javascript
zepto.js 实时监听输入框的方法
2018/12/04 Javascript
JS html事件冒泡和事件捕获操作示例
2019/05/01 Javascript
微信小程序实现拍照画布指定区域生成图片
2019/07/18 Javascript
js简单实现自动生成表格功能示例
2020/06/02 Javascript
node脚手架搭建服务器实现token验证的方法
2021/01/20 Javascript
[13:21]DOTA2国际邀请赛采访专栏:RSnake战队国士无双,Fnatic.Fly
2013/08/06 DOTA
Python列表list数组array用法实例解析
2014/10/28 Python
Python删除Java源文件中全部注释的实现方法
2017/08/30 Python
Python3.7实现中控考勤机自动连接
2018/08/28 Python
Django使用paginator插件实现翻页功能的实例
2018/10/24 Python
详解Python中的内建函数,可迭代对象,迭代器
2019/04/29 Python
python 使用pdfminer3k 读取PDF文档的例子
2019/08/27 Python
PyCharm License Activation激活码失效问题的解决方法(图文详解)
2020/03/12 Python
python字符串判断密码强弱
2020/03/18 Python
分享unittest单元测试框架中几种常用的用例加载方法
2020/12/02 Python
韩国11街:11STREET
2018/03/27 全球购物
Lentiamo丹麦:购买便宜的隐形眼镜
2021/01/13 全球购物
怎么处理XML的中文问题
2015/03/26 面试题
新闻专业个人求职信
2013/12/19 职场文书
挂牌仪式主持词
2014/03/20 职场文书
Redis主从配置和底层实现原理解析(实战记录)
2021/06/30 Redis