Search results
13 paź 2016 · I'm hoping to find a way to get the current viewable window's position (relative to the total page width/height) so I can use it to force a scroll from one section to another. However, there seems ...
5 kwi 2023 · You can use the element.getBoundingClientRect () function to measure the position of an element in JavaScript. This function returns an object with eight properties: top, right, bottom, left, x, y, width, and height. These properties tell you the position and size of the element relative to the viewport:
Example. Open a new window with a specified left and top position, and return its coordinates: const myWin = window.open("", "", "left=700,top=350,width=200,height=100"); let x = myWin.screenX; let y = myWin.screenY; Try it Yourself ». More examples below.
16 mar 2016 · The code for getting the X and Y position of an HTML element is provided below: // Helper function to get an element's exact position. function getPosition(el) { var xPos = 0; var yPos = 0; while (el) { if (el.tagName == "BODY") { // deal with browser quirks with body/window/document and page scroll.
12 sie 2020 · To get the scroll position of the document, you can use the pageXOffset, pageYOffset properties of the window object or the scrollLeft and scrollTop properties of the documentElement object. Here is an example: const x = window. pageXOffset || document. documentElement. scrollLeft; const y = scrollTop = window. pageYOffset || document ...
13 gru 2020 · Getting the Position Data Calling 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.
var element = document.getElementById('myElement'); var topPos = element.getBoundingClientRect().top + window.scrollY; var leftPos = element.getBoundingClientRect().left + window.scrollX; That allows me to get the real relative position of element on document, even if it has been scrolled.