CKTIS / JavaScript
Mouse coordinates: clientX, clientY, mousemove, and mouseleave
Read the cursor position relative to the viewport, update the interface as the mouse moves, and clear state when the cursor leaves an element and its descendants.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
element.addEventListener("mousemove", onMove);
element.addEventListener("mouseleave", onLeave);
// Inside a listener receiving a MouseEvent:
const x = event.clientX;
const y = event.clientY;clientX and clientY are read-only properties of a MouseEvent instance. They give the horizontal and vertical cursor position at the time of the event, in CSS pixels from the viewport’s top-left corner. These are not document, screen, or element coordinates. These are web-platform APIs, not part of ECMAScript 2024; availability depends on the runtime.
mousemove fires when the cursor moves over an element, even with no mouse button pressed. It can fire frequently, so keep its handler lightweight. It bubbles: when the event comes from a descendant, event.target can be that descendant, while event.currentTarget identifies the element with the listener during the handler.
mouseleave fires when the cursor leaves the element and all its descendants. Unlike mouseout, it does not bubble. Moving from an element into its descendant does not trigger mouseleave on that element. The DOM tree matters, not just the visible rectangle. Use this event to clear hover state, but clear that state yourself if the element is removed or replaced.
To get a position relative to the top-left corner of the element’s bounding rectangle, read rect with event.currentTarget.getBoundingClientRect() inside the handler and compute event.clientX - rect.left and event.clientY - rect.top. Both pairs use the same coordinate system, so do not add scroll offsets. Subtraction does not undo CSS transforms.
Examples
<!doctype html>
<meta charset="utf-8">
<title>Mouse event propagation</title>
<pre id="output"></pre>
<script>
(() => {
const output = document.getElementById("output");
const lines = [];
function log(line) {
lines.push(line);
console.log(line);
}
const area = document.createElement("div");
const child = document.createElement("span");
area.append(child);
area.addEventListener("mousemove", (event) => {
log(`move: ${event.clientX}, ${event.clientY}`);
});
area.addEventListener("mouseleave", () => log("leave"));
child.dispatchEvent(new MouseEvent("mousemove", {
bubbles: true, clientX: 24, clientY: 36
}));
child.dispatchEvent(new MouseEvent("mouseleave", { bubbles: false }));
area.dispatchEvent(new MouseEvent("mouseleave", { bubbles: false }));
output.textContent = lines.join("\n");
})();
</script>Result
move: 24, 36 leave
Save the example as an HTML file and open it in a browser. The elements are detached and the events are synthetic (`isTrusted` is `false`): they do not move the cursor or test which element is under it. The child’s `mousemove` reaches the parent. Only the `mouseleave` dispatched directly to the parent logs `leave`.
Related lessons
- JavaScript · Lesson 12 · Advanced · Version: 4
Sources
- HTML Standard — Event handlers
- MouseEvent: clientX property
- MouseEvent: clientY property
- Coordinate systems
- Element: mousemove event
- Element: mouseleave event
- Element: mouseout event
- Event: currentTarget property
- Element: getBoundingClientRect() method
- MouseEvent: MouseEvent() constructor
- EventTarget: dispatchEvent() method