JavaScript Element mousemove Event

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
DOM event
Instance event

What You’ll Learn

The mousemove event fires when a pointing device moves inside an element. Learn addEventListener vs onmousemove, coordinates, movementX/movementY, canvas drawing with mousedown/mouseup, performance tips, and five try-it labs.

01

Kind

Instance event

02

Type

MouseEvent

03

When

Pointer moves

04

Handler

onmousemove

05

Rate

Can be very high

06

Status

Baseline widely available

Introduction

mousemove answers: “Did the pointer just move while over this element?” It fires whether or not buttons are pressed, and it can fire extremely often while the user is moving.

That high rate makes it perfect for drawing, custom cursors, and drag tracking— and also easy to misuse. Keep handlers cheap, or only process moves while a drag/draw flag is true.

💡
Beginner tip

For canvas drawing, MDN pairs mousedown (start), mousemove (draw while active), and mouseup on window (stop even if the pointer leaves the canvas).

Understanding mousemove

An instance event that answers: “Did the pointer move here?”

  • Fires when the pointer moves while inside the element.
  • Buttons optional — moves fire even with no button down.
  • MouseEvent — coordinates, movement deltas, and buttons.
  • High rate — depends on mouse speed, device, and system load.
  • Baseline Widely available on MDN (since July 2015).

📝 Syntax

Use the event name with addEventListener, or set the handler property:

JavaScript
addEventListener("mousemove", (event) => { });

onmousemove = (event) => { };

Event type

A MouseEvent (inherits from UIEvent and Event).

Handy properties

PropertyMeaning
clientX / clientYPosition in the viewport
offsetX / offsetYPosition relative to the target’s padding edge (great for canvas)
movementX / movementYDelta since the previous mousemove
buttonsBitmask of buttons currently held

⚠️ High Fire Rate

MDN notes that mousemove can fire at a very high rate. Heavy DOM writes, layout thrash, or logging every event can stutter the page.

  • Only update UI while dragging / drawing if that is your use case.
  • Throttle or batch work with requestAnimationFrame.
  • Prefer reading event fields once and writing DOM sparsely.
  • Consider Pointer Events + capture for drag outside the element.

⚡ Quick Reference

GoalCode / note
Listenel.addEventListener("mousemove", fn)
Handler propertyel.onmousemove = fn
Viewport coordsevent.clientX, event.clientY
Element-local coordsevent.offsetX, event.offsetY
Deltasevent.movementX, event.movementY
MDN statusBaseline Widely available

🔍 At a Glance

Four facts to remember about mousemove.

Event type
MouseEvent

UIEvent + Event

Means
moved

Pointer relocated

Key fields
clientX

+ movementX

Baseline
yes

Widely available

Examples Gallery

Examples follow MDN Element: mousemove event. Move the pointer over the stage (and press to draw where noted).

📚 Getting Started

Read positions and use the handler property.

Example 1 — Log clientX / clientY

Show viewport coordinates as the pointer moves over the stage.

JavaScript
const stage = document.getElementById("stage");
const out = document.getElementById("out");

stage.addEventListener("mousemove", (event) => {
  out.textContent = event.clientX + ", " + event.clientY;
});
Try It Yourself

How It Works

Every move updates the readout. In real apps, avoid writing text this often without throttling.

Example 2 — onmousemove Property

Same idea using the handler property form.

JavaScript
const stage = document.getElementById("stage");
const out = document.getElementById("out");

stage.onmousemove = (event) => {
  out.textContent = "onmousemove @ " + event.offsetX + "," + event.offsetY;
};
Try It Yourself

How It Works

Prefer addEventListener when you need multiple listeners or easy removal.

📈 Deltas, Drawing & Drag

Use movement deltas, draw on a canvas (MDN), and track only while dragging.

Example 3 — movementX / movementY

Show how far the pointer moved since the previous mousemove.

JavaScript
const stage = document.getElementById("stage");
const out = document.getElementById("out");

stage.addEventListener("mousemove", (event) => {
  out.textContent =
    "dx=" + event.movementX + " dy=" + event.movementY;
});
Try It Yourself

How It Works

Deltas are useful for relative motion (panning a map, FPS-style look, custom scrubbers).

Example 4 — Draw on a Canvas (MDN)

Start on mousedown, draw on mousemove, stop on window mouseup.

JavaScript
let isDrawing = false;
let x = 0;
let y = 0;

const myPics = document.getElementById("myPics");
const context = myPics.getContext("2d");

myPics.addEventListener("mousedown", (e) => {
  x = e.offsetX;
  y = e.offsetY;
  isDrawing = true;
});

myPics.addEventListener("mousemove", (e) => {
  if (isDrawing) {
    drawLine(context, x, y, e.offsetX, e.offsetY);
    x = e.offsetX;
    y = e.offsetY;
  }
});

