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 ...
随机推荐
- G - 旅行的意义(概率DP) (DAG图的概率与期望)
为什么有人永远渴望旅行,或许就因为,巧合和温暖会在下一秒蜂拥而至吧. 一直想去旅游的天天决定在即将到来的五一假期中安排一场环游世界的旅行.为此,他已经提前查阅了很多资料,并准备画一张旅游路线图.天天先 ...
- 系统学习Javaweb9----BootStrap1
学习内容: 1.BootStrap的简述 2.BootStrap环境搭建 3.BootStrap环境搭建-基本模板创建 4.BootStrap环境搭建-基本模板讲解 5.BootStrap布局容器 6 ...
- 007.前端开发知识,前端基础CSS(2020-01-28)
一.布局 一列固定宽度且居中 两列左窄右宽型 通栏平均分布型 1.一列固定宽度且居中布局<body> .top+.banner+.main+.footer 按Tab键,得到下框中代码 &l ...
- [LC] 59. Spiral Matrix II
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral ord ...
- [LC] 114. Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...
- sqlserver 命令执行sql脚本
osql -S 主机名 -U 用户名 -P 密码 -i E:\20190723.sql
- 解决Navicat无法连接到MySQL的问题
解决Navicat无法连接到MySQL的问题 问题一:本地IP(xxx.xxx.xxx.xxx)没有访问远程数据库的权限.于是下面开启本地IP(xxx.xxx.xxx.xxx)对远程mysql数据库的 ...
- Spring第一课:IOC控制反转,什么是反转,什么又是控制?
前言 学习Spring第一课,就是认识IOC控制反转,要了解它还真得花一些功夫.今天主要理解透彻它的真谛,而不仅限于表面. 上道小菜 public class BusinessService { pr ...
- 翻转链表和k个一组翻转以及两两反转
一.输入一个链表,输出反转后的链表. 非递归实现: # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.v ...
- 博客已搬迁到 blog.vivym.xyz
博客已搬迁到 blog.vivym.xyz