00 Votes

jQuery: Determine absolute position of an element from the top

Question by Guest | 2013-09-22 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
0Best Answer0 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.
2013-09-25 at 18:56

ReplyPositive Negative
Reply

Related Topics

jQuery: Show and hide elements

Tutorial | 0 Comments

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 article on askingbox.com. That’s how it’s done.