JavaScript Document scrollsnapchanging Event

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Document event
Limited availability
Experimental

What You’ll Learn

The Document scrollsnapchanging event fires when the browser decides a new scroll snap target is pending (it will be selected when the current gesture ends). Learn CSS setup on html, the MDN pending class pattern, and five try-it labs.

01

Kind

Document event

02

Type

SnapEvent

03

Means

Pending snap target

04

Needs

scroll-snap-type on html

05

Key prop

snapTargetBlock

06

Status

Experimental

Introduction

While the user is mid-gesture, the browser may already know which section will snap when they let go. That upcoming choice is a pending snap target—and that is when Document scrollsnapchanging fires.

Pair it with scrollsnapchange for the final selection after the scrolling operation ends. Both use SnapEvent.

💡
Beginner tip

For Document scrollsnapchanging, set scroll-snap-type on html (MDN). If snap lives on a scrolling <div>, use Element scrollsnapchanging instead.

Understanding Document scrollsnapchanging

A Document event that answers: “Is a new snap target pending for when this gesture ends?”

  • Fires when the browser determines a new scroll snap target is pending (MDN).
  • Pending means it will be selected when the current scroll gesture ends.
  • Requires the HTML document as snap container (scroll-snap-type on html).
  • Event typeSnapEvent (inherits from Event).
  • Handlerdocument.onscrollsnapchanging or addEventListener("scrollsnapchanging", ...).
  • Status — Experimental and Limited availability on MDN (not Baseline).

📝 Syntax

Use the event name with addEventListener, or set the handler property:

JavaScript
addEventListener("scrollsnapchanging", (event) => { });

onscrollsnapchanging = (event) => { };

Event type

A SnapEvent, which inherits from the generic Event type.

MDN-style handler

JavaScript
document.addEventListener("scrollsnapchanging", (event) => {
  // remove previously-set "pending" classes
  const pendingElems = document.querySelectorAll(".pending");
  pendingElems.forEach((elem) => {
    elem.classList.remove("pending");
  });

  // Set current pending snap target class to "pending"
  event.snapTargetBlock.classList.add("pending");
});

Minimal CSS setup (Document as snap container)

JavaScript
html {
  scroll-snap-type: block mandatory;
}

section {
  scroll-snap-align: start;
  min-height: 100vh;
}

🔍 What Is a SnapEvent?

SnapEvent extends Event with snap-target references so you can style or announce the pending snap target during a gesture.

PropertyBeginner meaning
snapTargetBlockElement pending as snap target on the block axis (often vertical)
snapTargetInlineElement pending on the inline axis (often horizontal), when applicable

⚡ Quick Reference

GoalCode / note
Listendocument.addEventListener("scrollsnapchanging", fn)
Handler propertydocument.onscrollsnapchanging = fn
Pending sectionevent.snapTargetBlock
Enable document snaphtml { scroll-snap-type: block mandatory; }
Sibling selected eventscrollsnapchange
MDN statusExperimental & Limited availability

🔍 At a Glance

Four facts to remember about Document scrollsnapchanging.

Event type
SnapEvent

Has snap targets

Means
Pending

Will snap next

CSS
on html

scroll-snap-type

Status
Experimental

Not Baseline

Examples Gallery

Examples follow MDN Document: scrollsnapchanging event. Labs use document-level CSS snap. Support is limited—feature-detect and scroll mid-gesture to see pending targets.

📚 Getting Started

MDN pending-class pattern and the handler property.

Example 1 — Add pending Class (MDN)

Clear old pending marks, then highlight event.snapTargetBlock.

JavaScript
document.addEventListener("scrollsnapchanging", (event) => {
  const pendingElems = document.querySelectorAll(".pending");
  pendingElems.forEach((elem) => {
    elem.classList.remove("pending");
  });

  event.snapTargetBlock.classList.add("pending");
});
Try It Yourself

How It Works

MDN removes previous pending classes first so only the latest pending target is styled while the gesture continues.

Example 2 — document.onscrollsnapchanging

Log the pending target id with the handler property.

JavaScript
const out = document.getElementById("out");

document.onscrollsnapchanging = (event) => {
  const t = event.snapTargetBlock;
  out.textContent = t
    ? "Pending #" + (t.id || t.tagName)
    : "scrollsnapchanging (no snapTargetBlock)";
};
Try It Yourself

How It Works

Prefer addEventListener for multiple listeners. The property form matches MDN’s syntax listing.

📈 Detect, Pair & Label

Feature detection and pending vs selected pairing.

Example 3 — Feature Detect

Check for onscrollsnapchanging before relying on the event.

JavaScript
const out = document.getElementById("out");
const supported = "onscrollsnapchanging" in document;

out.textContent = supported
  ? "scrollsnapchanging supported — scroll toward a snap"
  : "scrollsnapchanging missing — CSS snap may still work";

if (supported) {
  document.addEventListener("scrollsnapchanging", (event) => {
    out.textContent =
      "Pending: " + (event.snapTargetBlock && event.snapTargetBlock.id);
  });
}
Try It Yourself

How It Works

CSS snap can work without this JS event. Treat pending styling as progressive enhancement.

Example 4 — Pending + Selected Pair

Use pending during the gesture and selected after scrollsnapchange.

