<div style="position:absolute;left:50px;width:500px;background-color: red;height:100px">
<div style="position:relative;left:150px;width:100px;background-color:green;height:50px"></div>
</div>
<script>
var div=document.getElementsByTagName("div")[1];
function getOffset(elem){
var actualLeft=elem.offsetLeft;
var actualTop=elem.offsetTop;
var father=elem.offsetParent;//返回父级节点
while(father!=null){
actualLeft+=father.offsetLeft;//父节点的坐标
actualTop+=father.offsetTop;
father=father.offsetParent;//如果父级还有父级
}
return{
X:actualLeft,
Y:actualTop
}
}
获取元素的绝对定位 elem.offsetLeft
最新推荐文章于 2023-09-02 17:56:14 发布