开发者

How to get the "absolute" position of an html element

开发者 https://www.devze.com 2023-01-18 06:34 出处:网络
I have some elements positioned via CSS th开发者_如何学Pythonis way: #myItem{ position: absolute;

I have some elements positioned via CSS th开发者_如何学Pythonis 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


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

精彩评论

暂无评论...
验证码 换一张
取 消