HTML onmousemove Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

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.

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).

Attribute Values

The attribute value is a script. Common patterns:

Value / patternDescriptionTry it
handler(event)Read clientX / clientY (viewport) while movingTry it
offsetX / offsetYPosition relative to the target element (good for pads/canvas)Try it
With mousedown / mouseupClassic press-and-drag patterns (for example drawing)Try it
addEventListener("mousemove")Preferred modern registration in appsTry it

Live Preview

Move the pointer over the pad. Status shows viewport or local (offset) coordinates.

coords

Move inside the pad — events stop when you leave it.

Move pad

Position: (none)

HTML Examples

Short study snippets. Use Try It Yourself for a live editor.

Example 1 — Log Viewport Coordinates

Show clientX and clientY while the pointer moves over a pad.

HTML
<div id="pad" onmousemove="showPos(event)">Move here</div>
<p id="log">Position: (none)</p>

<script>
  function showPos(e) {
    document.getElementById("log").textContent =
      "clientX: " + e.clientX + ", clientY: " + e.clientY;
  }
</script>
Try It Yourself

How It Works

  1. Move over the pad. mousemove fires repeatedly while inside.
  2. Read clientX / clientY. Values are relative to the browser viewport.
  3. Update a status line. Keep the update small — this event is frequent.
  4. Leave the pad. Movement outside does not fire this element’s mousemove.

Example 2 — Local Coordinates with offsetX / offsetY

Measure position relative to the pad itself — handy for drawing and hit-testing.

HTML
<div id="pad">Move here</div>
<p id="log">Local: (none)</p>

<script>
  document.getElementById("pad").addEventListener("mousemove", function (e) {
    document.getElementById("log").textContent =
      "offsetX: " + e.offsetX + ", offsetY: " + e.offsetY;
  });
</script>
Try It Yourself

How It Works

  1. Listen with addEventListener on the pad.
  2. Use offsetX / offsetY — distance from the target’s padding edge.
  3. Top-left of the pad is near 0,0 (browser rounding may vary slightly).
  4. 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>
Try It Yourself

How It Works

  1. mousedown stores the start point and sets drawing = true.
  2. mousemove draws a segment only while drawing is true.
  3. mouseup on window stops drawing even if release happens outside the canvas.
  4. offsetX / offsetY map the pointer to canvas coordinates.

Edge Cases & Pitfalls

Most surprises come from heavy handlers and confusing movement with enter/leave.

perf

Fires at a very high rate

Avoid expensive layout or network calls inside every mousemove. Update only the UI you need, or throttle.

scope

Only while inside the element

Leaving the target stops that element’s mousemove. For drag that continues outside, listen on document / window while dragging.

compare

Not the same as mouseover

mouseover is about entering; mousemove is about moving. Do not use continuous move handlers for one-shot “entered” logic.

touch

Touch is not a mouse

Phones may not deliver steady mousemove. Prefer pointermove for multi-device pointing.

a11y

Do not hide essential UI behind hover-move only

Keyboard and touch users may never move a mouse over the area. Provide equivalent non-hover ways to reach content and controls.

Browser Support

onmousemove is an HTML event handler for the mousemove event in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

onmousemove

Widely available since about July 2015. Supported across modern desktop browsers; treat continuous move tracking carefully on touch devices.

98% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer Legacy support
Supported
onmousemove Widely supported

Bottom line: Safe to use for pointer tracking. Keep handlers light; consider Pointer Events for touch/pen.

Key Takeaways

  • Role: run script while the pointer moves inside an element.
  • Coordinates: clientX/Y = viewport; offsetX/Y = element-local.
  • 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).

Next: onmouseout

Run script when the pointer leaves an element.

onmouseout tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful