The wheel
event is fired when a wheel button of a pointing device (usually a mouse) is rotated. This event replaces the non-standard deprecated mousewheel
event.
WheelEvent
This event implements the properties of WheelEvent
, MouseEvent
, UIEvent
and Event
.
WheelEvent
This interface inherits properties from its parents, MouseEvent
, UIEvent
and Event
.
WheelEvent.deltaX
Read only
double
representing the horizontal scroll amount.WheelEvent.deltaY
Read only
double
representing the vertical scroll amount.WheelEvent.deltaZ
Read only
double
representing the scroll amount for the z-axis.WheelEvent.deltaMode
Read only
unsigned long
representing the unit of the delta values scroll amount. Permitted values are: Constant | Value | Description |
---|---|---|
DOM_DELTA_PIXEL | 0x00 | The delta values are specified in pixels. |
DOM_DELTA_LINE | 0x01 | The delta values are specified in lines. |
DOM_DELTA_PAGE | 0x02 | The delta values are specified in pages. |
MouseEvent
This interface also inherits properties of its parents, UIEvent
and Event
.
MouseEvent.altKey
Read only
true
if the alt key was down when the mouse event was fired.MouseEvent.button
Read only
MouseEvent.buttons
Read only The buttons being depressed (if any) when the mouse event was fired.
MouseEvent.clientX
Read only
MouseEvent.clientY
Read only
MouseEvent.ctrlKey
Read only
true
if the control key was down when the mouse event was fired.MouseEvent.metaKey
Read only
true
if the meta key was down when the mouse event was fired.MouseEvent.movementX
Read only
mousemove
event.MouseEvent.movementY
Read only
mousemove
event.MouseEvent.offsetX
Read only
MouseEvent.offsetY
Read only
MouseEvent.pageX
Read only
MouseEvent.pageY
Read only
MouseEvent.region
Read only
null
is returned.MouseEvent.relatedTarget
Read only
The secondary target for the event, if there is one.
MouseEvent.screenX
Read only
MouseEvent.screenY
Read only
MouseEvent.shiftKey
Read only
true
if the shift key was down when the mouse event was fired.MouseEvent.which
Read only
MouseEvent.mozPressure
Read only
0.0
(minimum pressure) and 1.0
(maximum pressure).MouseEvent.mozInputSource
Read only
The type of device that generated the event (one of the MOZ_SOURCE_*
constants listed below). This lets you, for example, determine whether a mouse event was generated by an actual mouse or by a touch event (which might affect the degree of accuracy with which you interpret the coordinates associated with the event).
MouseEvent.webkitForce
Read only
MouseEvent.x
Read only
MouseEvent.clientX
.MouseEvent.y
Read only
MouseEvent.clientY
UIEvent
This interface also inherits properties of its parent, Event
.
UIEvent.cancelBubble
Boolean
indicating whether the bubbling of the event has been canceled or not.UIEvent.detail
Read only
long
with details about the event, depending on the event type.UIEvent.isChar
Read only
Boolean
indicating whether the event produced a key character or not.UIEvent.layerX
Read only
UIEvent.layerY
Read only
UIEvent.pageX
Read only
UIEvent.pageY
Read only
UIEvent.sourceCapabilities
Read only
UIEvent.view
Read only
WindowProxy
that contains the view that generated the event.UIEvent.which
Read only keyCode
of the key pressed, or the character code (charCode
) for an alphanumeric key pressed.Event
Event.bubbles
Read only
Event.cancelBubble
Event.stopPropagation()
. Setting its value to true
before returning from an event handler prevents propagation of the event.Event.cancelable
Read only
Event.composed
Read only
Event.currentTarget
Read only
Event.deepPath
Array
of DOM Node
s through which the event has bubbled.Event.defaultPrevented
Read only
event.preventDefault()
has been called on the event.Event.eventPhase
Read only
Event.explicitOriginalTarget
Read only
Event.originalTarget
Read only
Event.returnValue
Event.preventDefault()
and Event.defaultPrevented
.Event.scoped
Read only
Boolean
indicating whether the given event will bubble across through the shadow root into the standard DOM. This property has been renamed to composed
.Event.srcElement
Event.target
.Event.target
Read only
Event.timeStamp
Read only
DOMHighResTimeStamp
instead.Event.type
Read only
Event.isTrusted
Read only
This event implements the methods of WheelEvent
, MouseEvent
, UIEvent
and Event
.
WheelEvent
This interface doesn't define any methods, but inherits methods from its parents, MouseEvent
, UIEvent
and Event
.
MouseEvent
This interface also inherits methods of its parents, UIEvent
and Event
.
MouseEvent.getModifierState()
KeyboardEvent.getModifierState
() for details.MouseEvent.initMouseEvent()
MouseEvent
created. If the event has already being dispatched, this method does nothing.UIEvent
This interface also inherits methods of its parent, Event
.
UIEvent.initUIEvent()
UIEvent
object. If the event has already being dispatched, this method does nothing.Event
Event.initEvent()
Event.preventBubble()
Obsolete since Gecko 24
event.stopPropagation
instead.Event.preventCapture()
Obsolete since Gecko 24
event.stopPropagation
instead.Event.preventDefault()
Event.stopImmediatePropagation()
Event.stopPropagation()
Event.getPreventDefault()
Event.defaultPrevented
. Use Event.defaultPrevented
instead.Specification | Status | Comment |
---|---|---|
Document Object Model (DOM) Level 3 Events Specification The definition of 'wheel' in that specification. | Obsolete | Initial definition. |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 31 | (Yes) | 17.0 (17.0) | 9.0[1] | 18 | 7.0 |
Pinch-to-zoom maps to WheelEvent + Ctrl key[2]
| (Yes) | ? | 55.0 (55.0) | ? | ? | ? |
Feature | Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | ? | (Yes) | 17.0 (17.0) | ? | ? | ? |
Pinch-to-zoom maps to WheelEvent + Ctrl key[2]
| ? | ? | 55.0 (55.0) | ? | ? | ? |
[1] Internet Explorer exposes the wheel
event only via addEventListener
, there is no onwheel
attribute on DOM objects. Bug report.
[2] This mapping was implemented to allow developers to implement simple zooming functionality using the pinch-to-zoom gesture on mobile screens/trackpads (mousewheel + Ctrl commonly zooms).
The following script creates a global addWheelListener
method that allows to listen to the different wheel events across browsers and prevent the default behavior.
// creates a global "addWheelListener" method // example: addWheelListener( elem, function( e ) { console.log( e.deltaY ); e.preventDefault(); } ); (function(window,document) { var prefix = "", _addEventListener, support; // detect event model if ( window.addEventListener ) { _addEventListener = "addEventListener"; } else { _addEventListener = "attachEvent"; prefix = "on"; } // detect available wheel event support = "onwheel" in document.createElement("div") ? "wheel" : // Modern browsers support "wheel" document.onmousewheel !== undefined ? "mousewheel" : // Webkit and IE support at least "mousewheel" "DOMMouseScroll"; // let's assume that remaining browsers are older Firefox window.addWheelListener = function( elem, callback, useCapture ) { _addWheelListener( elem, support, callback, useCapture ); // handle MozMousePixelScroll in older Firefox if( support == "DOMMouseScroll" ) { _addWheelListener( elem, "MozMousePixelScroll", callback, useCapture ); } }; function _addWheelListener( elem, eventName, callback, useCapture ) { elem[ _addEventListener ]( prefix + eventName, support == "wheel" ? callback : function( originalEvent ) { !originalEvent && ( originalEvent = window.event ); // create a normalized event object var event = { // keep a ref to the original event object originalEvent: originalEvent, target: originalEvent.target || originalEvent.srcElement, type: "wheel", deltaMode: originalEvent.type == "MozMousePixelScroll" ? 0 : 1, deltaX: 0, deltaY: 0, deltaZ: 0, preventDefault: function() { originalEvent.preventDefault ? originalEvent.preventDefault() : originalEvent.returnValue = false; } }; // calculate deltaY (and deltaX) according to the event if ( support == "mousewheel" ) { event.deltaY = - 1/40 * originalEvent.wheelDelta; // Webkit also support wheelDeltaX originalEvent.wheelDeltaX && ( event.deltaX = - 1/40 * originalEvent.wheelDeltaX ); } else { event.deltaY = originalEvent.deltaY || originalEvent.detail; } // it's time to fire the callback return callback( event ); }, useCapture || false ); } })(window,document);
If a trusted wheel
event is not consumed, a DOMMouseScroll
event and a MozMousePixelScroll
event are fired for backward compatibility. Their attribute values are computed from the wheel
event's delta values and the nearest ancestor clipped element (i.e., overflow
is not visible). If either wheel
event or one of the legacy events is consumed by event.preventDefault()
, nothing happens.
The event order is:
wheel
event in the default event group (both web applications and add-ons can handle the events in this group)DOMMouseScroll
event in both event group if accumulated deltaY
of consecutive wheel
events become larger than 1.0 or less than -1.0MozMousePixelScroll
event in both event group if deltaY
of the latest wheel
event isn't zeroDOMMouseScroll
event in both event group if accumulated deltaX
of consecutive wheel
events become larger than 1.0 or less than -1.0MozMousePixelScroll
event in both event group if deltaX
of the latest wheel
event isn't zerowheel
event in the system group (only add-ons can handle in this group)
wheel (default event group) |
preventDefault() called | ||||
---|---|---|---|---|---|
DOMMouseScroll (Vertical) | Not fired |
preventDefault() called | |||
MozMousePixelScroll (Vertical) | Not fired |
defaultPrevented is true |
preventDefault() called | ||
DOMMouseScroll (Horizontal) | Not fired | Not affected | Not affected |
preventDefault() called | |
MozMousePixelScroll (Horizontal) | Not fired | Not affected | Not affected |
defaultPrevented is true |
preventDefault() called |
wheel (system event group) |
defaultPrevented is true |
defaultPrevented is true |
defaultPrevented is true |
defaultPrevented is true |
defaultPrevented is true |
If an Add-on needs to know if one of legacy events are consumed by web contents, it can use the wheel
event of #6. See the document of nsIEventListenerService
for the detail of system event group.
The delta values and default action of trusted events can be customized by user preferences (details). Therefore, any web developers shouldn't expect a particular action would occur after receiving this event.
The delta values do not indicate the actual scroll amount by default. If the user was holding a modifier key when moving the scroll wheel, the wheel action may cause another action such as moving forward or backward through browser history or zooming in or out. In addition, even when scrolling, the scrolled element may be different from its event target. The wheel event target is computed only from the mouse cursor position at the time at which the event was fired. That event may not only not be the one actually being scrolled, but may not even be scrollable.
On Windows, following 3 native events cause DOM wheel
events.
deltaMode
can be DOM_DELTA_LINE
or DOM_DELTA_PAGE
. It depends on user settings of Windows (The defualt setting causes DOM_DELTA_LINE
).deltaMode
can be DOM_DELTA_LINE
or DOM_DELTA_PAGE
. However, neither wheel scroll speed setting dialog of Windows nor similar UI of each mouse driver's utility typically has the UI to change to page scroll. So, typically, this value is DOM_DELTA_LINE
.deltaMode
is always DOM_DELTA_PIXEL
. But note that most touchpad devices of notebook emulate mouse wheel events rather than using this event. This event it typically used on tablet PC.On Mac, deltaMode
value depends on the device. If the device supports high resolution scroll by pixels, deltaMode
value is typically DOM_DELTA_PIXEL
. However, the user can change it to DOM_DELTA_LINE
with "mousewheel.enable_pixel_scrolling"
pref. If the device doesn't support high resolution scroll, it's always DOM_DELTA_LINE
.
On other platforms, the deltaMode
value is always DOM_DELTA_LINE
.
Untrusted wheel
events never cause any default action.
WheelEvent
DOMMouseScroll
and MozMousePixelScroll
mousewheel
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/Events/wheel