The HTML onscrollend event
The HTML onscrollend attribute runs JavaScript when scrolling finishes and the scroll position stops changing. It is an inline handler for the scrollend event; in modern code prefer addEventListener('scrollend', …).
Overview
The onscrollend event attribute runs JavaScript when scrolling ends. In JavaScript the event itself is named scrollend — drop the on prefix when you call addEventListener.
It is a scroll event, fired on a scrollable element or the document as the user scrolls. Scroll events can fire rapidly, so heavy work in the handler can hurt performance — throttle it, or prefer the more efficient IntersectionObserver where you can.
You can wire this up with the inline onscrollend HTML attribute, but the modern, recommended approach is element.addEventListener('scrollend', handler) in JavaScript. That keeps behavior out of your markup, lets you attach several handlers to the same event, and makes them easy to remove. The inline attribute is fine for quick demos.
Syntax
<element onscrollend="handler()">…</element>
element.addEventListener('scrollend', handler);
Best practices
- Prefer
element.addEventListener('scrollend', handler)over the inlineonscrollendattribute — it separates behavior from markup and allows multiple handlers. - Throttle or debounce scroll handlers, or use
requestAnimationFrame, since they fire rapidly. - Prefer
IntersectionObserverfor "is it in view" checks rather than scroll math. - Use the CSS scroll-behavior for smooth scrolling instead of scripting it.
Frequently asked questions
What is the onscrollend event?
scrollend.Why is my scroll handler slow?
requestAnimationFrame callback.What can I use instead of a scroll listener?
IntersectionObserver is more efficient than listening to scroll.Should I use the onscrollend attribute or addEventListener?
addEventListener('scrollend', …) in JavaScript. The inline onscrollend attribute works but mixes behavior into the markup and allows only one handler per element.