HTML onwheel Attribute

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
Events & Handlers

Introduction

The onwheel attribute is the standard inline event handler for the wheel event. It runs JavaScript when the user turns a mouse wheel, scrolls a trackpad, or performs a similar scroll gesture over an element. Pass event to the handler and read event.deltaY (vertical scroll amount; positive usually means scroll down). Use it for custom zoom, font-size controls, image carousels, or logging scroll direction. It is not the same as onscroll (position changed) or legacy onmousewheel (non-standard). Prefer addEventListener("wheel", …) in production.

What You’ll Learn

01

Event handler

Inline JS.

02

wheel event

Standard API.

03

deltaY

Scroll amount.

04

Custom UX

Zoom, resize.

05

addEventListener

Preferred way.

06

vs scroll

Different event.

Purpose of onwheel Attribute

The primary purpose of onwheel is to run custom JavaScript when a wheel gesture happens over an element — before or instead of default scrolling. Maps, image viewers, and design tools often use the wheel event for zoom and pan.

The handler receives a WheelEvent with deltaY, deltaX, and deltaMode. You can react to direction, adjust UI values, or call event.preventDefault() to block default scroll (only when necessary, and with { passive: false } when using addEventListener).

💡
Standard replacement for onmousewheel

onmousewheel is legacy and not supported in Firefox. Use onwheel="handleWheel(event)" in all new HTML.

📝 Syntax

Set onwheel on any element, or assign element.onwheel in JavaScript:

onwheel.html
<div onwheel="handleWheel(event)" tabindex="0">
  Scroll wheel here
</div>

<script>
  box.onwheel = handleWheel;
  box.addEventListener("wheel", handleWheel);
</script>

Syntax Rules

  • Value is JavaScript executed when the wheel event fires.
  • Pass event to read deltaY, deltaX, deltaMode.
  • Works on most elements — div, p, canvas, etc.
  • Add tabindex="0" on non-interactive elements so they can receive focus in demos.
  • Not the same as onscroll (fires when scroll position changes).
  • Also assignable as element.onwheel in script.
  • Modern alternative: el.addEventListener("wheel", handler).
  • For preventDefault(), use addEventListener("wheel", fn, { passive: false }).

💎 Values

The onwheel attribute accepts a string of JavaScript code:

  • onwheel="handleWheel(event)" — Call a named function with the event object.
  • onwheel="status.textContent = event.deltaY" — Inline update.
  • JavaScript: box.onwheel = (e) => { … } — property assignment.
wheel-deltaY.html
function handleWheel(event) {
  if (event.deltaY > 0) {
    status.textContent = "Scrolled down (deltaY: " + event.deltaY + ")";
  } else if (event.deltaY < 0) {
    status.textContent = "Scrolled up (deltaY: " + event.deltaY + ")";
  }
}

⚡ Quick Reference

Property / eventWhen it firesNotes
wheel / onwheelWheel or trackpad gestureStandard — use in new code
event.deltaYVertical scroll amount+ down, − up (typical)
event.deltaXHorizontal scroll amountTrackpad sideways scroll
mousewheel / onmousewheelLegacy wheel turnNot in Firefox — avoid
scroll / onscrollScroll position changedDifferent from wheel

Applicable Elements

TargetSupported?Notes
<div> scroll areaYesoverflow:auto + onwheel
<p>, <span>YesFont-size or zoom demos
<canvas>, mapsYesCustom zoom/pan
document / windowVia JSaddEventListener("wheel", …)
Touch-only devicesLimitedWheel is pointer-driven; provide touch alternatives

onwheel vs onmousewheel vs onscroll

HandlerWhen it firesTypical use
onwheelWheel / trackpad gestureCustom zoom, read deltaY
onmousewheelLegacy wheel turnOld IE-era code only
onscrollScroll position changedInfinite scroll, progress bar

Examples Gallery

Change font size with wheel, dynamic assignment, and a zoom counter with addEventListener.

👀 Live Preview

Scroll the wheel over the box — direction and deltaY update live:

Wheel over this area.

Extra lines so the box can scroll when content overflows.

Line 3

Line 4

Line 5

deltaY: —

Example — onwheel to change font size

Adjust text size incrementally when the user scrolls over a paragraph:

p-onwheel-fontsize.html
<p id="scrollText" onwheel="changeFontSize(event)">
  Scroll to change font size!
</p>
<p id="status">Font size: 16px</p>

<script>
  function changeFontSize(event) {
    var el = document.getElementById("scrollText");
    var size = parseFloat(getComputedStyle(el).fontSize);
    if (event.deltaY < 0) size += 2;
    else if (event.deltaY > 0) size -= 2;
    size = Math.max(12, Math.min(32, size));
    el.style.fontSize = size + "px";
    document.getElementById("status").textContent = "Font size: " + size + "px";
  }
</script>
Try It Yourself

How It Works

Each wheel tick fires changeFontSize. The sign of event.deltaY picks the direction; values are clamped between 12px and 32px.

Dynamic Values with JavaScript

Assign element.onwheel at runtime:

dynamic-onwheel.html
<div id="dynamicElement" tabindex="0">Scroll wheel here</div>

<script>
  document.getElementById("dynamicElement").onwheel = function (event) {
    log.textContent = "Wheel detected — deltaY: " + event.deltaY;
  };
</script>
Try It Yourself

How It Works

Assigning element.onwheel is equivalent to the inline attribute. The handler runs on every wheel gesture over that element.

Example — Zoom counter from wheel direction

Increase or decrease a zoom value with addEventListener("wheel", …):

zoom-counter.html
var zoom = 100;

box.addEventListener("wheel", function (event) {
  if (event.deltaY < 0) zoom += 10;
  else if (event.deltaY > 0) zoom -= 10;
  zoom = Math.max(50, Math.min(200, zoom));
  box.textContent = zoom + "%";
  box.style.transform = "scale(" + (zoom / 100) + ")";
});
Try It Yourself

How It Works

Read the sign of deltaY to pick direction. addEventListener is the recommended production pattern for wheel handlers.

♿ Accessibility

  • Do not trap scroll — Avoid preventDefault() on wheel unless necessary; it blocks normal page scroll.
  • Provide keyboard alternatives — +/- keys or buttons for zoom when wheel-only controls exist.
  • Respect reduced motion — Check prefers-reduced-motion before wheel-driven animations.
  • Focusable targets — Add tabindex="0" if a non-interactive div must receive wheel focus for demos.
  • Touch users — Wheel events are pointer-driven; offer pinch or button controls on touch devices.

🧠 How onwheel Works

1

Pointer over element

User on target.

Focus
2

Wheel turned

Mouse or trackpad.

Input
3

wheel fires

onwheel runs.

Event
=

Custom action

Zoom, resize, log.

Browser Support

The wheel event and onwheel handler are supported in all modern browsers. They are the standard replacement for legacy onmousewheel.

Standard Event · Fully supported

Universal onwheel support

All major browsers fire wheel including Firefox, unlike legacy mousewheel.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer IE 9+ supported
Full support
Opera Fully supported
Full support
onwheel attribute 99% supported

Bottom line: Use onwheel instead of legacy onmousewheel in all new projects.

💡 Best Practices

✅ Do

  • Use onwheel (not onmousewheel) in new HTML
  • Pass event and read deltaY / deltaX
  • Use addEventListener("wheel", …) in production
  • Clamp zoom/size values to sensible ranges
  • Offer keyboard or button alternatives for wheel-only UX

❌ Don’t

  • Rely on console.log for user feedback
  • Confuse onwheel with onscroll
  • Block page scroll with preventDefault() without good reason
  • Use legacy onmousewheel in new Firefox-facing code
  • Assume every user has a mouse wheel

Conclusion

The onwheel attribute runs JavaScript when a wheel or trackpad scroll gesture happens over an element — the standard way to build custom zoom, resize, and scroll-direction features.

Read event.deltaY for direction, prefer addEventListener("wheel", …) in production, and use onwheel instead of legacy onmousewheel.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onwheel

Bookmark these before adding wheel handlers.

5
Core concepts
02

deltaY

Direction.

Property
🚫 03

Not onscroll

Different.

Gotcha
🔍 04

Custom zoom

Common use.

Pattern
05

Not onmousewheel

Legacy only.

Migrate

❓ Frequently Asked Questions

It runs JavaScript when the wheel event fires — when the user turns a mouse wheel, scrolls a trackpad, or performs a similar scroll gesture over an element.
A number on the WheelEvent object. Positive usually means scroll down; negative means scroll up. Magnitude depends on the device and browser.
No. onwheel is standard and works in Firefox. onmousewheel is legacy and non-standard. See the onmousewheel tutorial for migration notes.
No. wheel fires on the scroll gesture. scroll fires when an element’s scroll position actually changes (including keyboard or touch scrolling).
element.addEventListener("wheel", handler) is preferred in production — especially when you need { passive: false } for preventDefault().
Yes in all modern browsers. It is the standard replacement for legacy onmousewheel.

Master wheel event handlers

Practice the onwheel attribute with font-size, dynamic assignment, and zoom examples in the Try It editor.

Try onwheel demo →

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.

5 people found this page helpful