HTML onmousemove Attribute

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
Events & Handlers

Introduction

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.

What You’ll Learn

01

Event handler

Inline JS.

02

mousemove

While moving.

03

Coordinates

clientX/Y.

04

Performance

Throttle.

05

addEventListener

Preferred way.

06

vs mouseover

Move vs enter.

Purpose of onmousemove Attribute

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

💡
High frequency event

mousemove can fire many times per second. Avoid heavy DOM updates or network calls on every event — use throttling or requestAnimationFrame.

📝 Syntax

Set onmousemove on any element that should track movement:

onmousemove.html
<div onmousemove="trackMouse(event)">
  Move the mouse here
</div>

<canvas width="400" height="200" onmousemove="draw(event)"></canvas>

Syntax Rules

  • Value is JavaScript executed on each mousemove while over the element.
  • Works on most elements: div, canvas, button, etc.
  • Pass event for clientX, clientY, offsetX, offsetY.
  • Fires only while the pointer is inside the element (unless listening on document).
  • The event bubbles up the DOM tree.
  • Modern alternative: el.addEventListener("mousemove", handler).
  • Pair with onmouseleave or onmouseout to reset UI when pointer exits.

💎 Values

The onmousemove attribute accepts a string of JavaScript code:

  • onmousemove="trackMouse(event)" — Pass the event object.
  • onmousemove="updateCoords(event.clientX, event.clientY)" — Pass coordinates.
  • JavaScript: box.onmousemove = (e) => { … } — property assignment.
coordinates.html
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})`;
});

⚡ Quick Reference

Property / eventMeaningNotes
mousemovePointer moved over elementFires continuously
event.clientX / clientYViewport positionMost common
event.offsetX / offsetYRelative to targetInside the box
mouseoverPointer enteredNot every move
Handler attributeonmousemoveInline on element

Applicable Elements

TargetSupported?Notes
<div>, <span>YesTracking zones, tooltips
<canvas>YesDrawing, games
<button>YesRare; prefer CSS hover
<img>YesImage maps, magnifiers
document / windowVia JSGlobal tracking in script

onmousemove vs onmouseover vs onmouseenter

HandlerWhen it firesTypical use
onmousemoveWhile pointer moves over elementCoordinates, drawing, drag
onmouseoverPointer enters element (bubbles)Hover highlight
onmouseenterPointer enters (no bubble from children)Clean enter detection
onmousedownButton pressed downStart drag with move

Examples Gallery

Track coordinates inline, addEventListener on a zone, and read offsetX/offsetY relative to the box.

👀 Live Preview

Move the mouse inside the box — coordinates update live:

Move here

X: —, Y: —

Example — Inline onmousemove handler

Display viewport coordinates while moving over a div:

div-onmousemove.html
<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>
Try It Yourself

How It Works

Each pixel of movement over the div fires mousemove. The handler reads clientX and clientY from the event.

Dynamic Values with JavaScript

Attach with addEventListener and update the box text:

dynamic-onmousemove.html
<div id="dynamicElement">Hover over me!</div>

<script>
  document.getElementById("dynamicElement").addEventListener("mousemove", function (event) {
    this.textContent = "X: " + event.clientX + ", Y: " + event.clientY;
  });
</script>
Try It Yourself

How It Works

Register once in script. With a non-arrow function, this is the element receiving the event.

Example — offsetX and offsetY (local position)

Position relative to the target element — useful inside a box or canvas:

local-coords.html
zone.addEventListener("mousemove", (event) => {
  dot.style.left = event.offsetX + "px";
  dot.style.top = event.offsetY + "px";
  label.textContent =
    "Local: " + event.offsetX + ", " + event.offsetY;
});
Try It Yourself

How It Works

offsetX and offsetY are measured from the padding edge of the target element — handy for drawing and hit detection.

♿ Accessibility

  • Do not require mouse movement — Provide keyboard or touch alternatives for any feature triggered only by mousemove.
  • Avoid motion-only UI — Users with motor difficulties may struggle with precision tracking.
  • Respect reduced motion — Check prefers-reduced-motion before animating elements that follow the cursor.
  • Keep targets large enough — Small tracking zones are hard to use on touch devices.
  • Consider CSS :hover — Simple hover styles often need no JavaScript.

🧠 How onmousemove Works

1

Pointer over element

User moves mouse.

Input
2

mousemove fires

Many times per sec.

Event
3

Handler runs

onmousemove code.

Update
=

Live tracking

Coords, draw, drag.

Browser Support

The mousemove event and onmousemove handler are supported in all browsers, including legacy Internet Explorer.

Mouse Events · Fully supported

Universal onmousemove support

Every major browser fires mousemove while the pointer moves over elements.

100% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer Fully supported
Full support
Opera Fully supported
Full support
onmousemove attribute 100% supported

Bottom line: Safe for coordinate tracking and interactive zones in any browser.

💡 Best Practices

✅ Do

  • Pass event and use clientX / clientY
  • Throttle heavy handlers — mousemove fires very often
  • Use addEventListener("mousemove", …) in production
  • Pair with mouseleave to reset UI when pointer exits
  • Use offsetX / offsetY for position inside a box

❌ Don’t

  • Confuse mousemove with mouseover — different events
  • Run expensive work on every mousemove without throttling
  • Rely on mouse movement alone for essential features
  • Log only to console in user-facing demos
  • Forget touch users — test on mobile devices

Conclusion

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

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onmousemove

Bookmark these before tracking the pointer.

5
Core concepts
🔢 02

clientX/Y

Viewport.

API
03

High rate

Throttle.

Perf
🖱 04

vs mouseover

Different.

Compare
05

Keyboard alt

Not mouse-only.

A11y

❓ Frequently Asked Questions

It runs JavaScript whenever the mouse pointer moves while it is over the element.
No. mouseover fires when the pointer enters. mousemove fires on every movement while inside.
Use event.clientX and event.clientY for viewport coords, or event.offsetX / offsetY relative to the element.
The event can fire dozens of times per second. Heavy DOM or API work on every fire can slow the page.
addEventListener("mousemove", handler) is preferred for production code.
Yes — universal support including Internet Explorer.

Master mouse tracking events

Practice the onmousemove attribute with live coordinate examples in the Try It editor.

Try onmousemove demo →

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.

5 people found this page helpful