做为走上前端不归路的我,以前只是认为offsetTop是元素的左边框至包含元素offsetParent的左内边框之间的像素距离,同理offsetRight是相对于上内边框。那么问题来了,包含元素offsetParent究竟是谁呢?

是我太天真还是天后知后觉!

其实:

页面中的元素的offsetLeft是离其最近的已经定位的元素,如果没有就相对于body元素计算。例如:

<style type="text/css">
.box1{
width: 100px;
height: 100px;
background: red;
}
#box2{
width: 50px;
height: 50px;
background: green;
margin: 0 auto;
}
</style>
<div class="box1">
<div id="box2"> </div>
</div>
<script type="text/javascript">
var box2 = document.getElementById('box2');
alert(box2.offsetParent.tagName);//输出BODY
alert(box2.offsetLeft);//输出从33
</script>

此时id为box2的div元素的祖先元素中没有已经定位的元素,所以box2的offsetLeft是相对于body元素计算所得的。

下面我们将上面的代码做如下修改

<style type="text/css">
.box1{
width: 100px;
height: 100px;
background: red;
position:relative;
}
#box2{
width: 50px;
height: 50px;
background: green;
margin: 0 auto;
}
</style>
<div class="box1">
<div id="box2"> </div>
</div>
<script type="text/javascript">
var box2 = document.getElementById('box2');
alert(box2.offsetParent.tagName);//输出DIV
alert(box2.offsetLeft);//输出从25
</script>

offsetTop同理。

通过查阅资料我还发现 IE6,IE7 对offsetParent解释有BUG,当祖先元素都不是定位元素且本身是定位元素的时候返回document.documentElement,其他情况终返回document.body:

<body>
<div id="b" style="position:relative">
<div id="a"></div>
</div><!-- a.offsetParent返回b -->
  <div id="d">
     <div id="c"></div>
  </div><!-- c.offsetParent返回document.body -->
<div id="f">
<div id="e" style="position:relative"></div>
</div><!-- e.fsetParent返回document.body 在ie6,ie7中返回document.documentElement -->
</body>

深入理解offsetTop与offsetLeft的更多相关文章

  1. offsetTop、offsetLeft、offsetWidth、offsetHeight的用法

    假设 obj 为某个 HTML 控件. obj.offsetTop 指 obj 相对于版面或由 offsetParent 属性指定的父坐标的计算上侧位置,整型,单位像素. obj.offsetLeft ...

  2. 获取到body的offsetTop和offsetLeft值

    function offsetTL(obj){//获取到body的offsetTop和offsetLeft var t=0,l=0; while(obj){ t=t+obj.offsetTop; l= ...

  3. offsetParent、offsetTop、offsetLeft、offsetWidth、offsetHeight

    w3c规范,请戳这里:http://www.w3.org/TR/cssom-view/#dom-htmlelement-offsetparent 一.offsetParent 英文解读: part o ...

  4. 扒一扒offsetleft,srollleft,pagex,clientx,postion().left等精确位置的获取与理解

    先上个pc端和手机端的图:   说明:上面的属性,都是in这个div的属性值.我是点击的in这个div的左上角,所以pageX.pageY是40. HTML: <div class=" ...

  5. 一文看懂js中元素偏移量(offsetLeft,offsetTop,offsetWidth,offsetHeight)

    偏移量(offset dimension) 偏移量:包括元素在屏幕上占用的所有可见空间,元素的可见大小有其高度,宽度决定,包括所有内边距,滚动条和边框大小(注意,不包括外边距). 以下4个属性可以获取 ...

  6. JS-scrollTop、scrollHeight、clientTop、clientHeight、offsetTop、offsetHeight的理解

    scrollTop, 可写(这些属性中唯一一个可写的元素) Element.scrollTop 属性可以获取或设置一个元素的内容垂直滚动的像素数. 一个元素的 scrollTop 值是这个元素的顶部到 ...

  7. js中top、clientTop、scrollTop、offsetTop的区别 文字详细说明版【转】

    之前在修改IE6下的一个显示bug时,查到过这些,贴这备忘,后面给出了详细说明,以前的版本,没仔细的说明,特希望大家也收藏下.   网页可见区域宽: document.body.clientWidth ...

  8. clientTop、offsetTop和scrollTop的区分

    页可见区域宽: document.body.clientWidth; 网页可见区域高: document.body.clientHeight; 网页可见区域宽: document.body.offse ...

  9. offsetLeft,Left,clientLeft的区别

    offsetLeft,Left,clientLeft的区别 假设 obj 为某个 HTML 控件. obj.offsetTop 指 obj 相对于版面或由 offsetParent 属性指定的父坐标的 ...

随机推荐

  1. MYSQL查看和修改存储引擎

    最近,公司的BBS压力越来越大,表死锁越来越严重. 在其它优化同作的情况下,MYISAM引擎转为INNODB引擎也纳入计划当中. 参考URL如下:简单,但要注意备份... ============ h ...

  2. Yii框架下不同contoller之间的方法调用

    一个contoller CustomerController里怎么调用另一个controller里的action,Acontoller调用SiteContoller的actionShow($id), ...

  3. 数学(莫比乌斯函数):BZOJ 2440 完全平方数

    Description 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平方数.他觉得这些 数看起来很令人难受.由此,他也讨厌所有是完全平方数的正整数倍的数.然而 这丝毫不影响他对其他数的热爱. 这 ...

  4. 并查集(逆序处理):HDU 5652 India and China Origins

    India and China Origins Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  5. 【动态规划】HDU 5492 Find a path (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5492 题目大意: 一个N*M的矩阵,一个人从(1,1)走到(N,M),每次只能向下或向右走.求(N+ ...

  6. WPF的登录界面的排版

    一:截图 二:XAML代码 <Window x:Class="wpf练习.登录窗体" xmlns="http://schemas.microsoft.com/win ...

  7. Android Service命令

    service可给Android 服务传消息,具体用法如下: Usage: service [-h|-?]        service list        service check SERVI ...

  8. UTF8与GBK、GB2312等其他字符编码的相互转换

    utf8与其他字符编码的转换是国际化必然遇到的问题. windows的api函数对: WideCharToMultiByte和MultiByteToWideChar 个人喜欢的跨平台库: 单纯做字符编 ...

  9. tyvj P1431 [Tyvj Jan]分配任务(最大流)

    P1431 [Tyvj Jan]分配任务 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述      随着tyvj发展越来越大,管理员的任务越来越重,如何合理的 ...

  10. VMware连不上网解决

    在VMware里安装了ubuntu,但是某一天打开它,ubuntu忽然不能上网了,于是百度了好几个方法,最后是这样解决的: 额...现在连接的是无线,前两天出问题的时候连接的是有线,在Internet ...