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 ...
随机推荐
- 基础篇六:Nginx编译配置参数
- P2486 [SDOI2011]染色 区间合并+树链剖分(加深对线段树的理解)
#include<bits/stdc++.h> using namespace std; ; struct node{ int l,r,cnt,lazy; node(,,,):l(l1), ...
- django框架进阶-解决跨域问题
####################################### """ 一.为什么会有跨域问题? 是因为浏览器的同源策略是对ajax请求进行阻拦了,但是不 ...
- git基本操作-长期维护
############### 初识git ############## """ git初识: 1,git是什么?git是一个帮助用户版本控制的软件, 2,g ...
- jsp 页面内容导出到Excel中
日常使用网络资源时经常需要把网页中的内容下载到本地,并且导出到Excel中,现在介绍一种非常简单的方式实现网络资源的下载.只需要讲jsp的最上面加上一句话 <% response.reset() ...
- CString转换成std::string
unicode的编码格式: CString strCS; std::string strSTD = CT2A(strCS.GetBuffer()); 其他的编码格式: CString strCS; ...
- 快速搭建本地Nuget服务
一 创建Nuget 服务项目 1.创建一个空白的asp.net web项目,需要.net 4.6以上 2.在Nuget中搜索 nuget.server ,可以看到是由 .Net 基金再维护的,几乎傻 ...
- Linux那些事——GTK+, Qt, Gnome, KDE, xWindow, xOrg区别
Linux那些事--GTK+, Qt, Gnome, KDE, xWindow, xOrg区别 Linux不仅内核开源,系统配置也是高度可定制化的,其中就包括我们所熟知的图形界面,从桌面环境,主题,字 ...
- Mysql错误问题:ERROR 1005 (HY000): Can't create table 'crm_1.tbl_client' (errno: 150)
MySQL外键创建条件: 1.两个表必须是InnoDB数据引擎2.外键表的外键字段必须是主键3.字段类型必须一致 创建表时创建外键: create table tbl_client(userName ...
- Allure介绍
以下内容基于pytest的框架进行展示: 什么是Allure Allure是一个独立的报告插件,生成美观易读的报告,目前支持语言:Java, PHP, Ruby, Python, Scala, C#. ...