Search results
14 sty 2009 · To retrieve the position relative to the page efficiently, and without using a recursive function: (includes IE also) var element = document.getElementById('elementId'); //replace elementId with your element's Id. var rect = element.getBoundingClientRect(); var elementLeft,elementTop; //x and y.
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.
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:
26 lip 2024 · In more technical terms, scrollY returns the Y coordinate of the top edge of the current viewport. If the document isn't scrolled at all top or down, then scrollY is 0. If there is no viewport, the returned value is 0.
26 mar 2022 · We can use the getBoundingClientRect method to get an element’s position relative to its viewport. For instance, we can write: const div = document.querySelector('div') const {top: t, left: l} = div.getBoundingClientRect(); console.log(t, l) We get the div with querySelector.
12 paź 2021 · We’ve to add scrollX and scrollY to get the element's position relative to the whole page. For instance, we can write: const div = document.querySelector('div') const getOffset = (el) => {const rect = el.getBoundingClientRect(); return {left: rect.left + window.scrollX, top: rect.top + window.scrollY};} console.log(getOffset(div));
22 gru 2015 · var elmTop = $elem.getBoundingClientRect().top + window.scrollY; element.getBoundingClientRect() gives the position within the viewport, and window.scrollY gives the distance from the top of the viewport to the top of the document.