1、offsetWidth: 为元素的width+元素的padding+边框的宽度

如图:

2、offsetLeft、offsetTop、offsetRight、offsetBottom

以offsetLeft为例进行说明,在不同的浏览器中其值不同,且与父元素的position属性(relative,absolute,fixed)有关。现分以下几种情况说明:(测试所用的浏览器版本为:Chrome 68.0.3440.106, opera54.0, Firefox61.0.1和IE11.0)

2.1在父元素均不设置position属性时,在Chrome,opera和IE浏览器中offsetLeft是元素边框外侧到浏览器窗口内侧的距离且body.offsetLeft=0,

在firefox浏览器中offsetLeft是元素边框外侧到body内侧的距离body.offsetLeft=-边框宽度

如图:

2.2当父元素设置position元素时又分为两种情况,

2.2.1如果父元素时body且body设置了position属性,在Chrome和opera浏览器中offsetLeft是元素边框外侧到body边框外侧的距离,

在IE和fireForx浏览器中offsetLeft是元素边框外侧到body边框内侧的距离

2.2.2如果父元素不是body元素且设置了position属性时,offsetLeft为元素边框外侧到父元素边框内侧的距离(各浏览器情况一致)。

如图

3、下面通过实例进行说明(Chrome浏览器):

Html结构为

Css样式:将body,container,box, content的margin和padding都设置为10px,container长宽为300px,box长宽为100px,content长宽为50px,都设置宽度为5px的边框。具体查看下方源代码。效果如下图

3.1 offsetWidth

container.offsetWidth =container的width+padding+边框宽度=300+2×10+2×5=330

console.log(container.offsetWidth)输出结果为

3.2

3.2.1父元素均不设置position属性

document.body.offsetLeft=0

document.body.offsetLeft= container边框外侧到窗口内侧的距离=body.margin+body边框宽度+container.margin+container.padding+container的left=10+5+10+10=35px;

aBoxes[0].offsetLeft=第一个div.box边框外侧到窗口内侧的距离=body.margin+body边框宽度+container.margin+container.padding+container边框宽度+box.margin+box.padding=10+5+10+10+5+10+10px=60px;

console.log(document.body.offsetLeft)

console.log(document.body.offsetLeft)

console.log(aBoxes[0].offsetLeft)输出结果为

2.2.2将body的position设置为relative

document.body.offsetLeft=0

container.offsetLeft=container边框外侧到body边框外侧的距离= body边框宽度+container.margin+container.padding =5+10+10=25px;

aBoxes[0].offsetLeft=第一个div.box边框外侧到body边框外侧的距离= body边框宽度+container.margin+container.padding+container边框宽度+box.margin+box.padding=5+10+10+5+10+10px=50px;

console.log(document.body.offsetLeft);

console.log(container.offsetLeft);

console.log(aBoxes[0].offsetLeft);输出结果为

2.2.3将container的position设置为relative,并设置其left为100px,body不设置position属性。

oContainer.offsetLeft= 为container边框外侧到窗口内侧的距离=body.margin+body边框宽度+container的left+container.margin+container.padding+container的left=10+5+100+10+10=135;

aBoxes[0].offsetLeft=第一个div.box边框外侧到container边框内侧的距离= container.padding+box.margin=10+10px=20;

aBoxes[1].offsetLeft= 第二个div.box边框外侧到container边框内侧的距离= container.padding + box.margin +box.offsetWidth+2*box.margin=10+10+130+10+10=170;

console.log(oContainer.offsetLeft);

console.log(aBoxes[0].offsetLeft);

