JavaScript Document scrollsnapchange Event

Beginner
5 min read
Updated: Nov 2026
3 examples
Event

Definition

In JavaScript, document scrollsnapchange fires on the document scroll container when scrolling ends and a new scroll snap target is selected (MDN). The overall document must be the snap container (scroll-snap-type on <html>).

Register a listener with addEventListener. Check readiness and browser support before you rely on this event.

Syntax

Listen for scrollsnapchange. Start with this line.

JavaScript
addEventListener("scrollsnapchange", …)
CallWhat it does
addEventListener("scrollsnapchange", …)Primary signature.

Built into the browser. Listen with document.addEventListener("scrollsnapchange", …) — no import required.

Library declarations
JavaScript
addEventListener("scrollsnapchange", …)

Parameters

ParameterDescription
scroll-snap-type on htmlRequired. Makes the document the snap container (MDN)
scroll-snap-alignRequired. Marks snap children
ListenerRequired. addEventListener or onscrollsnapchange
SnapEventRequired. snapTargetBlock / snapTargetInline

Return Value

Return ValueDescription
*Fires as a Document event (listener receives the Event object).

JavaScript Examples

Example 1 — Mark snapTargetBlock (MDN)

MDN pattern: add a selected class when a new snap target is chosen.

JavaScript
// Requires: scroll-snap-type on html + scroll-snap-align on children
document.addEventListener("scrollsnapchange", (event) => {
  document.querySelectorAll(".selected").forEach((el) => {
    el.classList.remove("selected");
  });
  if (event.snapTargetBlock) {
    event.snapTargetBlock.classList.add("selected");
  }
});
Try It Yourself

How It Works

  1. Listen on document — fires when a new snap target is selected (MDN).
  2. Clear old selection — remove selected from previous targets.
  3. Read snapTargetBlock — the element snapped in the block axis (MDN).
  4. Style it — add selected for a highlight or animation.

Example 2 — onscrollsnapchange Property

Same idea using the event handler property; log the target id.

JavaScript
document.onscrollsnapchange = (event) => {
  const el = event.snapTargetBlock;
  console.log(el ? el.id || el.tagName : "(none)");
};
Try It Yourself

How It Works

  1. Assign onscrollsnapchange — MDN supports the handler property.
  2. Read the block target from the SnapEvent.
  3. Log id or tagName for a quick identity check.
  4. Note: a later assignment replaces the previous on* handler.

Example 3 — Feature Detect

Experimental / Limited — gate listeners before relying on the event.

JavaScript
if ("onscrollsnapchange" in document) {
  document.addEventListener("scrollsnapchange", (event) => {
    console.log("snapped:", event.snapTargetBlock);
  });
} else {
  console.log("scrollsnapchange not supported");
}
Try It Yourself

How It Works

  1. Detect with "onscrollsnapchange" in document.
  2. Supported path — attach the real listener.
  3. Unsupported path — skip or use a CSS-only snap UI without the event.
  4. Why — MDN: Experimental and Limited availability.

Browser Support

Document scrollsnapchange is marked Experimental with Limited availability on MDN (not Baseline). Check compatibility and feature-detect.

✓ Experimental · Limited availability

Document scrollsnapchange

Fires when a new document snap target is selected. Requires scroll-snap-type on html.

Limited Check compat
Google Chrome Check BCD / Chromium
Partial
Microsoft Edge Follow Chromium
Partial
Mozilla Firefox Limited / check BCD
Limited
Apple Safari Limited / check BCD
Limited
Opera Follow Chromium
Partial
Internet Explorer No
No
scrollsnapchange Limited

Bottom line: Use with feature detection. See the MDN scrollsnapchange reference.

Frequently Asked Questions

MDN: it fires on the document scroll container at the end of a scrolling operation when a new scroll snap target is selected. The document (html) must be the scroll snap container.
MDN marks it Experimental and Limited availability (not Baseline). It is not Deprecated or Non-standard. Feature-detect before production use.
A SnapEvent (inherits Event). Use snapTargetBlock and/or snapTargetInline for the newly selected snap target element (MDN).
Set scroll-snap-type on the html element (for example block mandatory) and scroll-snap-align on snap children (MDN).
scrollend means scrolling finished. scrollsnapchange means a new snap target was selected at the end of scrolling. You often use both.
Same idea when an Element is the snap container (overflow + scroll-snap-type on that element). Document scrollsnapchange is for when html is the snap container.

Next: scrollsnapchanging

Continue with the Document scrollsnapchanging event.

scrollsnapchanging →

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.

10 people found this page helpful