Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. Element
  4. wheel

Element: wheel event

Limited availability

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

Thewheel 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 deprecatedmousewheel event.

Don't confuse thewheel event with thescroll event:

  • Awheel event doesn't necessarily dispatch ascroll event. For example, the element may be unscrollable at all. Zooming actions using the wheel or trackpad also firewheel events (withctrlKey set to true).
  • Ascroll event isn't necessarily triggered by awheel event. Elements can also be scrolled by using the keyboard, dragging a scrollbar, or using JavaScript.
  • Even when thewheel event does trigger scrolling, thedelta* values in thewheel event don't necessarily reflect the content's scrolling direction.

Therefore, do not rely on thewheel event'sdelta* properties to get the scrolling direction. Instead, detect value changes ofscrollLeft andscrollTop of the target in thescroll event.

Thewheel event is cancelable. In some browsers, only the firstwheel 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 settingpassive: true when callingaddEventListener(), which may cause the browser to generate non-cancelablewheel events.

Syntax

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

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

Event type

AWheelEvent. Inherits fromMouseEvent,UIEvent andEvent.

Event UIEvent MouseEvent WheelEvent

Event properties

This interface inherits properties from its ancestors,MouseEvent,UIEvent, andEvent.

WheelEvent.deltaXRead only

Returns adouble representing the horizontal scroll amount.

WheelEvent.deltaYRead only

Returns adouble representing the vertical scroll amount.

WheelEvent.deltaZRead only

Returns adouble representing the scroll amount for the z-axis.

WheelEvent.deltaModeRead only

Returns anunsigned long representing the unit of thedelta* values' scroll amount. Permitted values are:

ConstantValueDescription
WheelEvent.DOM_DELTA_PIXEL0x00Thedelta* values are specified in pixels.
WheelEvent.DOM_DELTA_LINE0x01Thedelta* 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_PAGE0x02Thedelta* values are specified in pages. Each mouse click scrolls a page of content.
WheelEvent.wheelDeltaRead onlyDeprecated

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

WheelEvent.wheelDeltaXRead onlyDeprecated

Returns an integer representing the horizontal scroll amount.

WheelEvent.wheelDeltaYRead onlyDeprecated

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 theaddEventListener() method:

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

Specifications

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

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp