What is clientX Javascript

Javascript • Position of an element in the browser window

Coordinates in the browser window

CSS also uses two coordinate systems: position fixed and position absolute. What is placed with position: absolute and top / left remains in its position in the document and scrolls with it when the page is scrolled. What is positioned with position: fixed is relative to the browser window and is firmly anchored there.

If the page has not been scrolled, the coordinates of an element in the document are the same as the coordinates of the browser window. The zero point of both coordinate systems is the same: top / left.

Size of the document

The HTML document is usually larger than the browser window - mostly higher (then the vertical scroll bars are created), but may also be wider than the browser window (then horizontal scroll bars are also created).

scrollHeight / scrollWidth vs innerHeight / innerWidth

For many years, jQuery was the reliable superset of Javascript. From IE 9, Vanilla Javascript returns values ​​such as the height and width of the browser window (innerHeight / innerWidth) and the rendered document (scrollHeight / scrollWidth) just as reliably.

jQueryVanilla Javascript $ (window) .scrollTop () window.pageYOffset $ (window) .scrollLeft () window.pageXOffset $ (document) .height () document.documentElement.scrollHeight $ (document) .width () document.documentElement.scrollwidth $ ( window) .height () window.innerHeight $ (window) .width () window.innerWidth

If we want to be very careful:

let w = window, d = document, e = d.documentElement, g = d.getElementsByTagName ('body') [0], x = w.innerWidth || e.clientWidth || g.clientWidth, y = w.innerHeight || e.clientHeight || g.clientHeight;

Source: Github window-height-width.js

let body = document.body, html = document.documentElement; hbox.innerHTML = "body.scrollHeight" + body.scrollHeight + "body.offsetHeight" + body.offsetHeight + "html.clientHeight" + html.clientHeight + "html.scrollHeight" + html.scrollHeight + html.offsetHeight;

Only IE10 delivers a different result offsetHeight - from IE11 all browsers are on the same line.

Position of an element in the document

Javascript has no standard method for reading the coordinates of an element - but it is unproblematic.

getBoundingClientRect returns the distance to the browser window at the top left in all modern browsers. Just add the height of the scrolled area and you're done.

function getCoords (elem) {let box = elem.getBoundingClientRect (); return {top: box.top + pageYOffset, left: box.left + pageXOffset}; }

Height and width of elements

offsetWidth and offsetHeight (integer)
provide the width and height of an element including padding and border (CSS border-box).
clientWidth and clientHeight (integer)
Visible part of the block content without border, scrollbar, but with padding.
scrollWidth and scrollHeight
Size of the block including the invisible parts outside the scroll area.
Tootsie roll powder gummies pastry icing donut chupa chups. Halvah cake chocolate toffee candy chocolate bar cotton candy caramels. Tiramisu biscuit wafer chocolate chupa chups dragée powder. Sugar plum jelly-o danish macaroon lollipop pastry. Jujubes cheesecake pudding lollipop muffin cookie lollipop liquorice. Lemon drops sesame snaps marzipan soufflé. Muffin donut icing apple pie muffin. Lollipop cookie candy. Cake marshmallow carrot cake chupa chups toffee candy sugar plum. Marshmallow chupa chups sweet roll muffin pie danish lemon drops ice cream chocolate cake. Sesame snaps tiramisu gummi bears danish bear claw pastry pie. Bear claw croissant marzipan cake oat cake pudding.
show.innerHTML = elem.offsetWidth; show.innerHTML = elem.offsetHeight; show.innerHTML = elem.clientHeight; show.innerHTML = elem.scrollHeight;

When a detailed calculation is required - offsetWidth and clientWidth are integers or whole numbers: element.getBoundingClientRect () returns the height and width of the element with decimal places.

Position of the mouse pointer

The position of the mouse pointer is not a property of the Javascript window object, but a property of the event.

The Javascript event object offers many properties for the position of the cursor - note that pageX / pageY act relative to the document, clientX / clientY relative to the browser window.

pageX / pageY
Horizontal / vertical position of the cursor from the upper left corner in relation to the document.
x / y
Equivalent to clientX, clientY
clientX / clientY
Horizontal / vertical position of the cursor from the upper left corner in relation to the browser window (aka viewport).
Position of the cursor in the document
box.innerHTML = "clientX / clientY" + eve.clientX + "" + eve.clientY; box.innerHTML + = "pageX / pageY" + eve.pageX + "" + eve.pageY; box.innerHTML + = "x / y" + eve.x + "" + eve.y;