The onscroll attribute is an event handler for the scroll event. It runs when that element’s scroll position changes — for example a box with overflow — or, on body / the document view, when the page scrolls.
Read position with element.scrollTop / scrollLeft, or window.scrollY / scrollX for the page. Related: onwheel (wheel input) and onresize.
Baseline · Widely available.
Not deprecated. Prefer addEventListener("scroll", …) in scripts; the HTML attribute is the same handler.
Scroll fires often — keep handlers cheap.
Avoid heavy DOM work on every scroll. Throttle with setTimeout when needed. For “element entered the viewport,” prefer Intersection Observer instead of scroll math.
Syntax
HTML Syntax
syntax.html
<div onscroll="handler">
Modern equivalent: el.addEventListener("scroll", function (event) { … });. Put the handler on the element that actually scrolls — scroll does not bubble.
Values
Attribute Values
The value is a script (event handler code). Common uses:
Most bugs are listening on the wrong element or doing too much work per scroll.
bubble
Scroll does not bubble
A parent onscroll will not see a child panel scroll. Attach to the scrolling element (or use capture carefully).
perf
Throttle heavy work
MDN: use your own timeout throttle. requestAnimationFrame alone matches scroll rate, so it does not throttle.
observe
Prefer Intersection Observer for visibility
Lazy-load / “in view” checks are clearer and cheaper than scroll-position math.
wheel
Wheel ≠ scroll
onwheel can fire when nothing moves (e.g. at the end of a page). onscroll means position changed.
end
Need “scroll finished”?
Look at the newer scrollend event where supported — separate from continuous scroll.
Compatibility
Browser Support
onscroll is an HTML event attribute for the scroll event on elements and the document. It is not a Node.js, Deno, or Bun API.
✓ Baseline · Widely available
onscroll
Supported everywhere modern browsers scroll. Keep handlers light; use Intersection Observer for viewport checks when possible.
98%Global support (approx.)
Google ChromeYes
Full support
Mozilla FirefoxYes
Full support
Apple SafariYes
Full support
Microsoft EdgeYes
Full support
OperaYes
Full support
Samsung InternetYes
Full support
Internet ExplorerYes
Supported
onscrollWidely supported
Bottom line: Safe to use. Attach to the scrolling element, read scrollTop/scrollY, and avoid expensive work on every event.
Remember
Key Takeaways
Role: run code when scroll position changes.
ReadscrollTop / scrollY for position.
Does not bubble — listen on the scroller.
Keep it light — throttle or use Intersection Observer.
One line:onscroll fires when an element or the page actually scrolls — attach it to the scroller and keep the handler cheap.
Frequently Asked Questions
It is an event handler for the scroll event. It runs when the user (or script) scrolls that element, or — on body/document/window — when the document view scrolls.
For an element use element.scrollTop and element.scrollLeft. For the page use window.scrollY and window.scrollX (or document.documentElement.scrollTop).
No. Scroll does not bubble. Put onscroll on the element that actually scrolls (or listen on document/window for page scroll). You can still use capture if needed.
Scroll fires very often. Keep the handler light. Throttle heavy work with setTimeout (MDN notes requestAnimationFrame alone does not throttle scroll). For “is this in view?” prefer Intersection Observer.
No. It is Baseline widely available. Prefer addEventListener("scroll", …) in modern scripts; the HTML attribute is the same handler.
onwheel fires for wheel/trackpad input even if the page does not move. onscroll fires when the scroll position actually changes.