Element: wheel event

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:

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.

EventUIEventMouseEventWheelEvent

Examples

undefined

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

See also