0 0 Votes

jQuery: Determine absolute position of an element from the top

Question by Guest | 22/09/2013 at 22:00

I have a DIV container and would like to determine the absolute positioning of this element measured from the upper side of the web site using jQuery. So almost the top position of the container.

Is this possible with jQuery? 

ReplyPositiveNegative

Stefan Trost

Show Profile | Message
Avatar
0Best Answer
0 Votes

Yes, it is. You can simply use the method .offset() giving us the current position of an element relative to the document. Offset provides us with your desired top position with .offset().top and the position from the left with .offset().left.

var a = $('#mydiv').offset().top;
var b = $('#mydiv').offset().left;

In this small example, we set the variable "a" to the top position of the container with the ID "mydiv" and "b" to the left-position of the same container.

If you are using the method .position() instead, you can determine the position relative to the parent element.
25/09/2013 at 18:56

ReplyPositive Negative
Reply

Related Topics

jQuery: Replace element
Question | 1 Answer

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own articles on askingbox.com. How to do.