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:
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.
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.
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.
1 gru 2016 · Take a look at the following properties: window.screen.height: Returns the height of the screen in pixels. window.screen.top: Returns the distance in pixels from the top side of the current screen. window.screen.width: Returns the width of the screen. window.screen.left: Returns the distance in pixels from the left side of the main screen to ...