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 ...
随机推荐
- java练习---5
//程序员:罗元昊 2017.9.17 package demo;import java.util.Scanner;public class Ly { public static void main ...
- JAVA遍历机制的性能的比较
本文首发于cartoon的博客 转载请注明出处:https://cartoonyu.github.io/cartoon-blog/post/java/java%E9%81%8D%E5% ...
- python查漏补缺 --- 模块及异常
1.方法定义好之后,如,def test(x) : ,此时将方法名赋值给一个新的变量,那么该变量等同于方法,可以具备test方法内部的全部功能2.导包的时候,可以使用as关键字在不同的名称下导入模块或 ...
- Android的步骤
1.开发Activity步骤 第一步:写一个类继承Activity 第二步:重写oncreate方法 第三步:在主配置文件中注册activity <activity android:name=& ...
- handlerMapping的初始化以及查找handler
前提:HttpServletBean初始化了一些servlet配置,接着FrameWorkServlet创建了WebApplicationContext,最后DispatcherServlet初始化一 ...
- 1.4.1python下载网页(每天一更)
# -*- coding: utf-8 -*- ''' Created on 2019年4月27日 @author: lenovo ''' # import urllib3 # def downloa ...
- [Hei.Captcha] Asp.Net Core 跨平台验证码实现
写在前面 说起来比较丢脸.我们有个手机的验证码发送逻辑需要使用验证码,这块本来项目里面就有验证码绘制逻辑,.Net Framework的,使用的包是System.Drawing,我把这验证码绘制逻辑复 ...
- 在 alpine 中使用 NPOI
在 alpine 中使用 NPOI Intro 在 .net 中常使用 NPOI 来做 Excel 的导入导出,NPOI 从 2.4.0 版本开始支持 .netstandard2.0,对于.net c ...
- 精准营销、批量提取QQ群成员号码
有时我们在做精准营销时,需要从社群里提取群成员的QQ号,群发邮件,常规的做法是手工一个个复制粘贴,这样的效率无疑是很低的,下面我来分享一个批量获取社群的QQ号方法. 需要具备以下工具: 1.大量精准Q ...
- 大型系列课程之-七夕告白之旅Electron篇
上一篇分享了一下vbs的撩妹攻略,但细心的兄弟会发现,这种脚本式的攻城方案并不得心应手,有很多妹子害怕是病毒根本不敢点击,而且这个脚本界面风格也不漂亮,不能轻易打动妹子的心,怎么破,小编这次在为各位老 ...