JavaScript
function clearClass(name) {
  document.querySelectorAll("." + name).forEach((el) => el.classList.remove(name));
}

if ("onscrollsnapchanging" in document) {
  document.addEventListener("scrollsnapchanging", (event) => {
    clearClass("pending");
    event.snapTargetBlock?.classList.add("pending");
  });
}

if ("onscrollsnapchange" in document) {
  document.addEventListener("scrollsnapchange", (event) => {
    clearClass("pending");
    clearClass("selected");
    event.snapTargetBlock?.classList.add("selected");
  });
}
Try It Yourself

How It Works

This is the clearest beginner model: changing = preview, change = commit.

Example 5 — Sticky “Next snap” Label

Show which section is pending from data-title.

JavaScript
const label = document.getElementById("label");

document.addEventListener("scrollsnapchanging", (event) => {
  const t = event.snapTargetBlock;
  if (!t) return;
  label.textContent =
    "Pending: " + (t.dataset.title || t.id || "section");
});
Try It Yourself

How It Works

Useful for slideshow UIs that preview the destination slide before the gesture ends.

🚀 Common Use Cases

  • Previewing which section will snap while the user is still scrolling.
  • Updating a “next snap” caption during the gesture.
  • Soft highlighting before the final scrollsnapchange commit.
  • Pairing pending + selected styles for clearer snap feedback.
  • Teaching pending vs selected scroll snap events.

🔧 How It Works

1

CSS makes html a snap container

scroll-snap-type on html; children use scroll-snap-align.

CSS
2

User scrolls

The browser snaps toward a valid target section.

Scroll
3

Pending target chosen

Browser decides which child will snap when the gesture ends.

Pending
4

scrollsnapchanging fires

Style the pending target (MDN pending class).

📝 Notes

  • MDN: Experimental and Limited availability—Experimental banner shown above.
  • Not Deprecated or Non-standard.
  • Document event needs snap on html; Element overflow snap uses Element events.
  • CSS snap can work without JS—use the event for enhancement only.
  • Related learning: securitypolicyviolation, scrollsnapchange, JavaScript hub.

Browser Support

Document scrollsnapchanging is marked Experimental and Limited availability on MDN (not Baseline). Logos use the shared browser-image-sprite.png sprite from this project. Feature-detect and keep CSS snap usable without the event.

Experimental · Limited availability

Document scrollsnapchanging

Fires when a new scroll snap target is pending (will be selected when the gesture ends).

Limited Check compat
Google Chrome Check BCD / Chromium scroll snap events
Partial
Mozilla Firefox Limited / check BCD
Limited
Apple Safari Limited / check BCD
Limited
Microsoft Edge Follow Chromium (check BCD)
Partial
Opera Follow Chromium behavior
Partial
Internet Explorer No scrollsnapchanging
Not supported
scrollsnapchanging Limited availability

Bottom line: Set scroll-snap-type on html, listen for scrollsnapchanging, style event.snapTargetBlock as pending, and pair with scrollsnapchange.

Conclusion

Document scrollsnapchanging previews the pending snap target during a gesture. Pair it with scrollsnapchange for the final selection. Keep CSS snap as the core UX.

Continue with securitypolicyviolation, scrollsnapchange, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Put scroll-snap-type on html for Document events
  • Read event.snapTargetBlock for the pending section
  • Feature-detect "onscrollsnapchanging" in document
  • Keep CSS snap usable without JS
  • Pair with scrollend when you also need settled position

❌ Don’t

  • Expect Baseline Widely available support today
  • Listen on Document for a nested overflow snap box
  • Assume every browser exposes SnapEvent
  • Rely on the event for core navigation (CSS first)
  • Ignore MDN Experimental / Limited availability warnings

Key Takeaways

Knowledge Unlocked

Five things to remember about Document scrollsnapchanging

Pending snap target — will be selected when the gesture ends.

5
Core concepts
🎨 02

SnapEvent

snapTargetBlock

API
💻 03

CSS on html

scroll-snap-type

Setup
🔍 04

Detect

before relying

Compat
⚠️ 05

Experimental

not Baseline

MDN

❓ Frequently Asked Questions

It fires on the scroll container when the browser determines a new scroll snap target is pending — it will be selected when the current scroll gesture ends. The Document must be the snap container (scroll-snap-type on html).
MDN marks Document scrollsnapchanging as Experimental and Limited availability (not Baseline). It is not Deprecated or Non-standard. Feature-detect before production use.
scrollsnapchanging means a snap target is pending during the gesture. scrollsnapchange means a new snap target was selected at the end of the scrolling operation. Pending first, then selected.
A SnapEvent, which inherits from Event. Use event.snapTargetBlock (and snapTargetInline when relevant) for the pending snap target element.
MDN’s example removes old pending classes, then adds pending to event.snapTargetBlock so only the current pending target is highlighted.
Same idea for an Element snap container. Document scrollsnapchanging is for when html is the scroll snap container.
Did you know?

Scroll snap itself is mostly CSS. Events like scrollsnapchanging exist so scripts can preview the upcoming snap target during a gesture—without guessing from raw scrollY math.

Next: Document securitypolicyviolation

Learn how CSP violations surface as Document events.

securitypolicyviolation →

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.

5 people found this page helpful