👀 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: —

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.
onmousewheel.
Standard way.
Scroll amount.
Zoom, counter.
Preferred way.
Different event.
onmousewheel AttributeThe 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.
onmousewheel is non-standard and not supported in Firefox. The standard inline attribute is onwheel="handleWheel(event)".
Legacy onmousewheel and standard onwheel side by side:
<!-- Legacy (avoid in new projects) -->
<div onmousewheel="handleWheel(event)">…</div>
<!-- Standard (preferred) -->
<div onwheel="handleWheel(event)" tabindex="0">
Scroll wheel here
</div>onmousewheel is legacy; prefer onwheel for new HTML.event to read deltaY, deltaX, deltaMode.overflow:auto on tall content when testing scroll.scroll event (position changed vs wheel turned).el.addEventListener("wheel", handler).preventDefault(), register with { passive: false } in script.The attribute accepts a string of JavaScript code:
onwheel="handleWheel(event)" — Standard handler with event object.onmousewheel="handleWheel(event)" — Legacy equivalent (limited browser support).box.onwheel = (e) => { … } — property assignment.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 + ")";
}
}| Property / event | Standard? | Notes |
|---|---|---|
wheel / onwheel | Yes | Use in modern code |
mousewheel / onmousewheel | No (legacy) | Not in Firefox |
event.deltaY | wheel event | + down, − up (typical) |
event.deltaX | wheel event | Horizontal scroll |
scroll event | Yes | Fires when scroll position changes |
| Target | Supported? | Notes |
|---|---|---|
<div> scroll area | Yes | overflow:auto + onwheel |
<canvas>, maps | Yes | Custom zoom/pan |
document / window | Via JS | Global wheel listeners |
onmousewheel in Firefox | No | Use onwheel |
| Touch/trackpad | wheel event | Also fires for many pointer devices |
onmousewheel vs onwheel vs onscroll| Handler | When it fires | Typical use |
|---|---|---|
onmousewheel | Legacy wheel turn | Old IE-era code only |
onwheel | Wheel / trackpad gesture | Custom zoom, intercept scroll |
onscroll | Scroll position changed | Infinite scroll, progress bar |
Read deltaY on wheel, addEventListener on a scroll box, and adjust a zoom counter from wheel direction.
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: —
Show scroll direction when the wheel moves over a div:
<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>Each wheel tick fires the handler. deltaY tells you direction and magnitude.
Attach with addEventListener("wheel", …):
<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>Register once in script. The listener runs on every wheel gesture over the element.
Increase or decrease a value based on deltaY:
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 + "%";
});Read sign of deltaY to pick direction. Clamp values so zoom stays in a sensible range.
preventDefault() on wheel unless necessary; it blocks normal page scroll.prefers-reduced-motion before wheel-driven animations.tabindex="0" if a non-interactive div must receive wheel focus for demos.User on target.
Trackpad or mouse.
onwheel runs.
Zoom, log deltaY.
onmousewheel is legacy and non-standard (not supported in Firefox). The standard onwheel attribute and wheel event are supported in all modern browsers.
onwheel · Fully supportedwheel over onmousewheelLegacy onmousewheel works in some Chromium browsers; onwheel works everywhere modern.
onwheel supportBottom line: Teach onmousewheel as legacy; write new code with onwheel / wheel.
onwheel / wheel in new projectsevent.deltaY and event.deltaXaddEventListener("wheel", …) in productiononmousewheel alone — Firefox breakswheelDelta (legacy) equals deltaYconsole in user-facing demoswheel with scroll eventsThe 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.
onmousewheelBookmark these before handling wheel input.
Non-standard.
HistoryUse this.
StandardDirection.
APIDifferent.
CompareFor zoom UX.
A11yonwheel instead.onwheel in new projects. It is standard and works in Firefox.wheel event only.wheel is the input gesture. scroll fires when scroll position actually changes.addEventListener("wheel", handler) is preferred, especially when you need { passive: false } for preventDefault().Practice standard onwheel handlers with deltaY examples in the Try It editor.
5 people found this page helpful