Search results
14 sty 2009 · The correct approach is to use element.getBoundingClientRect(): var rect = element.getBoundingClientRect(); console.log(rect.top, rect.right, rect.bottom, rect.left); Internet Explorer has supported this since as long as you are likely to care about and it was finally standardized in CSSOM Views.
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:
16 mar 2016 · var myElement = document.querySelector("#foo"); var position = getPosition(myElement); alert("The image is located at: " + position.x + ", " + position.y); You should also ensure you update the various position values when your browser is scrolled or resized.
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.
2 lut 2024 · Use the Element.getBoundingClientRect() Function to Get the Position of an Element in JavaScript. Use the offsetTop Property to Get the Position of an Element in JavaScript. Every element in the HTML document is placed at a given position. The position here refers to the x and y coordinates of elements.
15 sty 2024 · If you want to get the absolute position of the element on the page, not relative to the window, add the number of scrolled pixels to the element’s window location using scrollY: Click to Copy console.log("X: " + (rect.x + globalThis.scrollX) + ", Y: " + (rect.y + globalThis.scrollY));
20 cze 2015 · function getPos(el) { // yay readability for (var lx=0, ly=0; el != null; lx += el.offsetLeft, ly += el.offsetTop, el = el.offsetParent); return {x: lx,y: ly}; } However, if you just wanted the x,y position of the element relative to its container, then all you need is: var x = el.offsetLeft, y = el.offsetTop;