How to get the current position of an element relative to the window

Mohammad Fared

Is there any way to get the current position of an element relative to the window, not document

offset get the current position of an element relative to the document

Rounin

To calculate where the element is positioned relative to the top edge of the viewport, you can use a combination of:

  • getClientBoundingRect() (to determine the position of the element within the document); and
  • window.scrollY (to determine the vertical scroll position of the window).

Then, simply subtract the second value from the first:

var button = document.getElementsByTagName('button')[0];

var div = document.getElementsByTagName('div')[0];
var divRect = div.getBoundingClientRect();

function alertCurrentPosition() {
    var windowVerticalScroll = window.scrollY;
    window.alert('The top of the red square is ' + (divRect.top - windowVerticalScroll) + ' pixels below the top of the viewport');
}

button.addEventListener('click', alertCurrentPosition, false);
body {
height: 1200px;
}

button {
position: fixed;
top: 6px;
left: 6px;
}

div {
position: absolute;
top: 300px;
left: 50%;
width: 100px;
height: 100px;
background-color: rgb(255,0,0);
}
<button>Scroll the Viewport and Click Me</button>
<div></div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get element position relative to parent?

From Dev

How to get the current position of an element in angularjs

From Dev

How can I get the position of an element relative to the broswer viewport

From Dev

How can I get the position of the current focused window?

From Dev

Get position of current Hovered element

From Dev

Get position of current Hovered element

From Dev

How to position a relative element after a fixed element

From Dev

Get an element position relative to the top of the viewport

From Dev

How do I get the offset of an element from the current screen position?

From Dev

How to position topLevel() widget relative to root window?

From Dev

How does work position sticky relative window?

From Dev

Translate an element every time relative to its current position?

From Dev

Get the current window position under Windows

From Dev

Get the position from top of current window

From Dev

How to use absolute position relative to parent element

From Dev

How to position an element relative to the background image width

From Dev

How to have fixed position of element but relative to container

From Dev

How to use absolute position relative to parent element

From Dev

How to position an element relative to the background image width

From Dev

How do i position this element relative to an image?

From Dev

Position Relative to the window

From Dev

JS Get Current Position of Animated Element

From Java

How to get current relative directory of your Makefile?

From Dev

How to get relative path from current folder?

From Dev

How do i position a div relative to the window page (responsive css)

From Dev

How to get current element attribute

From Dev

How to get the position of SVG element

From Dev

How to get position of the clicked element

From Dev

How to get position of element in ArrayList

Related Related

  1. 1

    How to get element position relative to parent?

  2. 2

    How to get the current position of an element in angularjs

  3. 3

    How can I get the position of an element relative to the broswer viewport

  4. 4

    How can I get the position of the current focused window?

  5. 5

    Get position of current Hovered element

  6. 6

    Get position of current Hovered element

  7. 7

    How to position a relative element after a fixed element

  8. 8

    Get an element position relative to the top of the viewport

  9. 9

    How do I get the offset of an element from the current screen position?

  10. 10

    How to position topLevel() widget relative to root window?

  11. 11

    How does work position sticky relative window?

  12. 12

    Translate an element every time relative to its current position?

  13. 13

    Get the current window position under Windows

  14. 14

    Get the position from top of current window

  15. 15

    How to use absolute position relative to parent element

  16. 16

    How to position an element relative to the background image width

  17. 17

    How to have fixed position of element but relative to container

  18. 18

    How to use absolute position relative to parent element

  19. 19

    How to position an element relative to the background image width

  20. 20

    How do i position this element relative to an image?

  21. 21

    Position Relative to the window

  22. 22

    JS Get Current Position of Animated Element

  23. 23

    How to get current relative directory of your Makefile?

  24. 24

    How to get relative path from current folder?

  25. 25

    How do i position a div relative to the window page (responsive css)

  26. 26

    How to get current element attribute

  27. 27

    How to get the position of SVG element

  28. 28

    How to get position of the clicked element

  29. 29

    How to get position of element in ArrayList

HotTag

Archive