Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the top position of an element?

Is it possible to get the top position of an element using javascript/jquery ?

The element is a table, if that matters.

like image 269
eKek0 Avatar asked Jul 09 '09 00:07

eKek0


People also ask

How do you find the top position of an element?

$("#myTable"). offset(). top; This will give you the computed offset (relative to document) of any object.

How do you get an element's top position relative to the browser's viewport?

We can use the getBoundingClientRect method to get an element's position relative to its viewport. We get the div with querySelector . Then we call getBoundingClientRect on it to get an object with the top and left properties. top has the position relative to the top of the viewport.

What is offset () Top?

The offsetTop property returns the top position (in pixels) relative to the parent. The returned value includes: the top position, and margin of the element. the top padding, scrollbar and border of the parent.


2 Answers

If you want the position relative to the document then:

$("#myTable").offset().top;

but often you will want the position relative to the closest positioned parent:

$("#myTable").position().top;
like image 192
Prestaul Avatar answered Oct 17 '22 21:10

Prestaul


$("#myTable").offset().top;

This will give you the computed offset (relative to document) of any object.

like image 13
xandy Avatar answered Oct 17 '22 22:10

xandy