JavaScript MouseEvent movementX Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Newly available
Instance property

What You’ll Learn

MouseEvent.movementX is a read-only number: how far the pointer moved horizontally since the previous move event. Learn when it is non-zero, how it relates to screenX, unit caveats, safer DIY deltas, and five try-it labs.

01

Kind

Instance property

02

Returns

Number (delta X)

03

Status

Baseline Newly available

04

Live on

mousemove

05

Pair with

movementY

06

Else

Usually 0

Introduction

Absolute positions like clientX answer “where is the cursor?” movementX answers “how far did it move left or right since the last move?”—perfect for pans, look controls, and drawing tools.

JavaScript
document.addEventListener("mousemove", (event) => {
  console.log(event.movementX); // delta since previous mousemove
});
💡
Beginner tip

Think of movementX as a tiny “speedometer” for horizontal travel between two move events—not a ruler from the left edge of the window.

Understanding the Property

MDN: movementX provides the difference in the X coordinate of the mouse (or pointer) between the given move event and the previous move event of the same type. In other words, it is computed like currentEvent.screenX - previousEvent.screenX.

  • Instance — read event.movementX in a handler.
  • Delta — positive when moving right, negative when moving left (typical).
  • Move-only — zero on click and most other mouse events.
  • Unit caution — browsers may disagree on pixel units (see Notes).

📝 Syntax

JavaScript
const dx = mouseEvent.movementX;

Value

A number for the horizontal movement since the previous same-type move event. Always 0 on other MouseEvent types (and on pointer events other than pointermove / pointerrawupdate).

Safer DIY delta (portable units)

JavaScript
let prevScreenX = null;

document.addEventListener("mousemove", (e) => {
  const dx = prevScreenX == null ? 0 : e.screenX - prevScreenX;
  prevScreenX = e.screenX;
  console.log({ movementX: e.movementX, diyDx: dx });
});

⚖️ movementX vs Absolute X Properties

PropertyMeaningBest for
movementXDelta X since last moveRelative motion / look / pan
clientXX in the viewportTooltips, hit-testing in window
screenXX on the monitorMulti-monitor / DIY deltas
movementYDelta Y since last moveVertical twin of movementX

⚡ Quick Reference

GoalCode
Read horizontal deltaevent.movementX
Read both deltas{ x: e.movementX, y: e.movementY }
Listen for motionaddEventListener("mousemove", …)
DIY portable deltae.screenX - prevScreenX
MDN statusBaseline Newly available (Jan 2026)

🔍 At a Glance

Four facts about movementX.

Kind
instance

On the event

Type
number

Delta X

Status
baseline

Newly available

Pair
movementY

Vertical twin

Examples Gallery

Examples follow MDN MouseEvent.movementX. Move the mouse to see non-zero deltas; click to confirm zeros.

📚 Getting Started

See deltas on real mousemove events.

Example 1 — Log movementX / movementY

MDN-style demo: prepend each delta line while you move.

JavaScript
const log = document.getElementById("log");

function logMovement(event) {
  log.insertAdjacentHTML(
    "afterbegin",
    `movement: ${event.movementX}, ${event.movementY}
` ); while (log.childNodes.length > 128) log.lastChild.remove(); } document.addEventListener("mousemove", logMovement);
Try It Yourself

How It Works

Each mousemove reports how far the pointer traveled since the previous mousemove. Cap the log length so the page stays responsive.

Example 2 — Zero on Non-Move Events

Prove that a plain click reports movementX === 0.

JavaScript
document.addEventListener("click", (e) => {
  console.log({
    type: e.type,
    movementX: e.movementX, // 0
    clientX: e.clientX      // absolute position still set
  });
});
Try It Yourself

How It Works

Absolute coordinates still update on click. Movement deltas only apply to move-style events.

📈 Portable Deltas & Simple UX

Follow MDN’s unit warning and build tiny interactive demos.

Example 3 — DIY Delta from screenX

MDN suggests computing your own delta when units must stay consistent.

JavaScript
let prev = null;

document.addEventListener("mousemove", (e) => {
  const diyDx = prev == null ? 0 : e.screenX - prev;
  prev = e.screenX;
  console.log({ movementX: e.movementX, diyDx });
});
Try It Yourself

How It Works

Storing the previous screenX gives you a delta in the same unit family as screenX, which can be more predictable across engines.

Example 4 — Accumulate Horizontal Travel

Sum absolute movementX to estimate how far the cursor has traveled.

JavaScript
let traveled = 0;
const out = document.getElementById("out");

document.addEventListener("mousemove", (e) => {
  traveled += Math.abs(e.movementX);
  out.textContent = `Horizontal travel ≈ ${Math.round(traveled)}`;
});
Try It Yourself

How It Works

