Element: wheel event - Web APIs | MDN
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 properties
This interface inherits properties from its ancestors, MouseEvent, UIEvent, and Event.
WheelEvent.deltaXRead only-
Returns a
doublerepresenting the horizontal scroll amount. WheelEvent.deltaYRead only-
Returns a
doublerepresenting the vertical scroll amount. WheelEvent.deltaZRead only-
Returns a
doublerepresenting the scroll amount for the z-axis. WheelEvent.deltaModeRead only-
Returns an
unsigned longrepresenting the unit of thedelta*values' scroll amount. Permitted values are:Constant Value Description WheelEvent.DOM_DELTA_PIXEL0x00The delta*values are specified in pixels.WheelEvent.DOM_DELTA_LINE0x01The 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_PAGE0x02The delta*values are specified in pages. Each mouse click scrolls a page of content. WheelEvent.wheelDeltaRead only Deprecated-
Returns an integer (32-bit) representing the distance in pixels.
WheelEvent.wheelDeltaXRead only Deprecated-
Returns an integer representing the horizontal scroll amount.
WheelEvent.wheelDeltaYRead only Deprecated-
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 |