window.addEventListener("mouseup", () => {
  if (isDrawing) {
    x = 0;
    y = 0;
    isDrawing = false;
  }
});

function drawLine(context, x1, y1, x2, y2) {
  context.beginPath();
  context.strokeStyle = "black";
  context.lineWidth = 1;
  context.moveTo(x1, y1);
  context.lineTo(x2, y2);
  context.stroke();
  context.closePath();
}
Try It Yourself

How It Works

offsetX/offsetY give coordinates relative to the canvas. Listening for mouseup on window ends the stroke even if you release outside.

Example 5 — Track Only While Dragging

Ignore idle moves; update a readout only when the primary button is held.

JavaScript
const stage = document.getElementById("stage");
const out = document.getElementById("out");
let dragging = false;

stage.addEventListener("mousedown", (event) => {
  if (event.button !== 0) return;
  dragging = true;
  out.textContent = "dragging…";
});

stage.addEventListener("mousemove", (event) => {
  if (!dragging) return;
  out.textContent = "drag @ " + event.offsetX + "," + event.offsetY;
});

window.addEventListener("mouseup", () => {
  dragging = false;
  out.textContent = "idle";
});
Try It Yourself

How It Works

Gating on a flag (or event.buttons) cuts wasted work when the pointer is just hovering.

🚀 Common Use Cases

  • Canvas / whiteboard drawing while the button is held.
  • Custom cursors, crosshairs, and follow-mouse UI.
  • Drag handles, scrubbers, and pan/zoom controls.
  • Relative look / pan using movementX / movementY.
  • Debugging pointer paths with lightweight coordinate logs.

🔧 How It Works

1

Pointer over element

Hotspot is inside the element’s hit area.

Target
2

mousemove

Fires repeatedly as the pointer relocates.

Move
3

Read coords / deltas

Use client/offset positions or movementX/Y.

Data
4

Update UI sparingly

Draw, drag, or throttle—keep the handler light.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • Can fire at a very high rate—keep work cheap or gate behind a drag flag.
  • For drag past element bounds, listen on window / use pointer capture.
  • Touch / pen: consider pointermove for a unified model.
  • Related learning: mouseout, mouseleave, addEventListener(), JavaScript hub.

Browser Support

mousemove is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project. Coordinate and movement fields are supported across modern engines.

Baseline · Widely available

Element mousemove

Fires when the pointer moves inside an element; pair with mousedown/mouseup for drawing and drag.

Full Widely available
Google Chrome Full support
Yes
Mozilla Firefox Full support
Yes
Apple Safari Full support
Yes
Microsoft Edge Full support
Yes
Opera Full support
Yes
Internet Explorer Supported
Yes
mousemove Baseline

Bottom line: Safe and universal. Treat fire rate carefully, and prefer pointer events for multi-input drag UX.

Conclusion

mousemove is the continuous “pointer moved” signal. Use it for drawing and drag tracking, keep handlers light, and pair it with mousedown / mouseup for session start and stop.

Continue with mouseout, mouseleave, addEventListener(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Gate move work behind a drag/draw flag when possible
  • Use offsetX/offsetY for element-local drawing
  • Listen for mouseup on window to end strokes
  • Throttle heavy UI with requestAnimationFrame
  • Prefer addEventListener in production code

❌ Don’t

  • Do expensive DOM work on every raw mousemove
  • Forget that moves fire even with no button pressed
  • Assume release always happens over the same element
  • Ignore touch/pen if your app needs them—consider pointer events
  • Overwrite onmousemove if you need multiple listeners

Key Takeaways

Knowledge Unlocked

Five things to remember about mousemove

Pointer moved—often, fast, and useful for draw/drag.

5
Core concepts
📄02

MouseEvent

coords · deltas

API
👆03

High rate

keep handlers light

Perf
🔄04

mousedown/up

draw/drag session

Pair
🎯05

Baseline

widely available

Status

❓ Frequently Asked Questions

It fires when a pointing device moves while its hotspot is inside the element. It can fire whether or not any mouse buttons are pressed.
No. MDN marks Element mousemove as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
A MouseEvent (inherits from UIEvent and Event). Useful fields include clientX, clientY, offsetX, offsetY, movementX, movementY, and buttons.
It can fire at a very high rate depending on mouse speed, machine speed, and other work. Keep handlers light, throttle updates, or only track while dragging.
Use mousedown to start, mousemove to draw while a flag is true, and mouseup (often on window) to stop — the MDN canvas pattern.
For unified mouse/touch/pen tracking, Pointer Events (pointermove) plus setPointerCapture are often a better modern model. mousemove remains widely used for mouse-only demos.
Did you know?

movementX and movementY report motion since the previous mousemove, which is perfect for relative controls—even when absolute screen position is less important.

Next: mouseout

Learn the bubbling leave event and how it differs from mouseleave.

mouseout →

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