简体   繁体   中英

How to get the “absolute” position of an html element

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM