👀 Live Preview
Move the mouse inside the box — coordinates update live:
X: —, Y: —

The onmousemove attribute is an inline event handler for the mousemove event. It runs JavaScript repeatedly while the mouse pointer moves over an element. Use it to track coordinates, draw on a canvas, move a tooltip, or update a slider preview. Pass event to read clientX and clientY. It is not the same as onmouseover (pointer enters) — mousemove fires on every small movement while inside the element. Because it can fire dozens of times per second, keep handler code light or throttle it.
Inline JS.
While moving.
clientX/Y.
Throttle.
Preferred way.
Move vs enter.
onmousemove AttributeThe primary purpose of onmousemove is to react to continuous pointer movement over an element. Common uses include live coordinate readouts, custom cursors, drawing apps, image magnifiers, and drag operations (often paired with onmousedown on the same element or document).
The event bubbles, so a handler on a parent can receive movements from children — but the handler only runs while the pointer is over elements in that subtree (unless you attach to document during a drag).
mousemove can fire many times per second. Avoid heavy DOM updates or network calls on every event — use throttling or requestAnimationFrame.
Set onmousemove on any element that should track movement:
<div onmousemove="trackMouse(event)">
Move the mouse here
</div>
<canvas width="400" height="200" onmousemove="draw(event)"></canvas>mousemove while over the element.div, canvas, button, etc.event for clientX, clientY, offsetX, offsetY.document).el.addEventListener("mousemove", handler).onmouseleave or onmouseout to reset UI when pointer exits.The onmousemove attribute accepts a string of JavaScript code:
onmousemove="trackMouse(event)" — Pass the event object.onmousemove="updateCoords(event.clientX, event.clientY)" — Pass coordinates.box.onmousemove = (e) => { … } — property assignment.area.addEventListener("mousemove", (event) => {
// Viewport coordinates
const x = event.clientX;
const y = event.clientY;
// Relative to the target element
const localX = event.offsetX;
const localY = event.offsetY;
label.textContent = `X: ${x}, Y: ${y} (local: ${localX}, ${localY})`;
});| Property / event | Meaning | Notes |
|---|---|---|
mousemove | Pointer moved over element | Fires continuously |
event.clientX / clientY | Viewport position | Most common |
event.offsetX / offsetY | Relative to target | Inside the box |
mouseover | Pointer entered | Not every move |
| Handler attribute | onmousemove | Inline on element |
| Target | Supported? | Notes |
|---|---|---|
<div>, <span> | Yes | Tracking zones, tooltips |
<canvas> | Yes | Drawing, games |
<button> | Yes | Rare; prefer CSS hover |
<img> | Yes | Image maps, magnifiers |
document / window | Via JS | Global tracking in script |
onmousemove vs onmouseover vs onmouseenter| Handler | When it fires | Typical use |
|---|---|---|
onmousemove | While pointer moves over element | Coordinates, drawing, drag |
onmouseover | Pointer enters element (bubbles) | Hover highlight |
onmouseenter | Pointer enters (no bubble from children) | Clean enter detection |
onmousedown | Button pressed down | Start drag with move |
Track coordinates inline, addEventListener on a zone, and read offsetX/offsetY relative to the box.
Move the mouse inside the box — coordinates update live:
X: —, Y: —
Display viewport coordinates while moving over a div:
<div onmousemove="trackMouse(event)">
Move the mouse over this box.
</div>
<p id="output"></p>
<script>
function trackMouse(event) {
document.getElementById("output").textContent =
"Mouse coordinates: X = " + event.clientX +
", Y = " + event.clientY;
}
</script>Each pixel of movement over the div fires mousemove. The handler reads clientX and clientY from the event.
Attach with addEventListener and update the box text:
<div id="dynamicElement">Hover over me!</div>
<script>
document.getElementById("dynamicElement").addEventListener("mousemove", function (event) {
this.textContent = "X: " + event.clientX + ", Y: " + event.clientY;
});
</script>Register once in script. With a non-arrow function, this is the element receiving the event.
Position relative to the target element — useful inside a box or canvas:
zone.addEventListener("mousemove", (event) => {
dot.style.left = event.offsetX + "px";
dot.style.top = event.offsetY + "px";
label.textContent =
"Local: " + event.offsetX + ", " + event.offsetY;
});offsetX and offsetY are measured from the padding edge of the target element — handy for drawing and hit detection.
mousemove.prefers-reduced-motion before animating elements that follow the cursor.User moves mouse.
Many times per sec.
onmousemove code.
Coords, draw, drag.
The mousemove event and onmousemove handler are supported in all browsers, including legacy Internet Explorer.
onmousemove supportEvery major browser fires mousemove while the pointer moves over elements.
Bottom line: Safe for coordinate tracking and interactive zones in any browser.
event and use clientX / clientYaddEventListener("mousemove", …) in productionmouseleave to reset UI when pointer exitsoffsetX / offsetY for position inside a boxconsole in user-facing demosThe onmousemove attribute runs JavaScript continuously while the pointer moves over an element — perfect for coordinates, drawing, and interactive tracking.
Pass the event object, throttle heavy logic, and distinguish mousemove from mouseover when designing hover and drag experiences.
onmousemoveBookmark these before tracking the pointer.
Not on enter.
EventViewport.
APIThrottle.
PerfDifferent.
CompareNot mouse-only.
A11ymouseover fires when the pointer enters. mousemove fires on every movement while inside.event.clientX and event.clientY for viewport coords, or event.offsetX / offsetY relative to the element.addEventListener("mousemove", handler) is preferred for production code.Practice the onmousemove attribute with live coordinate examples in the Try It editor.
5 people found this page helpful