JS之clientWidth、offsetWidth等属性介绍
一、clientXXX 属性
代码演示
// css 部分
<style>
.test{
width:100px;
height:100px;
border:1px solid red;
padding:20px;
}
</style>
// body 和 js 部分
<div class="test">
hello
</div>
<script>
var div = document.getElementsByTagName('div')[0];
console.log(div.clientWidth);
console.log(div.clientHeight)
console.log(div.clientTop)
console.log(div.clientLeft)
// 注意不含有以下两个属性
// console.log(div.clientRight)
// console.log(div.clientBottom)
</script>
控制台打印输出(结果)


根据答应结果和div盒子模型比较可得:
clientWidth = padding值*2(左右padding值) + width 值;
clientHeight = padding值*2(上下padding值) + height 值;
clientTop = border值(上边框值)
clientLeft = border值(左边框值)
二、offsetXXX 属性
js代码
<script>
var div = document.getElementsByTagName('div')[0];
console.log(div.offsetWidth);
console.log(div.offsetHeight)
console.log(div.offsetTop)
console.log(div.offsetLeft)
//注意: 以下两个属性不存在
console.log(div.offsetRight)
console.log(div.offsetBottom)
</script>
打印结果分析

div盒子模型

body 盒子模型

由以上两个模型分析得出
offsetWidth = 左右padding值+width值+左右border值
offsetHeight = 上下padding值+height值+上下border值
offsetTop 表示该元素的边框上边缘与它最近父元素的上边框内边缘的距离,如果没有父元素则把body当做它的父元素,由此知(默认情况下body有个margin值8px)
offsetTop = 8
offsetLeft = 8
JS之clientWidth、offsetWidth等属性介绍的更多相关文章
- js中clientWidth, scrollWidth, innerWidth, outerWidth,offsetWidth的区别
js中clientWidth, scrollWidth, innerWidth, outerWidth,offsetWidth的属性汇总,测试浏览器:ie7~ie11.chrome 和 firefox ...
- 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 ...
- H5,PC网页屏幕尺寸相关整理(scrollLeft,scrollWidth,clientWidth,offsetWidth)
HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth到底指的哪到哪的距离之完全详解scrollHeight: 获取对象的滚动高度. scrollLef ...
- HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth完全详细的说明
HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth具体指完全解释究竟哪里的距离scrollHeight: 获取对象的高度滚动. scrollLe ...
- 转 HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth之完全详解
HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth到底指的哪到哪的距离之完全详解 scrollHeight: 获取对象的滚动高度. scrollLe ...
随机推荐
- python requests、xpath爬虫增加博客访问量
这是一个分析IP代理网站,通过代理网站提供的ip去访问CSDN博客,达到以不同ip访同一博客的目的,以娱乐为主,大家可以去玩一下. 首先,准备工作,设置User-Agent: #1.headers h ...
- cookbook_类与对象
1修改实例的字符串表示 可以通过定义__str__()和__repr__()方法来实现 class Pair: def __init__(self,x,y): self.x = x self.y = ...
- es6,@import一直报错 Can't resolve。。
最近在项目中新增了一个按钮组的组件页面,但是在其他页面引入时,一直报错 引入方式:
- TIJ读书笔记-第21章-并发
一本Think in java,从去年6月份开始读,读了快一年了,也快读完了,然而回头想想,却好像什么也不记得了,好记性不如烂笔头,那就从现在开始记录一下吧.由于现在在读的是并发,那就先从这章开始吧. ...
- 【iOS】更换证书遇到的问题
今天给一个项目换证书的时候遇到了这个问题: Code Sign error: Provisioning profile does not match bundle identifier: The pr ...
- MapReduce 运行全过程解析
关注公众号,大家可以在公众号后台回复“博客园”,免费获得作者 Java 知识体系/面试必看资料. 前言 前面我们讲了 MapReduce 的编程模型,我们知道他主要分成两大阶段来完成一项任务,一是 m ...
- HelloDjango 系列教程:Django 的接客之道
文中涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 Web 服务简单的说就是处理请求,每个请求就像是一个"顾客".首先热情地把顾客迎接进来,然后满足用户的个性 ...
- ContentProvider 使用详解
极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...
- mac安装ElasticSearch+head+node+一个例子~
1.下载ElasticSearch 官网下载链接:https://www.elastic.co/cn/downloads/past-releases(进去的可能会比较慢,网络好的情况下会好一些) 我下 ...
- Java——数据结构(链表)
链表,可扩展长度,泛型. public class Link { Node header = null; //头结点 int length;//当前链表长度 class Node { Node nex ...