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

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.
Inline JS.
Standard API.
Scroll amount.
Zoom, resize.
Preferred way.
Different event.
onwheel AttributeThe 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).
onmousewheel is legacy and not supported in Firefox. Use onwheel="handleWheel(event)" in all new HTML.
Set onwheel on any element, or assign element.onwheel in JavaScript:
<div onwheel="handleWheel(event)" tabindex="0">
Scroll wheel here
</div>
<script>
box.onwheel = handleWheel;
box.addEventListener("wheel", handleWheel);
</script>wheel event fires.event to read deltaY, deltaX, deltaMode.div, p, canvas, etc.tabindex="0" on non-interactive elements so they can receive focus in demos.onscroll (fires when scroll position changes).element.onwheel in script.el.addEventListener("wheel", handler).preventDefault(), use addEventListener("wheel", fn, { passive: false }).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.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 | When it fires | Notes |
|---|---|---|
wheel / onwheel | Wheel or trackpad gesture | Standard — use in new code |
event.deltaY | Vertical scroll amount | + down, − up (typical) |
event.deltaX | Horizontal scroll amount | Trackpad sideways scroll |
mousewheel / onmousewheel | Legacy wheel turn | Not in Firefox — avoid |
scroll / onscroll | Scroll position changed | Different from wheel |
| Target | Supported? | Notes |
|---|---|---|
<div> scroll area | Yes | overflow:auto + onwheel |
<p>, <span> | Yes | Font-size or zoom demos |
<canvas>, maps | Yes | Custom zoom/pan |
document / window | Via JS | addEventListener("wheel", …) |
| Touch-only devices | Limited | Wheel is pointer-driven; provide touch alternatives |
onwheel vs onmousewheel vs onscroll| Handler | When it fires | Typical use |
|---|---|---|
onwheel | Wheel / trackpad gesture | Custom zoom, read deltaY |
onmousewheel | Legacy wheel turn | Old IE-era code only |
onscroll | Scroll position changed | Infinite scroll, progress bar |
Change font size with wheel, dynamic assignment, and a zoom counter with addEventListener.
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: —
Adjust text size incrementally when the user scrolls over a paragraph:
<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>Each wheel tick fires changeFontSize. The sign of event.deltaY picks the direction; values are clamped between 12px and 32px.
Assign element.onwheel at runtime:
<div id="dynamicElement" tabindex="0">Scroll wheel here</div>
<script>
document.getElementById("dynamicElement").onwheel = function (event) {
log.textContent = "Wheel detected — deltaY: " + event.deltaY;
};
</script>Assigning element.onwheel is equivalent to the inline attribute. The handler runs on every wheel gesture over that element.
Increase or decrease a zoom value with addEventListener("wheel", …):
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) + ")";
});Read the sign of deltaY to pick direction. addEventListener is the recommended production pattern for wheel handlers.
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.
Mouse or trackpad.
onwheel runs.
Zoom, resize, log.
The wheel event and onwheel handler are supported in all modern browsers. They are the standard replacement for legacy onmousewheel.
onwheel supportAll major browsers fire wheel including Firefox, unlike legacy mousewheel.
Bottom line: Use onwheel instead of legacy onmousewheel in all new projects.
onwheel (not onmousewheel) in new HTMLevent and read deltaY / deltaXaddEventListener("wheel", …) in productionconsole.log for user feedbackonwheel with onscrollpreventDefault() without good reasononmousewheel in new Firefox-facing codeThe 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.
onwheelBookmark these before adding wheel handlers.
wheel / onwheel.
APIDirection.
PropertyDifferent.
GotchaCommon use.
PatternLegacy only.
Migratewheel event fires — when the user turns a mouse wheel, scrolls a trackpad, or performs a similar scroll gesture over an element.WheelEvent object. Positive usually means scroll down; negative means scroll up. Magnitude depends on the device and browser.onwheel is standard and works in Firefox. onmousewheel is legacy and non-standard. See the onmousewheel tutorial for migration notes.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().onmousewheel.Practice the onwheel attribute with font-size, dynamic assignment, and zoom examples in the Try It editor.
5 people found this page helpful