jQuery mousemove Event

Beginner
5 min read
Updated: Nov 2026
3 examples
Mouse Events

Definition

The mousemove event fires when the pointer moves inside an element (since jQuery 1.7 with .on()). Use it for drag UI, drawing, sliders, and live coordinates. jQuery normalizes event.pageX and event.pageY across browsers.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("#box").on("mousemove", function (event) { … })

event.pageX / pageY   → document coordinates
event.clientX / clientY → viewport coordinates

.trigger("mousemove") → run handlers (coords may be undefined)
.off("mousemove")     → unbind when done
CallWhat it does
mousemoveLearn the jQuery mousemove event: bind with .on('mousemove'), read event.pageX and event.pageY, trigger handlers, use the drag bind/unbind pattern, examples, pitfalls, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Select the element that should report pointer movement.
Step 2Bind with .on("mousemove", handler) (or only during drag — see below).
Step 3Read event.pageX / event.pageY (or client coords) inside the handler.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Log pageX / pageY

Append coordinates each time the pointer moves over the target — the official .on("mousemove") pattern.

jQuery
$("#target").on("mousemove", function (event) {
  var msg = "Handler for mousemove called at ";
  msg += event.pageX + ", " + event.pageY;
  $("#log").append(msg + "\n");
});
Try It Yourself

How It Works

  1. Bind the event. .on("mousemove", fn) registers the handler on #target.
  2. Move inside the element. Each pixel of movement calls the function.
  3. Read document coords. jQuery provides normalized pageX and pageY.

Example 2 — Trigger from Another Element

Click a button to fire the same handler with .trigger("mousemove") — coordinates are often undefined.

jQuery
$("#target").on("mousemove", function (event) {
  $("#log").append(
    "at " + event.pageX + ", " + event.pageY + "\n"
  );
});

$("#other").on("click", function () {
  $("#target").trigger("mousemove");
});
Try It Yourself

How It Works

  1. Same handler. Real moves and triggers both call the bound function.
  2. Synthetic event. .trigger("mousemove") has no real pointer.
  3. Missing coords. pageX / pageY are often undefined.

Example 3 — pageX vs clientX

Show both coordinate systems while moving — adapted from the official jQuery API demo.

jQuery
$("#pad").on("mousemove", function (event) {
  $("#page").text(
    "page: (" + event.pageX + ", " + event.pageY + ")"
  );
  $("#client").text(
    "client: (" + event.clientX + ", " + event.clientY + ")"
  );
});
Try It Yourself

How It Works

  1. Document coords. pageX / pageY include scroll offset.
  2. Viewport coords. clientX / clientY are relative to the visible window.
  3. jQuery helps. pageX / pageY are normalized for consistent cross-browser use.

Frequently Asked Questions

mousemove fires whenever the mouse pointer moves inside an element — even by one pixel. Bind with .on('mousemove', handler) (since jQuery 1.7). Any HTML element can receive it. It is one of the highest-frequency browser events, so keep handlers light.
pageX and pageY are relative to the document (including scroll). clientX and clientY are relative to the viewport. jQuery normalizes pageX and pageY across browsers — prefer them when positioning elements on the page.
It can fire hundreds of times per second. Heavy DOM work, layout thrashing, or multiple handlers per move can slow the page. Keep handlers fast, throttle or use requestAnimationFrame when needed, and unbind as soon as tracking is done.
Bind mousemove inside a mousedown handler, then unbind on mouseup. Attach mouseup high in the DOM (often $(document).one('mouseup', fn)) because release may happen outside the original element.
Call .trigger('mousemove') on the element. Handlers run, but pageX and pageY are often undefined unless you pass a custom event object with coordinates.
Use .on('mousemove', handler). The .mousemove() shorthand is deprecated since jQuery 3.3. Unbind with .off('mousemove'). .on() also supports delegation: .on('mousemove', '.handle', fn).

Next: .mousemove() Method — Deprecated Shorthand

See the deprecated shorthand and how it maps to .on("mousemove").

.mousemove() Method — Deprecated Shorthand tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

10 people found this page helpful