Using Math.abs counts left and right motion the same. Reset traveled when you start a new gesture.

Example 5 — Simple Horizontal Pan

Move a box with movementX while the mouse button is held.

JavaScript
const box = document.getElementById("box");
let dragging = false;
let x = 40;

box.addEventListener("mousedown", () => { dragging = true; });
window.addEventListener("mouseup", () => { dragging = false; });

window.addEventListener("mousemove", (e) => {
  if (!dragging) return;
  x += e.movementX;
  box.style.transform = `translateX(${x}px)`;
});
Try It Yourself

How It Works

Relative motion keeps the drag smooth even if the cursor briefly leaves the box. Pair with movementY for free 2D dragging.

🚀 Common Use Cases

  • Pointer-lock look / camera controls in games.
  • Canvas drawing tools that react to stroke velocity or direction.
  • Drag-to-pan maps, timelines, and image viewers.
  • Gesture metrics (how far the cursor traveled this frame).
  • Teaching relative vs absolute mouse coordinates next to clientX.

🔧 How It Works

1

Pointer moves

Browser records the new screen position.

Input
2

Delta vs previous move

Roughly screenX - previous.screenX.

Compute
3

mousemove event fires

movementX carries that horizontal delta.

Event
4

Your handler uses the delta

Pan, rotate, draw, or accumulate travel.

📝 Notes

  • Baseline Newly available (MDN, since January 2026)—verify older browsers if needed.
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • MDN warning: unit systems for movementX / screenX can differ by browser and OS.
  • Prefer DIY screenX/clientX deltas when pixel units must match everywhere.
  • Related: metaKey, movementY, clientX, MouseEvent(), Window, JavaScript hub.

Modern Browser Support

MouseEvent.movementX is Baseline Newly available (MDN: since January 2026). Logos use the shared browser-image-sprite.png sprite from this project. Older browsers may need fallbacks or DIY screenX deltas.

Baseline · Newly available

MouseEvent.movementX

Horizontal move delta—mind unit differences; DIY screenX deltas are safer for portable apps.

Modern Newly available
Google Chrome Supported in current releases
Full support
Mozilla Firefox Supported in current releases
Full support
Apple Safari Supported in current releases
Full support
Microsoft Edge Supported · Chromium
Full support
Opera Supported in modern versions
Full support
Internet Explorer Not a modern Baseline target
No support
movementX Modern

Bottom line: Use movementX on mousemove; fall back to screenX deltas when targeting older engines.

Conclusion

event.movementX is the horizontal distance the pointer traveled since the previous move event. Use it for relative motion UX, remember it is usually 0 outside move events, and consider DIY screenX deltas when unit consistency matters.

Continue with movementY, clientX, Window, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Read movementX on mousemove / pointer move events
  • Pair with movementY for 2D relative motion
  • Offer a DIY screenX delta when units must match
  • Test on the OSes and browsers you ship to
  • Reset accumulators when a gesture ends

❌ Don’t

  • Expect non-zero values on plain click events
  • Assume identical pixel units in every browser
  • Confuse deltas with absolute clientX positions
  • Skip fallbacks if you support older engines
  • Mutate event.movementX on live user events

Key Takeaways

Knowledge Unlocked

Five things to remember about movementX

Horizontal move delta—relative motion, not absolute position.

5
Core concepts
📈 02

Delta X

since last move

Type
🔄 03

Move events

else usually 0

When
⚠️ 04

Unit caution

DIY if needed

Portability
🛡️ 05

Baseline

newly available

Status

❓ Frequently Asked Questions

It is a read-only number: the difference in the X coordinate of the pointer between this move event and the previous move event of the same type. Conceptually: current.screenX − previous.screenX.
No. MDN marks MouseEvent.movementX as Baseline Newly available (since January 2026). It is not Deprecated, Experimental, or Non-standard, so no status warning banner is needed. Older browsers may still lack reliable support.
It is always zero on MouseEvent types other than mousemove, and on PointerEvent types other than pointermove or pointerrawupdate. A plain click reports 0.
Browsers disagree on whether movementX uses physical pixels, logical pixels, or CSS pixels—unlike a strict reading of the spec. For portable deltas, many apps compute their own delta from successive screenX or clientX values.
movementY is the vertical counterpart. Together they describe how far the pointer moved since the last move event of the same type.
Pointer-lock games, camera pans, drawing tools, and any UI that needs relative movement rather than absolute cursor position.
Did you know?

movementX is defined in the Pointer Lock specification family. With the pointer locked (cursor hidden and confined), relative deltas keep working even when the OS cursor would have hit the edge of the screen—exactly what first-person look controls need.

Next: MouseEvent.movementY

Learn vertical pointer deltas between consecutive move events.

movementY →

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