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

I have some elements arranged through CSS as follows:

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

I would like to get the distance from the top and left margins of the page . How can I get this measure using javascript / jquery?

thank

+3
source share
2 answers

Look at

jQuery

. position ()

and

. offset ()

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

$("#myItem").offset().top;
+6
source

You can use .offset()for this:

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

You can try it here .

+5
source

Source: https://habr.com/ru/post/1766818/


All Articles