HTML onwheel Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The onwheel attribute is an event handler for the wheel event. It runs when the user rotates a mouse wheel (or a similar trackpad gesture) over the element. The event is a WheelEvent with deltaX, deltaY, and deltaZ.

It replaces the old non-standard onmousewheel. Do not confuse it with onscroll: wheel is input; scroll means the position changed.

Standard wheel event (replaces mousewheel).

Not deprecated. Prefer addEventListener("wheel", …) in scripts; the HTML attribute is the same handler.

Wheel ≠ scroll direction.

A wheel event may not scroll anything (unscrollable target, zoom with Ctrl). For scroll position, read scrollTop / scrollY in a scroll handler — not delta* alone.

HTML Syntax

syntax.html
<div onwheel=" handler">

Modern equivalent: el.addEventListener("wheel", handler). Use { passive: true } unless you must call preventDefault().

Attribute Values

The value is a script (event handler code). Useful WheelEvent fields:

PropertyMeaningTry it
deltaYVertical wheel delta (sign/units vary)Try it
deltaXHorizontal wheel / trackpad deltaTry it
preventDefault()Stop default scroll/zoom (needs non-passive listener)Try it
addEventListenerSame event; can set passiveTry it

Live Preview

Move the pointer over the box and use the mouse wheel or a trackpad scroll gesture. The log shows deltaY (and deltaX when present).

Wheel target

Works best with a mouse wheel or trackpad. Touch-only devices often have no wheel input.

Hover here and roll the wheel
Deltas

Waiting for wheel…

HTML Examples

Short demos. Use a mouse wheel or trackpad over the target. Touch phones may show nothing.

Example 1 — Log deltaY

Show wheel deltas without canceling the default action.

HTML
<p id="out">Waiting…</p>
<div id="box" tabindex="0"
  style="border:1px solid #ccc;padding:1rem;max-width:16rem"
  onwheel="document.getElementById('out').textContent =
    'deltaY=' + event.deltaY + ', deltaX=' + event.deltaX">
  Roll the wheel over this box
</div>
Try It Yourself

How It Works

  1. Pointer over the target element.
  2. Wheel / trackpad fires wheel.
  3. deltaY / deltaX describe the gesture (not guaranteed scroll direction).
  4. Default scroll still happens unless you cancel.

Example 2 — Scale with the Wheel

Cancel default scrolling and zoom the element (study demo — needs a non-passive listener in JS).

HTML
<div id="zoom">Scale me with the wheel</div>
<script>
  var scale = 1;
  var el = document.getElementById("zoom");
  el.addEventListener("wheel", function (event) {
    event.preventDefault();
    scale += event.deltaY * -0.01;
    if (scale < 0.5) scale = 0.5;
    if (scale > 2) scale = 2;
    el.style.transform = "scale(" + scale + ")";
  }, { passive: false });
</script>
Try It Yourself

How It Works

  1. passive: false allows preventDefault().
  2. Default scroll/zoom is blocked for this gesture.
  3. deltaY adjusts a scale factor.
  4. transform: scale() updates the element size.

Example 3 — addEventListener("wheel", …, { passive: true })

Preferred modern form when you only observe the gesture.

HTML
<p id="status">Waiting…</p>
<div id="panel" tabindex="0"
  style="border:1px solid #ccc;padding:1rem;max-width:16rem">
  Wheel here (passive listener)
</div>
<script>
  document.getElementById("panel").addEventListener("wheel", function (event) {
    document.getElementById("status").textContent =
      "deltaY: " + event.deltaY;
  }, { passive: true });
</script>
Try It Yourself

How It Works

  1. Listener attaches with passive: true.
  2. Browser can scroll without waiting to cancel.
  3. Handler only reads deltaY.
  4. Use this unless you intentionally override default behavior.

Edge Cases & Pitfalls

Most mistakes mix up wheel with scroll, or block scrolling for performance and accessibility.

scroll

Do not use delta* as scroll direction

MDN: detect scrollTop / scrollLeft changes in scroll instead.

passive

Canceling wheel can hurt scroll performance

Prefer { passive: true }. Only use passive: false when you must preventDefault().

legacy

Avoid onmousewheel

Deprecated non-standard predecessor. Use wheel / onwheel.

touch

No wheel on many phones

Provide buttons, pinch, or other controls — do not rely on wheel alone.

a11y

Do not trap scrolling without a plan

Blocking wheel for custom zoom can confuse keyboard and assistive users. Offer alternatives.

Browser Support

onwheel is an HTML event attribute for the standard wheel event. It is not a Node.js, Deno, or Bun API.

✓ Widely available on desktop

onwheel

Supported in modern desktop browsers. Mousewheel is obsolete. Touch-first devices may never fire wheel — design a fallback.

96% Global support (approx.)
Google Chrome Yes
Full support
Mozilla Firefox Yes
Full support
Apple Safari Yes
Full support
Microsoft Edge Yes
Full support
Opera Yes
Full support
Samsung Internet Yes
Full support
Internet Explorer 9+
Supported
onwheel Widely supported

Bottom line: Safe to use for desktop wheel/trackpad gestures. Pair with scroll listeners for position, and never depend on wheel alone for mobile.

Key Takeaways

  • Role: react to mouse wheel / trackpad wheel input.
  • Not scroll — use onscroll for position changes.
  • Prefer { passive: true } unless canceling.
  • Skip deprecated onmousewheel.

One line: onwheel reports wheel/trackpad gestures — not the same as scrolling, and not a substitute for mobile touch UX.

Frequently Asked Questions

It is an event handler for the wheel event. It runs when the user rotates a mouse wheel or performs a similar trackpad gesture over the element.
wheel is input (device rotation). scroll means the scroll position changed. Wheel can fire without scrolling; scrolling can happen without a wheel (keyboard, scrollbar, script).
WheelEvent properties for vertical and horizontal wheel deltas. Signs and units vary by device and deltaMode — do not treat them as the scroll direction of the page.
No. mousewheel is a non-standard deprecated predecessor. Use the standard wheel event / onwheel instead.
Wheel handlers that may call preventDefault can delay scrolling. addEventListener("wheel", handler, { passive: true }) tells the browser you will not cancel, which helps performance. Use passive: false only when you must preventDefault (for example custom zoom).
No. wheel is the modern replacement for mousewheel. Support is broad on desktop; treat touch-first UIs carefully because there is no wheel hardware.

Next: open

Control whether <details> or <dialog> starts open.

open tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful