HTML onmousewheel Attribute

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

Introduction

The onmousewheel attribute is a legacy inline event handler for the non-standard mousewheel event. It runs JavaScript when the user turns the mouse wheel over an element. You may still see it in older tutorials and IE-era code. For new projects, use the standard onwheel attribute and wheel event instead — they work in Firefox and all modern browsers. Pass event and read event.deltaY (vertical scroll amount; positive usually means scroll down). Use a scrollable container or focus the element so wheel events are easy to test.

What You’ll Learn

01

Legacy handler

onmousewheel.

02

onwheel

Standard way.

03

deltaY

Scroll amount.

04

Custom UX

Zoom, counter.

05

addEventListener

Preferred way.

06

vs scroll

Different event.

Purpose of onmousewheel Attribute

The original purpose of onmousewheel was to run custom JavaScript when the user scrolls the wheel — for example custom zoom, image carousels, or intercepting scroll inside a map. It predates the standardized wheel event.

Today, treat onmousewheel as historical. Write new code with onwheel or addEventListener("wheel", …). The handler receives a WheelEvent with deltaY, deltaX, and deltaMode.

💡
Use onwheel in new code

onmousewheel is non-standard and not supported in Firefox. The standard inline attribute is onwheel="handleWheel(event)".

📝 Syntax

Legacy onmousewheel and standard onwheel side by side:

onmousewheel-vs-onwheel.html
<!-- Legacy (avoid in new projects) -->
<div onmousewheel="handleWheel(event)"></div>

<!-- Standard (preferred) -->
<div onwheel="handleWheel(event)" tabindex="0">
  Scroll wheel here
</div>

Syntax Rules

  • onmousewheel is legacy; prefer onwheel for new HTML.
  • Value is JavaScript executed when a wheel gesture occurs over the element.
  • Pass event to read deltaY, deltaX, deltaMode.
  • Works on most elements; use overflow:auto on tall content when testing scroll.
  • Not the same as the scroll event (position changed vs wheel turned).
  • Modern alternative: el.addEventListener("wheel", handler).
  • To call preventDefault(), register with { passive: false } in script.

💎 Values

The attribute accepts a string of JavaScript code:

  • onwheel="handleWheel(event)" — Standard handler with event object.
  • onmousewheel="handleWheel(event)" — Legacy equivalent (limited browser support).
  • 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 / eventStandard?Notes
wheel / onwheelYesUse in modern code
mousewheel / onmousewheelNo (legacy)Not in Firefox
event.deltaYwheel event+ down, − up (typical)
event.deltaXwheel eventHorizontal scroll
scroll eventYesFires when scroll position changes

Applicable Elements

TargetSupported?Notes
<div> scroll areaYesoverflow:auto + onwheel
<canvas>, mapsYesCustom zoom/pan
document / windowVia JSGlobal wheel listeners
onmousewheel in FirefoxNoUse onwheel
Touch/trackpadwheel eventAlso fires for many pointer devices

onmousewheel vs onwheel vs onscroll

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

Examples Gallery

Read deltaY on wheel, addEventListener on a scroll box, and adjust a zoom counter from wheel direction.

👀 Live Preview

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

Wheel over this area.

Extra lines so the box can scroll internally when content overflows.

Line 3

Line 4

Line 5

deltaY: —

Example — Inline wheel handler (standard onwheel)

Show scroll direction when the wheel moves over a div:

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

<script>
  function handleWheel(event) {
    document.getElementById("output").textContent =
      "deltaY: " + event.deltaY;
  }
</script>
Try It Yourself

How It Works

Each wheel tick fires the handler. deltaY tells you direction and magnitude.

Dynamic Values with JavaScript

Attach with addEventListener("wheel", …):

dynamic-wheel.html
<div id="myElement" tabindex="0">Wheel here</div>

<script>
  document.getElementById("myElement").addEventListener("wheel", function (event) {
    document.getElementById("log").textContent =
      "Wheel: deltaY = " + event.deltaY;
  });
</script>
Try It Yourself

How It Works

Register once in script. The listener runs on every wheel gesture over the element.

Example — Zoom counter from wheel direction

Increase or decrease a value based on deltaY:

zoom-counter.html
let zoom = 100;

box.addEventListener("wheel", (event) => {
  if (event.deltaY < 0) zoom += 10;
  else if (event.deltaY > 0) zoom -= 10;
  zoom = Math.max(50, Math.min(200, zoom));
  label.textContent = "Zoom: " + zoom + "%";
});
Try It Yourself

How It Works

Read sign of deltaY to pick direction. Clamp values so zoom stays in a sensible range.

♿ 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 and trackpad users — Wheel events cover more than classic mouse wheels; test on laptops.

🧠 How onmousewheel / onwheel Works

1

Pointer over element

User on target.

Focus
2

Wheel turned

Trackpad or mouse.

Input
3

wheel fires

onwheel runs.

Event
=

Custom action

Zoom, log deltaY.

Browser Support

onmousewheel is legacy and non-standard (not supported in Firefox). The standard onwheel attribute and wheel event are supported in all modern browsers.

Use onwheel · Fully supported

Prefer standard wheel over onmousewheel

Legacy onmousewheel works in some Chromium browsers; onwheel works everywhere modern.

99% onwheel support
Google Chrome onwheel full · onmousewheel legacy
Use onwheel
Mozilla Firefox onwheel only
onwheel required
Apple Safari onwheel supported
Full support
Microsoft Edge onwheel supported
Full support
Internet Explorer onmousewheel (legacy)
Legacy only
Opera onwheel supported
Full support
onwheel (recommended) 99% supported

Bottom line: Teach onmousewheel as legacy; write new code with onwheel / wheel.

💡 Best Practices

✅ Do

  • Use onwheel / wheel in new projects
  • Read event.deltaY and event.deltaX
  • Use addEventListener("wheel", …) in production
  • Offer keyboard/button alternatives for custom zoom
  • Test on trackpads and touch devices

❌ Don’t

  • Rely on onmousewheel alone — Firefox breaks
  • Assume wheelDelta (legacy) equals deltaY
  • Log only to console in user-facing demos
  • Block wheel scroll without good reason
  • Confuse wheel with scroll events

Conclusion

The onmousewheel attribute is a legacy way to handle mouse wheel input. Understand it for old code, but use the standard onwheel attribute and wheel event in new work.

Read deltaY, prefer addEventListener, and avoid blocking default scroll unless your UX truly requires it.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onmousewheel

Bookmark these before handling wheel input.

5
Core concepts
02

onwheel

Use this.

Standard
🔢 03

deltaY

Direction.

API
🔄 04

Not scroll

Different.

Compare
05

Keyboard alt

For zoom UX.

A11y

❓ Frequently Asked Questions

It is a legacy inline handler for the non-standard mousewheel event. Modern code should use onwheel instead.
Always prefer onwheel in new projects. It is standard and works in Firefox.
Vertical scroll amount on the standard wheel event. Positive usually means scroll down; negative means scroll up.
No. Firefox supports the wheel event only.
No. wheel is the input gesture. scroll fires when scroll position actually changes.
Yes — addEventListener("wheel", handler) is preferred, especially when you need { passive: false } for preventDefault().

Master wheel event handlers

Practice standard onwheel handlers with deltaY examples in the Try It editor.

Try wheel 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