MouseEvent: movementX property

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

The movementX read-only property of the MouseEvent interface 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, the value of the property is computed like this: currentEvent.movementX = currentEvent.screenX - previousEvent.screenX. The value is zero for all events other than mousemove, pointermove, and pointerrawupdate.

Warning: Browsers use different units for movementX and screenX than what the specification defines. Depending on the browser and operating system, the movementX units may be a physical pixel, a logical pixel, or a CSS pixel. You may want to avoid the movement properties, and instead calculate the delta between the current client values (screenX, screenY) and the previous client values.

Value

A number. Always zero on any MouseEvent other than mousemove, and any PointerEvent other than pointermove or pointerrawupdate.

Examples

Log mouse movement for mousemove events

This example logs the amount of mouse movement using movementX and movementY.

HTML

html
<p id="log">Move your mouse around.</p>

JavaScript

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

function logMovement(event) {
  log.insertAdjacentHTML(
    "afterbegin",
    `movement: ${event.movementX}, ${event.movementY}<br>`,
  );
  while (log.childNodes.length > 128) log.lastChild.remove();
}

document.addEventListener("mousemove", logMovement);

Result

Specifications

Specification
Pointer Lock 2.0
# dom-mouseevent-movementx

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
movementX
Chrome – No support
Chrome 22 – 36 (Release date: 2012-09-25)
prefix
prefix Implemented with the vendor prefix: webkit
Chrome – Full support
Chrome 37 (Release date: 2014-08-26)
footnote Full support
Edge – Full support
Edge 13 (Release date: 2015-11-12)
footnote Full support
Firefox – No support
Firefox 1 – 40 (Release date: 2004-11-09)
prefix
prefix Implemented with the vendor prefix: moz
Firefox – Full support
Firefox 41 (Release date: 2015-09-22)
footnote Full support
Opera – No support
Opera 15 – 23 (Release date: 2013-07-02)
prefix
prefix Implemented with the vendor prefix: webkit
Opera – Full support
Opera 24 (Release date: 2014-09-02)
footnote Full support
Safari – No support
Safari 6 – 7 (Release date: 2012-07-25)
prefix
footnote Removed in 8 and later
prefix Implemented with the vendor prefix: webkit
Safari – Full support
Safari 9 (Release date: 2015-09-30)
footnote Full support
Chrome Android – No support
Chrome Android
footnote
footnote See bug 40290045
Firefox for Android – No support
Firefox for Android 4 – 40 (Release date: 2011-03-29)
prefix
prefix Implemented with the vendor prefix: moz
Firefox for Android – Full support
Firefox for Android 41 (Release date: 2015-09-22)
footnote Full support
Opera Android – No support
Opera Android
footnote
footnote See bug 40290045
Safari on iOS – Full support
Safari on iOS 17 (Release date: 2023-09-18)
footnote Full support
Samsung Internet – No support
Samsung Internet 1 – 2.1 (Release date: 2013-04-27)
prefix
prefix Implemented with the vendor prefix: webkit
Samsung Internet – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote Full support
WebView Android – No support
WebView Android
footnote
footnote See bug 40290045
WebView on iOS – Full support
WebView on iOS 17 (Release date: 2023-09-18)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
See implementation notes.
Requires a vendor prefix or different name for use.
Has more compatibility info.

See also