做为走上前端不归路的我,以前只是认为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. java实战之数组工具集

    java是一门面向对象的语言,我们也提到过,面向对象的一个优点就在于能够提高代码的复用性,前面我们详细讲过数组的查找,排序,等等,为了提高代码的复用性,我们何不自己写一个数组的工具集,来综合下前面所学 ...

  2. Sublime Text 3 配置

    配置: { "font_face": "Source Code Pro", , "highlight_line": true, " ...

  3. 动态规划(水题):COGS 261. [NOI1997] 积木游戏

    261. [NOI1997] 积木游戏 ★★   输入文件:buildinggame.in   输出文件:buildinggame.out   简单对比时间限制:1 s   内存限制:128 MB S ...

  4. DateDiff 函数,用生日获得年龄

    一:截图 二:代码 using System; using System.Collections.Generic; using System.ComponentModel; using System. ...

  5. 《Mathematical Olympiad——组合数学》——抽屉原理

    抽屉原理可以说是组合数学中最简单易懂的一个原理了,其最简单最原始的一个表达形式:对于n本书放到n-1个抽屉中,保证每个抽屉都要有书,则必存在一个抽屉中有2本书.但是这个简单的原理在很多问题中都能够巧妙 ...

  6. the identity used to sign the executable is no longer valid.解决方法

    the identity used to sign the executable is no longer valid.解决方法 一.重新下载Provisioning Profile 1.到devel ...

  7. Maven deploy

      1.在maven/conf/setting.xml中: 在<servers>中加入 <server> <id>internal</id> <u ...

  8. Solr多核的配置

    Solr 多核(MultiCore)配置 Solr Multicore意义 Solr Multicore 是 solr 1.3 的新特性.其目的一个solr实例,可以有多个搜索应用.< xmln ...

  9. 【转】AngularJs $location获取url参数

    // 带#号的url,看?号的url,见下面 url = http://qiaole.sinaapp.com?#name=cccccc $location.absUrl(); // http://qi ...

  10. adb logcat命令查看并过滤android输出log

    cmd命令行中使用adb logcat命令查看android系统和应用的log,dos窗口按ctrl+c中断输出log记录. logcat日志中的优先级/tag标记: android输出的每一条日志都 ...