最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get the "absolute" position of an html element - Stack Overflow

programmeradmin0浏览0评论

I have some elements positioned via CSS this way:

#myItem{
position: absolute;
left: 50%;
margin-left: -350px;
}

I'd like to get their distance from top and left margin of the page. How can I get those measure with javascript/jquery?

Thanks

I have some elements positioned via CSS this way:

#myItem{
position: absolute;
left: 50%;
margin-left: -350px;
}

I'd like to get their distance from top and left margin of the page. How can I get those measure with javascript/jquery?

Thanks

Share Improve this question asked Sep 27, 2010 at 17:38 ArtoAleArtoAle 2,9772 gold badges27 silver badges50 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Take a look at jQuery's

.position()

and

.offset()

EDIT: As mentioned by @Nick, .offset() is what you want if you need the position relative to the document

$("#myItem").offset().top;

You can use .offset() for this:

var offset = $("#myItem").offset();
//use offset.left, offset.top

You can give it a try here.

发布评论

评论列表(0)

  1. 暂无评论