console.log(aBoxes[1].offsetLeft);输出结果为:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Document</title><style type="text/css">*{margin: 0;padding: 0;}body{//position: relative;margin:10px;padding: 10px;border: 5px solid black;}#container{position: relative;left: 100px;margin: 10px;padding: 10px;border: 5px solid #ccc;background: red;width: 300px;height: 300px;//overflow: hidden;}.box{position: relative;float: left;margin: 10px;padding: 10px;border: 5px solid black;background: orange;width: 100px;height: 100px;}.content{margin: 10px;padding: 10px;border: 5px solid black;background: yellow;width: 50px;height: 50px;}</style>
</head>
<body><div id="container">            <div class="box"><div class="content"></div></div><div class="box"><div class="content"></div></div></div><script type="text/javascript">var oContainer=document.getElementById("container");var aBoxes=getElementsByClassName(oContainer,"box");console.log("container.offsetWidth="+oContainer.offsetWidth);console.log("box.offsetWidth="+aBoxes[0].offsetWidth);//console.log(aBoxes[1].firstElementChild.offsetWidth);//console.log(document.body.offsetLeft);console.log("container.offsetLeft="+oContainer.offsetLeft);console.log("box1.offsetLeft="+aBoxes[0].offsetLeft);console.log("box2.offsetLeft="+aBoxes[1].offsetLeft);console.log("content.offsetLeft="+aBoxes[1].firstElementChild.offsetLeft);// alert(document.body.offsetLeft);// alert(oContainer.offsetLeft);// alert(aBoxes[0].offsetLeft);// alert(aBoxes[1].offsetLeft);// alert(aBoxes[1].firstElementChild.offsetLeft);function getElementsByClassName(parent,className){var allChildren=parent.getElementsByTagName("*");var arr=[];for(var i=0;i<allChildren.length;i++){if (allChildren[i].className==className) {arr.push(allChildren[i]);               }}      return arr;         }</script></body>
</html>

小解:offsetWidth与offsetLeft相关推荐

  1. JS——offsetWidth与offsetLeft用法之无缝滚动

    知识讲解: (1)offsetWidth是什么? 答:它可以获取物体宽度的数值:offsetWidth实际获取的是盒模型(width+border + padding)的宽度. 如图: (2)offs ...

  2. 对于offsetWidth,offsetHeight,offsetLeft,offsetTop的理解

    如何理解区分offsetWidth,offsetHeight,offsetLeft,offsetTop 在刚开始使用的时候总是不能理解这四个有什么不一样,用在哪里,因为这几个词真的太像了,emmm,总 ...

  3. offsetWidth与offsetLeft

    1.offsetWidth: 为元素的width+元素的padding+边框的宽度 如图: 2.offsetLeft.offsetTop.offsetRight.offsetBottom 以offse ...

  4. [绍棠] scrollWidth,clientWidth,offsetWidth的区别

    说明: scrollWidth:对象的实际内容的宽度,不包边线宽度,会随对象中内容超过可视区后而变大. clientWidth:对象内容的可视区的宽度,不包滚动条等边线,会随对象显示大小的变化而改变. ...

  5. 分享下offsetLeft和clientLeft和scrollLeft区别的总结

    在做前端的时候很多时间都会用到offset,clientLeft,scrollLeft-类似这样的属性,他们到底有什么区别呢?他们分别是指哪里呢?我们一起来分析分析. 1.offsetWidth,of ...

  6. js中的offsetLeft和style.left

    (1)style.left是带单位"px"的,而offsetLeft没有单位,另外,style.left必须是内联样式,或者在JS中通过style.left赋值,否则取得的将为空字 ...

  7. JS写的不咋地的碰撞检测

    最近在学习碰撞检测相关的知识,但说实话,写的不咋地.但是因为鄙人学识浅薄,所以觉得基本上还行,但是挺追求我完美的,所以想拿出来让大神们批评批评. 先来看一下效果 感觉哇,真卡,真难受.因为本来是正方形 ...

  8. 深入理解定位父级offsetParent及偏移大小

    偏移量(offset dimension)是javascript中的一个重要的概念.涉及到偏移量的主要是offsetLeft.offsetTop.offsetHeight.offsetWidth这四个 ...

  9. 基于js鼠标拖动图片排序

    分享一款基于js的图片排序效果.鼠标拖动图片,重新排列图片的排列顺序.该插件适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗.效果图如下: 在线 ...

最新文章

  1. PHP对于浮点型的数据需要用不同的方法去解决
  2. pom.xml中的dependencyManagement
  3. Windows 服务器开机自启动
  4. 单元测试中,模拟一个新对象
  5. 【IOS 开发】Object - C 面向对象 - 类 , 对象 , 成员变量 , 成员方法
  6. maven下载包慢解决
  7. elementui select 赋值后,回显没有默认选中label,而是显示value值
  8. java单例模式(饿汉式和懒汉式的几种不同写法)
  9. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) C. XOR Equation 数学
  10. Xcode 自定义代码块及代码块迁移
  11. 腾讯新出了一款音乐app,它能代替QQ音乐吗?
  12. php自动填表单,网页自动填表基础教程从简单开始
  13. Jprofiler激活码
  14. 怎么写加密邮件,企业邮箱支持吗?【企业邮箱注册】
  15. SNF快速开发平台2019-权限管理模型-记录级-字段级权限实践
  16. python system interpreter_2. Using the Python Interpreter:使用Python解释器
  17. 未能加载文件或程序集“FastReport
  18. 即插即涨2-3%!AC-FPN:用于目标检测的注意力引导上下文的特征金字塔网络
  19. 升级宽带到500M光纤
  20. 计算机无法共享打印,解决win7电脑打印机无法共享|打印机共享提示0x000006d9应如何解决...

热门文章

  1. 拨号被远程计算机终止出现619,网络错误代码大全.doc
  2. mysql 无法启动14001_Mysql服务无法启动,解决办法。
  3. 领峰:炒黄金要如何做好止损呢
  4. 空间分析:2-4.Python生成泰森多边形
  5. 微信公众号的注册(实例:订阅号)
  6. 氢燃料电池汽车的前景、主要优势及发展难点
  7. IT历史: 键盘发展历史
  8. VR全景-720全景项目加盟,市场开展方式
  9. 基于地理因式分解法的POI推荐排序算法(Ranking based Geographical Factorization Method,Rank-GeoFM)
  10. oracle的小不点