Jquery get element top position relative to window năm 2024

jQuery's .offset() method is pretty close to what we want, except it gets the offset coordinates for an object in relation to the whole document. What we'd want is to couple this with the .scrollTop() and .scrollLeft() methods.

.scrollTop() will give you the number of pixels scrolled from the top of an object, so you basically want to take the difference of the scrollTop of the document and the actual offset top coordinate to get the viewport top offset.

coffeescript example:

photo = $('
# photo')
offset = photo.offset()
photoViewportOffsetTop = offset.top - $(document).scrollTop()
photoViewportOffsetLeft = offset.left - $(document).scrollLeft()

$(document).scrollTop()

Make sure you use $(document) as the object you're looking for the scrollTop() or scrollLeft() for, rather than the object itself, because the object might not be scrolled out of the frame. You want the offset of the viewport scrolled away from the document.

In order to get the location of an element relative to the document, jQuery offset() method is used. The offset() method is an inbuilt method in jQuery which is used to set or returns the offset coordinates of the selected element. We can also use the jQuery position() method. The position() method is an inbuilt method in jQuery which is used to find the position of the first matched element relative to its parent element in the DOM tree. Syntax:

$(selector).offset()

Below examples illustrate the above approach: Example 1:

< html >

< head >

< `0> `

`2 `3

`2 `5

`2 `7

`9 0> `

< <`4> `

`2<`7

<`8<`9

` `2`html`1

` `2`html`3

`<`8`html`5

` `2`html`1

`9<4> `

< > `4 > 5> `6

`9> 4> `

`9`head >

< <`5> `

< <`9`head`0<9> `

< `head`5`head`6

` `2`head`8

`2> 0> 1> `2

`2> `4

`9`head`5> `

< > `1 2> 1> `

< `7 ``8 9<0> `

`2< <4 > 5> `6

`9 7> `

< > `1 2> 1> `

< `09 10 9 12 13 09> `

< `18 19 18> `

< > `4> `

`2 `27

<`8 `29

`30 `31

`30 `33

`34 `35

<`8 `37

`2 `37

`9> 4> `

`9<5> `

`9`html >

Output:

Jquery get element top position relative to window năm 2024
Example 2:

< html >

< head >

< `0> `

`2 `3

`2 `64

`2 `7

`9 0> `

< <`4> `

`2<`7

<`8<`9

` `2`html`1

` `2`html`3

`<`8`html`5

` `2`html`1

`9<4> `

< > `4 > 5> `6

`9> 4> `

`9`head >

< <`5> `

< <`9`head`0<9> `

< `head`5`head`6

` `2`head`8

`2> 0> 1> `2

`2> `4

`9`head`5> `

< > `1 2> 1> `

< `7 ``8 9<0> `

`2< <4 > 5> `6

`9 7> `

< > `1 2> 1> `

< `09 10 9 12 13 09> `

< `18 19 18> `

< > `4> `

`2 `27

<`8<`78

`30<`80

`30<`82

`30<`84

<`8 `37

`2 `37

`9> 4> `

`9<5> `

`9`html >

Output:

Jquery get element top position relative to window năm 2024

Last Updated : 18 Mar, 2020

Like Article

Save Article

Share your thoughts in the comments

Please Login to comment...

How do you find the element position relative to a window?

Getting the Position DataCalling getBoundingClientRect() on an element will return data about its size and position relative to the viewport. The data is wrapped in a DOMRect object that provides the element's x and y positions in the viewport, and its width and height .

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.

How do you get the position of an element based on a viewport?

Use the getBoundingClientRect() method to get the size of the element and its relative position to the viewport. Compare the position of the element with the viewport height and width to check if the element is visible in the viewport or not.

How do you position relative to a viewport?

An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used to position the element. A fixed element does not leave a gap in the page where it would normally have been located.