The onmousemove attribute is an inline event handler for the mousemove event. It runs when a pointing device moves while its hotspot is inside the element — whether or not a mouse button is pressed.
Use it to read coordinates (clientX/clientY, offsetX/offsetY). For enter/leave, prefer onmouseover / onmouseout. For unified mouse/pen/touch tracking, see pointermove.
Baseline · Widely available.
Not deprecated and not experimental. Stable across modern browsers since about July 2015.
High-frequency event.
MDN: mousemove can fire very often. Keep handlers light — heavy work on every move can make the page feel slow.
Syntax
HTML Syntax
syntax.html
<element onmousemove="script">
script is JavaScript that runs while the pointer moves. Pass event to read coordinates. Script alternative: element.addEventListener("mousemove", handler).
Use offsetX / offsetY — distance from the target’s padding edge.
Top-left of the pad is near 0,0 (browser rounding may vary slightly).
Prefer this pair when mapping pointer position onto the element’s own surface.
Example 3 — Draw with mousedown + mousemove + mouseup
A minimal MDN-style sketch: draw only while the button is held.
HTML
<canvas id="board" width="320" height="180"></canvas>
<p>Hold the mouse button and move to draw.</p>
<script>
var board = document.getElementById("board");
var ctx = board.getContext("2d");
var drawing = false;
var x = 0;
var y = 0;
function line(x1, y1, x2, y2) {
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
board.addEventListener("mousedown", function (e) {
x = e.offsetX;
y = e.offsetY;
drawing = true;
});
board.addEventListener("mousemove", function (e) {
if (!drawing) return;
line(x, y, e.offsetX, e.offsetY);
x = e.offsetX;
y = e.offsetY;
});
window.addEventListener("mouseup", function () {
drawing = false;
});
</script>
Drag patterns: combine with mousedown and mouseup.
Performance: keep move handlers cheap.
One line:onmousemove tracks motion inside an element — choose the right coordinate system and keep the handler light.
Frequently Asked Questions
It is an inline event handler that runs JavaScript when the mousemove event fires — a pointing device moves while its hotspot is inside the element. It fires whether or not a button is pressed.
mousemove fires continuously while the pointer moves inside the element. mouseover fires when the pointer enters the element (and can bubble from children). For “entered this box,” mouseover/mouseenter is usually clearer; for tracking position while moving, use mousemove.
clientX/clientY are relative to the viewport. offsetX/offsetY are relative to the padding edge of the target element — useful for drawing on a canvas or pad.
mousemove can fire at a very high rate. Keep the handler light. Avoid heavy DOM work on every move; throttle or update only what you need.
Touch often does not send continuous mousemove the way a mouse does. For finger/pen tracking, prefer Pointer Events such as pointermove.
No. The mousemove event is Baseline widely available. Inline handlers are fine for learning; apps often use addEventListener("mousemove", handler).