Element: wheel event

Limited availability

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

The wheel event fires when the user rotates a wheel button on a pointing device (typically a mouse). It is also fired for related devices that simulate wheel actions, such as trackpads and mouse balls.

This event replaces the non-standard deprecated mousewheel event.

Don't confuse the wheel event with the scroll event:

  • A wheel event doesn't necessarily dispatch a scroll event. For example, the element may be unscrollable at all. Zooming actions using the wheel or trackpad also fire wheel events (with ctrlKey set to true).
  • A scroll event isn't necessarily triggered by a wheel event. Elements can also be scrolled by using the keyboard, dragging a scrollbar, or using JavaScript.
  • Even when the wheel event does trigger scrolling, the delta* values in the wheel event don't necessarily reflect the content's scrolling direction.

Therefore, do not rely on the wheel event's delta* properties to get the scrolling direction. Instead, detect value changes of scrollLeft and scrollTop of the target in the scroll event.

The wheel event is cancelable. In some browsers, only the first wheel event in a sequence is cancelable, and later events are non-cancelable. If the event is canceled, no scrolling or zooming is performed. This may cause performance issues as the browser has to wait for every wheel event to be processed before actually scrolling the content. You can avoid this by setting passive: true when calling addEventListener(), which may cause the browser to generate non-cancelable wheel events.

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

js
addEventListener("wheel", (event) => { })

onwheel = (event) => { }

Event type

A WheelEvent. Inherits from MouseEvent, UIEvent and Event.

Event UIEvent MouseEvent WheelEvent

Event properties

This interface inherits properties from its ancestors, MouseEvent, UIEvent, and Event.

WheelEvent.deltaX Read only

Returns a double representing the horizontal scroll amount.

WheelEvent.deltaY Read only

Returns a double representing the vertical scroll amount.

WheelEvent.deltaZ Read only

Returns a double representing the scroll amount for the z-axis.

WheelEvent.deltaMode Read only

Returns an unsigned long representing the unit of the delta* values' scroll amount. Permitted values are:

Constant Value Description
WheelEvent.DOM_DELTA_PIXEL 0x00 The delta* values are specified in pixels.
WheelEvent.DOM_DELTA_LINE 0x01 The delta* values are specified in lines. Each mouse click scrolls a line of content, where the method used to calculate line height is browser dependent.
WheelEvent.DOM_DELTA_PAGE 0x02 The delta* values are specified in pages. Each mouse click scrolls a page of content.
WheelEvent.wheelDelta Read only

Returns an integer (32-bit) representing the distance in pixels.

WheelEvent.wheelDeltaX Read only

Returns an integer representing the horizontal scroll amount.

WheelEvent.wheelDeltaY Read only

Returns an integer representing the vertical scroll amount.

Examples

Scaling an element via the wheel

This example shows how to scale an element using the mouse (or other pointing device) wheel.

html
<div>Scale me with your mouse wheel.</div>
css
body {
  min-height: 100vh;
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

div {
  width: 105px;
  height: 105px;
  background: #ccddff;
  padding: 5px;
}
js
let scale = 1;
const el = document.querySelector("div");

function zoom(event) {
  event.preventDefault();

  scale += event.deltaY * -0.01;

  // Restrict scale
  scale = Math.min(Math.max(0.125, scale), 4);

  // Apply scale transform
  el.style.transform = `scale(${scale})`;
}

el.onwheel = zoom;

addEventListener equivalent

The event handler can also be set up using the addEventListener() method:

js
el.addEventListener("wheel", zoom, { passive: false });

Specifications

Specification
UI Events
# event-type-wheel
HTML
# handler-onwheel

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
wheel event
Chrome – Full support
Chrome 31 (Release date: 2013-11-12)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 17 (Release date: 2012-11-20)
footnote Full support
Opera – Full support
Opera 18 (Release date: 2013-11-19)
footnote Full support
Safari – Full support
Safari 7 (Release date: 2013-10-22)
footnote Full support
Chrome Android – Full support
Chrome Android 31 (Release date: 2013-11-14)
footnote Full support
Firefox for Android – Full support
Firefox for Android 17 (Release date: 2012-11-20)
footnote Full support
Opera Android – Full support
Opera Android 18 (Release date: 2013-11-20)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 2 (Release date: 2014-10-17)
footnote Full support
WebView Android – Full support
WebView Android 4.4.3 (Release date: 2014-06-02)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote No support

Legend

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

Full support
Full support
No support
No support

See also