views:

58

answers:

2

Hi guys, 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

+4  A: 

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;
fehays
`.position()` is not correct for this, it's relative to the offset parent, not the page like the OP is after.
Nick Craver
@Nick, indeed. Thanks for pointing that out. I did still want to inform the poster of both methods.
fehays
+4  A: 

You can use .offset() for this:

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

You can give it a try here.

Nick Craver