asfman
android developer
posts - 90,  comments - 213,  trackbacks - 0
function getPosition(o)
{
 var temp={};
 temp.left=temp.right=temp.top=temp.bottom=0;
 var oWidth=o.offsetWith,oHeight=o.offsetheight;
 while(o!=document.body)
 {
  temp.left+=o.offsetLeft;
  temp.top+=o.offsetTop;
  var border=parseInt(o.offsetParent.currentStyle.borderWidth);
  if(border)
  {
   temp.left+=border;
   temp.top+=border;
  }
  o=o.offsetParent;
 }
 temp.right=temp.left+oWidth;
 temp.bottom=temp.top+temp.oHeight;
 return temp;
}

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页 5</title>
</head>
<div id="asfman" style="position:absolute;width:60px;height:60px;border:2px solid red;"></div>

<body id="b">
<div id="d2" style="position:absolute;left:100px;top:100px;border:1px solid red;width:100px;height:100px">
<div  id="d1"style="position:relative;left:20px;top:20px;margin:10px;border:15px solid green;width:50px; height:50px;padding:3px;">
<div id="d3" style="margin:8px;position:relative;left:0px;top:0px;border:10px solid green;width:20px; height:20px;">

</div>
</div>
</div>
<script>
function getPosition(o)
{
    
var temp={};
    temp.left
=temp.right=temp.top=temp.bottom=0;
    
var oWidth=o.offsetWith,oHeight=o.offsetheight;
    
while(o!=document.body)
    
{
        temp.left
+=o.offsetLeft;
        temp.top
+=o.offsetTop;
        
var border=parseInt(o.offsetParent.currentStyle.borderWidth);
        
if(border)
        
{
            temp.left
+=border;
            temp.top
+=border;
        }

        o
=o.offsetParent;
    }

    temp.right
=temp.left+oWidth;
    temp.bottom
=temp.top+temp.oHeight;
    
return temp;
}

asfman.style.top
=getPosition(d3).top;
asfman.style.left
=getPosition(d3).left;
</script>
</body>

</html>
posted on 2007-04-26 14:29 汪杰 阅读(242) 评论(1)  编辑 收藏 引用 所属分类: javascript

FeedBack:
# re: 获取位置
2007-05-06 16:07 | 汪杰
note:offsetLeft包括padding但不包括border  回复  更多评论
  
只有注册用户登录后才能发表评论。

<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(15)

随笔分类(1)

随笔档案(90)

文章分类(727)

文章档案(712)

相册

收藏夹

http://blog.csdn.net/prodigynonsense

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 457604
  • 排名 - 6

最新随笔

最新评论

阅读排行榜

评论排行榜