HTML onscroll Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

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.

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.

Attribute Values

The value is a script (event handler code). Common uses:

TargetWhat to readTry it
Overflow boxthis.scrollTop / scrollLeftTry it
Page / windowwindow.scrollY / scrollXTry it
addEventListenerSame event without the HTML attributeTry it

Live Preview

Scroll inside the box. The log shows scrollTop as it changes.

Scroll box

The box needs overflow + fixed height so it can scroll. That CSS is part of making onscroll useful.

Line 1 — scroll me

Line 2

Line 3

Line 4

Line 5

Line 6

Line 7

Line 8

Line 9

Line 10

Position

scrollTop: 0

HTML Examples

Short demos. Overflow CSS is included only so the box can scroll — required for studying onscroll.

Example 1 — onscroll on an Overflow Box

Show scrollTop while scrolling inside a tall panel.

HTML
<p id="pos">scrollTop: 0</p>
<div id="box" style="height:6rem;overflow:auto;border:1px solid #ccc"
  onscroll="document.getElementById('pos').textContent =
    'scrollTop: ' + this.scrollTop">
  <p>Line 1</p><p>Line 2</p><p>Line 3</p>
  <p>Line 4</p><p>Line 5</p><p>Line 6</p>
  <p>Line 7</p><p>Line 8</p>
</div>
Try It Yourself

How It Works

  1. Overflow + height make the div scrollable.
  2. User scrolls inside the box.
  3. onscroll runs; this is the scrolling element.
  4. scrollTop updates the status line.

Example 2 — Page Scroll with window.scrollY

Listen on the document view and show how far the page has scrolled.

HTML
<p id="y" style="position:sticky;top:0;background:#fff">scrollY: 0</p>
<p>Scroll the page…</p>
<p style="height:40rem">Tall content</p>
<script>
  window.addEventListener("scroll", function () {
    document.getElementById("y").textContent =
      "scrollY: " + Math.round(window.scrollY);
  });
</script>
Try It Yourself

How It Works

  1. Page scroll is tracked on window / document.
  2. scrollY is the vertical offset of the viewport.
  3. Sticky label stays visible while you scroll (demo only).
  4. body onscroll can work similarly as an HTML attribute form.

Example 3 — addEventListener("scroll")

Same element scroll without the HTML attribute — preferred in modern scripts.

HTML
<p id="out">Waiting…</p>
<div id="panel" style="height:6rem;overflow:auto;border:1px solid #ccc">
  <p>A</p><p>B</p><p>C</p><p>D</p>
  <p>E</p><p>F</p><p>G</p><p>H</p>
</div>
<script>
  document.getElementById("panel").addEventListener("scroll", function () {
    document.getElementById("out").textContent =
      "scrollTop: " + this.scrollTop;
  });
</script>
Try It Yourself

How It Works

  1. Listener attaches to the scrolling panel.
  2. scroll fires when scrollTop changes.
  3. this.scrollTop is written to the status line.
  4. No bubbling — the listener must be on that panel.

Edge Cases & Pitfalls

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.

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 Chrome Yes
Full support
Mozilla Firefox Yes
Full support
Apple Safari Yes
Full support
Microsoft Edge Yes
Full support
Opera Yes
Full support
Samsung Internet Yes
Full support
Internet Explorer Yes
Supported
onscroll Widely supported

Bottom line: Safe to use. Attach to the scrolling element, read scrollTop/scrollY, and avoid expensive work on every event.

Key Takeaways

  • Role: run code when scroll position changes.
  • Read scrollTop / 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.

Next: onsearch

Handle search events on input type="search".

onsearch tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

8 people found this page helpful