您的位置:首页 > 运维架构

【转】js中offsetLeft,offsetTop,offsetParent详解

2016-07-21 12:21 573 查看
转载:http://www.android100.org/html/201405/23/13070.html

本文章来给各位同这介绍一下关于js中offsetLeft,offsetTop,offsetParent详解,如果你对此教程有兴趣不防进入参考一下。

为前端开发工程师,你确定真的理解offsetLeft,offsetTop以及offsetParent吗,今天小编在这里给大家详细说说这三个js的特性,当然前两个其特性是一样的,在这里我就只拿offsetLeft的说明。

1.offsetLeft/offsetTop

在页面任一元素的offsetLeft总是找到离其最近的已经定位的元素定位,如果没有,就根据根节点body定位,然后获取其left值例如
 代码如下复制代码
<div id=”div2″>

<div id=”div3″>

<div id=”div4″></div>

</div>

</div>
css:

#div2{width:400px;height:400px;background:#0f0;margin-left:10px}

#div3{width:300px;height:300px;background:#00f;margin-left:10px}

#div4{width:200px;height:200px;background:#000;margin-left:10px;}
div4的offsetLeft根据body定位:10+10+10=30

如果我们将css做如下改变
 代码如下复制代码
css:

#div2{width:400px;height:400px;background:#0f0;position:relative;margin-left:10px}

#div3{width:300px;height:300px;background:#00f;margin-left:10px}

#div4{width:200px;height:200px;background:#000;margin-left:10px;}
div4的offsetLeft根据div2定位:10+10=20

offsetTop同理

2.offsetParent

其实offsetParent,返回一个元素离其最近的已经定位的元素,如果没有就返回body,其概念和offsetLeft差不多

3.实例:封装一个函数获得任一元素在页面的位置
 代码如下复制代码
var GetPosition= function (obj)

{

var left = 0;

var top = 0;

while(obj.offsetParent)//如果obj的有最近的父级定位元素就继续

{

left += obj.offsetLeft;//累加

top += obj.offsetTop;

obj=obj.offsetParent;//更新obj,继续判断新的obj是否还有父级定位,然后继续累加

}

return {“left”:left,”top”:top}//返回json格式

}
 IE6,IE7 对offsetParent解释有个小BUG,当祖先元素都不是定位元素且本身是定位元素的时候返回document.documentElement,其他情况终返回document.body!!
 代码如下复制代码
<body>

    <div id="b" style="position:relative">

        <div id="a"></div>

    </div>
 
    <div id="d">

        <div id="c"></div>

    </div>
 
    <div id="f">

        <div id="e" style="position:relative"></div>

    </div>

</body>

 
a.offsetParent >> b

c.offsetParent >> document.body

e.offsetParent >> document.body  ( ie,6,7 >> document.documentElement )

 

按照这样理解可以推断elemnt.offsetParent本身在获取的时候要通过页面不断查找父元素,直到找到最近的定位元素或者document.body,在代码多层嵌套的时候,需要向上查找未知的层次。效率可见一斑~ 尤其是在某些浏览器下计算页面元素位置的时候引用elemnt.offsetParent element.offsetLeft element.offsetTop 三者进行循环计算的时候效率很低。

 elemnt.offsetLeft elemnt.offsetTop

 Summary: 返回相对elemnt.offsetParent的位置

 PS:IE8,IE9 Preview,Opera 和个别状况下的IE6,IE7返回的值包括elemnt.offsetParent的borderLeftWidth和borderTopWidth
 代码如下复制代码
<style type="text/css">

#b{position:relative; background:#E3EBF4; width:300px; height:300px; border:20px solid #628DC0 }

#a{position:absolute; background:#9E9E9E; width:100px; height:100px; left:100px; top:100px;}

</style>
 <div id="b">

<div id="a"></div>

</div>

 
document.getElementByIdx('a').offsetLeft >> 100

document.getElementByIdx('a').offsetTop >> 100

上面是正确的值,而在IE8和Opera里面均返回 120,如果对b的CSS做一些修改,如下:

 #b{position:relative; background:#E3EBF4; border:20px solid #628DC0 } 去掉width 和height,

 那么在ie6,7里面返回也出现了问题

 ie6:

document.getElementByIdx('a').offsetLeft >> 120

document.getElementByIdx('a').offsetTop  >> 100

 ie7:

document.getElementByIdx('a').offsetLeft >> 120

document.getElementByIdx('a').offsetTop  >> 120

 所以在IE8,Opera 以及特殊情况的IE6 IE7里面 如果通过elemnt.offsetParent element.offsetLeft element.offsetTop计算页面元素位置需要在每一次循环中减去borderLeftWidth和borderTopWidth 的值。当然目前来说除了Opera 9.2 以外均不需要考虑这种情况,因为其他Browser或者更高版本都支持 getBoundingClientRect()来计算获取页面元素位置。关于getBoundingClientRect()
来这里!

 上面的Demo在正常情况下绝大多数时候offsetLeft,offsetTop和left,top的值是相等的,但当a同时具有marging属性的时候offsetLeft,offsetTop均是包含marging值的,所以在模拟获得left top值的时候需要稍加注意!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: