clientWidth offsetWidth等视窗尺寸
clientWidth和offsetWidth
clientWidth
是一个只读属性,返回元素的内部宽度,该属性包括内边距,但不包括垂直滚动条(如果有)、边框和外边距。
offsetWidth
是一个只读属性,返回一个元素的布局宽度。一个典型的offsetWidth是测量包含元素的边框、水平线上的内边距、竖直方向滚动条(如果有的话)、以及CSS设置的宽度(width)值。
用法:var offsetWidth = element.offsetWidth;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
#parent {
width: 200px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>
<div id="parent"></div>
<script>
var clientWidth = window.document.getElementById("parent").clientWidth;
var offsetWidth = window.document.getElementById("parent").offsetWidth;
console.log(clientWidth); //200
console.log(offsetWidth); //200
</script>
</body>
</html>
当我们给上面parent元素加上边框内边距时:
#parent {
width: 200px;
height: 200px;
background-color: red;
border: 10px solid black;
padding: 10px;
}
输出结果为:
// 220
// 240
现在我们给parent加一个子元素,并让滚动条出现,完整代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
#parent {
width: 200px;
height: 200px;
background-color: red;
border: 10px solid black;
padding: 10px;
overflow: auto;
}
#son {
width: 300px;
height: 300px;
}
</style>
</head>
<body>
<div id="parent">
<div id="son"></div>
</div>
<script>
var clientWidth = window.document.getElementById("parent").clientWidth;
var offsetWidth = window.document.getElementById("parent").offsetWidth;
console.log(clientWidth);
console.log(offsetWidth);
</script>
</body>
</html>
显示结果如图:
输出信息如下:
// 205
// 240
对于上述代码作个简要说明,clientWidth值为205是这样计算来的:原本我们设置parent的宽度为200,因为我们设置了父元素overflow:scroll属性出现滚动条后,滚动条宽度被包括在这个设置的宽度之内。chrome浏览器滚动条默认宽度为15,所以parent宽度就只剩185.按照上面clientWidth定义来计算,clientWidth = 185(实际width) + 10(padding) + 10(padding).
未完待续
clientWidth offsetWidth等视窗尺寸的更多相关文章
- H5,PC网页屏幕尺寸相关整理(scrollLeft,scrollWidth,clientWidth,offsetWidth)
HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth到底指的哪到哪的距离之完全详解scrollHeight: 获取对象的滚动高度. scrollLef ...
- JS中关于clientWidth offsetWidth scrollWidth 的区别及意义
网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...
- HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth
HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...
- HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth之完全详解
HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth到底指的哪到哪的距离之完全详解 scrollHeight: 获取对象的滚动高度. scrollLe ...
- scrollLeft,scrollWidth,clientWidth,offsetWidth 可实现导航栏固定不动(冻结)的效果
HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位 ...
- HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth之全然具体解释
HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth究竟指的哪到哪的距离之全然具体解释scrollHeight: 获取对象的滚动高度. scrol ...
- clientWidth,offsetWidth,scrollWidth区别
<html> <head> <title>clientWidth,offsetWidth,scrollWidth区别</title> </head ...
- HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth完全详细的说明
HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth具体指完全解释究竟哪里的距离scrollHeight: 获取对象的高度滚动. scrollLe ...
- 转 HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth之完全详解
HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth到底指的哪到哪的距离之完全详解 scrollHeight: 获取对象的滚动高度. scrollLe ...
随机推荐
- SAP PM:参考维护工单创建测量凭证
(1)使用FM:CO_BC_ORDER_POST获取工单资料(Aufnr, aufpo and materials etc): (2)使用FM:MEASUREM_DOCUM_RFC_SINGLE_00 ...
- LGOJ3327 【SDOI2015】约数个数和
又是一道卡常好题 坑掉我的 \(define \space int \space long \space long\) 感觉出题人并没有获得什么快乐-- Description link 题意概述: ...
- 提高线程数,解决redis超时问题
根据压测结果做出的修改历史: 第一步:只针对maxWorkerThreads.maxIoThreads和minWorkerThreads做了修改<processModel autoConfig= ...
- ExecutorCompletionService理解记忆
java.util.concurrent 类 ExecutorCompletionService<V> java.lang.Object 继承者 java.util.concurren ...
- zabbix 使用邮件发送告警信息
配置系统mail命令,使其可以发送外网邮件 mail 命令配置 修改zabbix_server配置文件,使其可以执行告警脚本 [root@rexen etc]# vim /usr/local/zabb ...
- as和强制类型转换的区别
之前一直以为as就是强制类型转换,只是as是AS3中新的语法,之前用在有继承关系的对象之间的转换也无甚区别,但是今天却让我领悟到了它俩之间的区别. 原起:今天要给ColorPicker控件动态赋值,它 ...
- ambulance|severely|halt
N-COUNT 救护车An ambulance is a vehicle for taking people to and from hospital. very seriously 严重地 Thei ...
- python学习笔记(11)文件操作
一.读文件 读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直 ...
- perf4j+spring+aop 配置 注解方式
今天将perf4j基于spring aop方式进入了接入,接入方法还是比较简单.具体配置如下: logback.xml <!--perf4j配置--> <appender name= ...
- 吴裕雄--天生自然Android开发学习:Android studio 3.5安装详解
3. 建立AVD(安卓虚拟设备) 点击右上角AVD Manager图标,单击按钮Create Virtual Device,选择Nexus 5X,下一步,选择版本9.0,Download,然后Next ...