求:js 返回dom对象的css的left和top值的代码?

fxs_2008 2010-12-01 03:00:55
求:js 返回dom对象的css的left和top值的代码?
jquery有这个方法,但不值纯js是如何得到的。

试了一下,不行。

...全文
237 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Study_Work_2009 2010-12-04
  • 打赏
  • 举报
回复
var top = parseInt(document.getElementById('id').offsetTop)+parseInt(document.body.scrollTop);
var left = parseInt(document.getElementById('id').offsetLeft);
sohighthesky 2010-12-03
  • 打赏
  • 举报
回复
var getPos:function(o){//取元素坐标
var x = 0, y = 0;
do{
x += o.offsetLeft;
y += o.offsetTop;
}while(o=o.offsetParent);
return {'x':x,'y':y};
}
chinahnzl 2010-12-03
  • 打赏
  • 举报
回复
var top =parseInt( document.getElementById('id').style.top);
var left =parseInt( document.getElementById('id').style.left);
persuit666 2010-12-03
  • 打赏
  • 举报
回复
可以参考getboundingclientrect()方法
hch126163 2010-12-03
  • 打赏
  • 举报
回复
var top =parseInt( document.getElementById('id').style.top);
var left =parseInt( document.getElementById('id').style.left);
仲兴轩 2010-12-03
  • 打赏
  • 举报
回复
JQuery

$("#id").css("left");

$("#id").css("top");

不行?
zyzy15 2010-12-01
  • 打赏
  • 举报
回复

<style>
.inp {position:absolute; left:50px; top:100px}
</style>
<INPUT TYPE="text" NAME="inp_1" id="inp_1" value="abc" class="inp">
<INPUT TYPE="button" VALUE="Click" ONCLICK="fun()">
<SCRIPT LANGUAGE="JavaScript">
function fun(){
var inp1 = document.getElementById("inp_1");
alert("left:"+inp1.currentStyle.left+"; top:"+inp1.currentStyle.top);
}
</SCRIPT>

猿敲月下码 2010-12-01
  • 打赏
  • 举报
回复
var top = document.getElementById('id').offsetTop+document.body.scrollTop
var left = document.getElementById('id').offsetLeft;

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