MDN self-hosted library · e01.org

Window: scrollsnapchanging event

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

html
<main>
  <!-- Significant content -->
</main>
css
main {
  width: 250px;
  height: 450px;
  overflow: scroll;
}
css
html {
  scroll-snap-type: block mandatory;
}
js
window.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");
});