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 ... 
随机推荐
- 研究NLP100篇必读的论文---已整理可直接下载
			100篇必读的NLP论文 100 Must-Read NLP 自己汇总的论文集,已更新 链接:https://pan.baidu.com/s/16k2s2HYfrKHLBS5lxZIkuw 提取码:x ... 
- 三:mysql条件查询
			1:查询工资等于5000的员工 
- [LC] 434. Number of Segments in a String
			Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ... 
- Java IO: FileReader和FileWriter
			作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) 本章节将简要介绍FileReader和FileWriter.与FileInputStream和File ... 
- SHELL用法五(Case语句)
			1.SHELL编程Case语句案例实战 1)Case选择条件语句的格式: case $INPUT in Pattern1) 语句1 ;; Pattern2) 语句2 ;; esac 2)Case语句企 ... 
- take office|boast|think twice|dispose of|level|stuff|'s mature for|a green hand|'s a slave to|
			One reporter wrote that Dewey was acting like a man who had already been elected and was only passin ... 
- JavaScript小数转百分比
			在浏览器的控制台操作0.28*100会得到28.000000000000004这样一个不太精确的值 进行转换 toPercent(point){ let str = Number(point * 10 ... 
- python两个队列实现一个栈和两个栈实现一个队列
			1.两个栈实现一个队列 两个栈stack1和stack2, push的时候直接push进stack1,pop时需要判断stack1和stack2中的情况.如果stack2不为空的话,直接从stack2 ... 
- chap4-关联接口测试-通过全局变量传递cookie
			# 1 http_request.py import requests class HttpRequest: def http_request(self, url, method, data=None ... 
- byte的取值范围是-128~127,那么包含-128和127吗?
			本帖最后由 王德升老师 于 2019-12-27 17:56 编辑 byte的取值范围为什么是-128~127?如果面试官问你取值范围包含127吗?1. 首先我们知道Java中byte类型是1个字节占 ... 
