Examples

Each one live, with its code. They share one stylesheet — the look is yours; Spill only scrolls.

The examples share one stylesheet for the look; each adds its own touches. Drag them with a mouse, scroll them with a trackpad or a finger, reach them with the keyboard.

Basic

A row of slides with previous and next buttons and a dot for each snap — the API's way.

import Spill from "@bozdev/spill";

const root = document.querySelector(".carousel--basic");
const prev = root.querySelector(".carousel__button--prev");
const next = root.querySelector(".carousel__button--next");
const dots = root.querySelector(".carousel__dots");

const api = Spill(root.querySelector(".carousel__viewport"), { align: "start" });

// Previous and next: a snap each, off at the edges
prev.addEventListener("click", () => api.scrollPrev());
next.addEventListener("click", () => api.scrollNext());
const toggleButtons = () => {
  prev.disabled = !api.canScrollPrev();
  next.disabled = !api.canScrollNext();
};

// A dot for each snap
const buildDots = () => {
  dots.replaceChildren(
    ...api.scrollSnapList().map((_, index) => {
      const dot = document.createElement("button");
      dot.type = "button";
      dot.setAttribute("aria-label", `Go to slide ${index + 1}`);
      dot.addEventListener("click", () => api.scrollTo(index));
      return dot;
    }),
  );
};
const selectDot = () => {
  [...dots.children].forEach((dot, index) => dot.setAttribute("aria-current", String(index === api.selectedScrollSnap())));
};

const refresh = () => {
  buildDots();
  selectDot();
  toggleButtons();
};
api.on("init", refresh).on("reInit", refresh).on("select", () => {
  selectDot();
  toggleButtons();
});

Align

Where a snap sits in the viewport — start, center or end — switched live with reInit, the selected snap kept.

import Spill from "@bozdev/spill";

const root = document.querySelector(".carousel--align");
const prev = root.querySelector(".carousel__button--prev");
const next = root.querySelector(".carousel__button--next");
const choices = [...root.querySelectorAll("[data-align]")];

// It starts on the second snap: the scroll can't go before its start, so at the first snap every align
// looks the same (unless containScroll: false) — the difference shows from the middle on
const api = Spill(root.querySelector(".carousel__viewport"), { align: "center", startIndex: 1 });

// The switch: the carousel rebuilt with the new align (reInit keeps the selected snap)
for (const choice of choices) {
  choice.addEventListener("click", () => {
    api.reInit({ align: choice.dataset.align });
    choices.forEach((each) => each.setAttribute("aria-pressed", String(each === choice)));
  });
}

// Previous and next: a snap each, off at the edges
prev.addEventListener("click", () => api.scrollPrev());
next.addEventListener("click", () => api.scrollNext());
const toggleButtons = () => {
  prev.disabled = !api.canScrollPrev();
  next.disabled = !api.canScrollNext();
};
api.on("init", toggleButtons).on("reInit", toggleButtons).on("select", toggleButtons);

Slides to scroll

Several slides in view and a snap a page: slidesToScroll "auto" groups as many as fit — two in a narrow carousel, three in a wide one.

import Spill from "@bozdev/spill";

const root = document.querySelector(".carousel--slides-to-scroll");
const prev = root.querySelector(".carousel__button--prev");
const next = root.querySelector(".carousel__button--next");
const dots = root.querySelector(".carousel__dots");

// A snap a page: as many slides as fit in view ("auto"). A number groups that many: slidesToScroll: 2
const api = Spill(root.querySelector(".carousel__viewport"), { align: "start", slidesToScroll: "auto" });

// Previous and next: a page each, off at the edges
prev.addEventListener("click", () => api.scrollPrev());
next.addEventListener("click", () => api.scrollNext());
const toggleButtons = () => {
  prev.disabled = !api.canScrollPrev();
  next.disabled = !api.canScrollNext();
};

// A dot for each snap — fewer than the slides. When the carousel changes width and a page holds two slides
// instead of three, Spill measures again and fires reInit: the dots are built again there.
const buildDots = () => {
  dots.replaceChildren(
    ...api.scrollSnapList().map((_, index) => {
      const dot = document.createElement("button");
      dot.type = "button";
      dot.setAttribute("aria-label", `Go to page ${index + 1}`);
      dot.addEventListener("click", () => api.scrollTo(index));
      return dot;
    }),
  );
};
const selectDot = () => {
  [...dots.children].forEach((dot, index) => dot.setAttribute("aria-current", String(index === api.selectedScrollSnap())));
};

const refresh = () => {
  buildDots();
  selectDot();
  toggleButtons();
};
api.on("init", refresh).on("reInit", refresh).on("select", () => {
  selectDot();
  toggleButtons();
});

Drag free

No snapping: thrown with a mouse or a finger, the row glides and stops where it stops.

import Spill from "@bozdev/spill";

const root = document.querySelector(".carousel--drag-free");
const prev = root.querySelector(".carousel__button--prev");
const next = root.querySelector(".carousel__button--next");

// dragFree: no snap points — a throw glides on and the row rests wherever it ends
const api = Spill(root.querySelector(".carousel__viewport"), { align: "start", dragFree: true });

// The snaps are still there for the API: the buttons step one at a time, and the selected snap follows the
// nearest one as the row moves
prev.addEventListener("click", () => api.scrollPrev());
next.addEventListener("click", () => api.scrollNext());
const toggleButtons = () => {
  prev.disabled = !api.canScrollPrev();
  next.disabled = !api.canScrollNext();
};
api.on("init", toggleButtons).on("reInit", toggleButtons).on("select", toggleButtons);

Skip snaps

A strong throw passes several snaps and stops where its momentum runs out; a gentle one moves a single snap.

import Spill from "@bozdev/spill";

const root = document.querySelector(".carousel--skip-snaps");
const prev = root.querySelector(".carousel__button--prev");
const next = root.querySelector(".carousel__button--next");
const dots = root.querySelector(".carousel__dots");

// skipSnaps: a strong throw may pass several snaps. Without it, a throw moves one snap at most
// (under a finger, the browser's scroll-snap-stop: always)
const api = Spill(root.querySelector(".carousel__viewport"), { align: "start", skipSnaps: true });

// Previous and next: a snap each, off at the edges
prev.addEventListener("click", () => api.scrollPrev());
next.addEventListener("click", () => api.scrollNext());
const toggleButtons = () => {
  prev.disabled = !api.canScrollPrev();
  next.disabled = !api.canScrollNext();
};

// A dot for each snap: it shows how far a throw went
const buildDots = () => {
  dots.replaceChildren(
    ...api.scrollSnapList().map((_, index) => {
      const dot = document.createElement("button");
      dot.type = "button";
      dot.setAttribute("aria-label", `Go to slide ${index + 1}`);
      dot.addEventListener("click", () => api.scrollTo(index));
      return dot;
    }),
  );
};
const selectDot = () => {
  [...dots.children].forEach((dot, index) => dot.setAttribute("aria-current", String(index === api.selectedScrollSnap())));
};

const refresh = () => {
  buildDots();
  selectDot();
  toggleButtons();
};
api.on("init", refresh).on("reInit", refresh).on("select", () => {
  selectDot();
  toggleButtons();
});

Variable widths

Slides of different widths, each its own snap and centered when it's selected (the first and the last held to the edges) — Spill reads the sizes from your CSS.

import Spill from "@bozdev/spill";

const root = document.querySelector(".carousel--variable-widths");
const prev = root.querySelector(".carousel__button--prev");
const next = root.querySelector(".carousel__button--next");
const dots = root.querySelector(".carousel__dots");

// Each slide is a snap, centered whatever its width. At the ends, the first and the last
// slide hold to the edges instead (containScroll: "trimSnaps", the default).
const api = Spill(root.querySelector(".carousel__viewport"), { align: "center" });

// Previous and next: a snap each, off at the edges
prev.addEventListener("click", () => api.scrollPrev());
next.addEventListener("click", () => api.scrollNext());
const toggleButtons = () => {
  prev.disabled = !api.canScrollPrev();
  next.disabled = !api.canScrollNext();
};

// A dot for each snap
const buildDots = () => {
  dots.replaceChildren(
    ...api.scrollSnapList().map((_, index) => {
      const dot = document.createElement("button");
      dot.type = "button";
      dot.setAttribute("aria-label", `Go to slide ${index + 1}`);
      dot.addEventListener("click", () => api.scrollTo(index));
      return dot;
    }),
  );
};
const selectDot = () => {
  [...dots.children].forEach((dot, index) => dot.setAttribute("aria-current", String(index === api.selectedScrollSnap())));
};

const refresh = () => {
  buildDots();
  selectDot();
  toggleButtons();
};
api.on("init", refresh).on("reInit", refresh).on("select", () => {
  selectDot();
  toggleButtons();
});

Vertical

A column: axis "y", a viewport with a fixed height, and the container's flex-direction set to column.

import Spill from "@bozdev/spill";

const root = document.querySelector(".carousel--vertical");
const prev = root.querySelector(".carousel__button--prev");
const next = root.querySelector(".carousel__button--next");
const dots = root.querySelector(".carousel__dots");

// axis "y": the viewport scrolls up and down. The column itself is the CSS's (style.css).
const api = Spill(root.querySelector(".carousel__viewport"), { axis: "y", align: "start" });

// Previous and next: a snap each, off at the edges
prev.addEventListener("click", () => api.scrollPrev());
next.addEventListener("click", () => api.scrollNext());
const toggleButtons = () => {
  prev.disabled = !api.canScrollPrev();
  next.disabled = !api.canScrollNext();
};

// A dot for each snap
const buildDots = () => {
  dots.replaceChildren(
    ...api.scrollSnapList().map((_, index) => {
      const dot = document.createElement("button");
      dot.type = "button";
      dot.setAttribute("aria-label", `Go to slide ${index + 1}`);
      dot.addEventListener("click", () => api.scrollTo(index));
      return dot;
    }),
  );
};
const selectDot = () => {
  [...dots.children].forEach((dot, index) => dot.setAttribute("aria-current", String(index === api.selectedScrollSnap())));
};

const refresh = () => {
  buildDots();
  selectDot();
  toggleButtons();
};
api.on("init", refresh).on("reInit", refresh).on("select", () => {
  selectDot();
  toggleButtons();
});

Right to left

direction "rtl" with dir="rtl" on the carousel: the first slide on the right, the next one to its left, and the buttons mirrored.

import Spill from "@bozdev/spill";

const root = document.querySelector(".carousel--right-to-left");
const prev = root.querySelector(".carousel__button--prev");
const next = root.querySelector(".carousel__button--next");
const dots = root.querySelector(".carousel__dots");

// direction "rtl" goes with dir="rtl" in the markup (on the viewport or, as here, an ancestor):
// the one lays the row out from the right, the other tells Spill to read the scroll that way.
const api = Spill(root.querySelector(".carousel__viewport"), { align: "start", direction: "rtl" });

// Previous and next: a snap each, off at the edges (the row mirrors them: previous on the right)
prev.addEventListener("click", () => api.scrollPrev());
next.addEventListener("click", () => api.scrollNext());
const toggleButtons = () => {
  prev.disabled = !api.canScrollPrev();
  next.disabled = !api.canScrollNext();
};

// A dot for each snap, the first one on the right
const buildDots = () => {
  dots.replaceChildren(
    ...api.scrollSnapList().map((_, index) => {
      const dot = document.createElement("button");
      dot.type = "button";
      dot.setAttribute("aria-label", `Go to slide ${index + 1}`);
      dot.addEventListener("click", () => api.scrollTo(index));
      return dot;
    }),
  );
};
const selectDot = () => {
  [...dots.children].forEach((dot, index) => dot.setAttribute("aria-current", String(index === api.selectedScrollSnap())));
};

const refresh = () => {
  buildDots();
  selectDot();
  toggleButtons();
};
api.on("init", refresh).on("reInit", refresh).on("select", () => {
  selectDot();
  toggleButtons();
});

Breakpoints

One slide per view on a phone, two on a tablet, three on a desktop, and from 1440px Spill steps aside for a grid. Resize the window to try it: the dots follow each change.

import Spill from "@bozdev/spill";

const root = document.querySelector(".carousel--breakpoints");
const prev = root.querySelector(".carousel__button--prev");
const next = root.querySelector(".carousel__button--next");
const dots = root.querySelector(".carousel__dots");

const api = Spill(root.querySelector(".carousel__viewport"), {
  align: "start",
  // Each query's options are merged in, in this order, while it matches. When one starts or stops
  // matching, the carousel is rebuilt (reInit), its selected snap kept.
  breakpoints: {
    "(min-width: 640px)": { slidesToScroll: 2 }, // two slides in view: two a move
    "(min-width: 1024px)": { slidesToScroll: 3 }, // three in view: three a move
    "(min-width: 1440px)": { active: false }, // off: all it wrote taken back, only the queries watched
  },
});

// Previous and next: a snap each, off at the edges
prev.addEventListener("click", () => api.scrollPrev());
next.addEventListener("click", () => api.scrollNext());
const toggleButtons = () => {
  prev.disabled = !api.canScrollPrev();
  next.disabled = !api.canScrollNext();
};

// A dot for each snap: six, three or two of them — none while the carousel is off
const buildDots = () => {
  dots.replaceChildren(
    ...api.scrollSnapList().map((_, index) => {
      const dot = document.createElement("button");
      dot.type = "button";
      dot.setAttribute("aria-label", `Go to page ${index + 1}`);
      dot.addEventListener("click", () => api.scrollTo(index));
      return dot;
    }),
  );
};
const selectDot = () => {
  [...dots.children].forEach((dot, index) => dot.setAttribute("aria-current", String(index === api.selectedScrollSnap())));
};

// reInit follows every breakpoint change (and a new size): the dots are built again for the new snaps
const refresh = () => {
  buildDots();
  selectDot();
  toggleButtons();
};
api.on("init", refresh).on("reInit", refresh).on("select", () => {
  selectDot();
  toggleButtons();
});

Progress

A bar under the carousel that fills as it scrolls, from scrollProgress() on every scroll event: by hand, with a mouse or with the buttons.

import Spill from "@bozdev/spill";

const root = document.querySelector(".carousel--progress");
const prev = root.querySelector(".carousel__button--prev");
const next = root.querySelector(".carousel__button--next");
const bar = root.querySelector(".carousel__progress-bar");

const api = Spill(root.querySelector(".carousel__viewport"), { align: "start" });

// The bar: where the scroll is, 0 (the start) to 1 (the end), as a CSS variable the CSS scales the bar by.
// "scroll" comes once a frame, whatever moves it: a finger, a trackpad, a mouse's drag or the buttons.
const showProgress = () => bar.style.setProperty("--progress", String(api.scrollProgress()));

// Previous and next: a snap each, off at the edges
prev.addEventListener("click", () => api.scrollPrev());
next.addEventListener("click", () => api.scrollNext());
const toggleButtons = () => {
  prev.disabled = !api.canScrollPrev();
  next.disabled = !api.canScrollNext();
};

// On init and reInit (a new size changes the scroll's length) both are brought up to date
const refresh = () => {
  showProgress();
  toggleButtons();
};
api.on("init", refresh).on("reInit", refresh).on("scroll", showProgress).on("select", toggleButtons);

Events

Every event the carousel fires, written down as it happens — newest first, the same event again counted on its line.

import Spill from "@bozdev/spill";

const root = document.querySelector(".carousel--events");
const prev = root.querySelector(".carousel__button--prev");
const next = root.querySelector(".carousel__button--next");
const log = root.querySelector(".carousel__log");

const api = Spill(root.querySelector(".carousel__viewport"), { align: "start" });

prev.addEventListener("click", () => api.scrollPrev());
next.addEventListener("click", () => api.scrollNext());

// What a line says about an event, read from the API as it fires
const details = {
  select: (carousel) => `snap ${carousel.selectedScrollSnap()}`,
  settle: (carousel) => `snap ${carousel.selectedScrollSnap()}`,
  scroll: (carousel) => `${Math.round(carousel.scrollProgress() * 100)}%`,
  slidesInView: (carousel) => `[${carousel.slidesInView().join(", ")}]`,
};

const span = (part, text = "") => {
  const element = document.createElement("span");
  element.className = `carousel__log-${part}`;
  element.textContent = text;
  return element;
};

// The log: newest first, six lines at most. The same event again (a scroll, frame after frame) counts up on its line.
let newest = null;
const write = (carousel, name) => {
  const detail = details[name]?.(carousel) ?? "";
  if (newest?.name === name) {
    newest.count += 1;
    newest.detail.textContent = detail;
    newest.counter.textContent = `×${newest.count}`;
    return;
  }
  const line = document.createElement("li");
  newest = { name, count: 1, detail: span("detail", detail), counter: span("count") };
  line.append(span("name", name), newest.detail, newest.counter);
  log.prepend(line);
  log.children[6]?.remove();
};

// Every event of the core. Each callback gets the API and the event's name.
const events = ["init", "reInit", "destroy", "select", "scroll", "settle", "resize", "slidesInView", "slidesChanged", "slideFocusStart", "slideFocus", "pointerDown", "pointerUp"];
for (const name of events) api.on(name, write);

By attributes

No script of its own: numbered dots, a progress bar, Autoplay with a play button, and a next button outside the carousel.

// That's all: every [data-spill-carousel] on the page builds itself and wires its controls — the one outside it too
import "@bozdev/spill/auto";

Autoplay

The next slide every four seconds, a play / pause button that shows what autoplay is doing, and a line that fills until the next move.

import Spill from "@bozdev/spill";
import Autoplay from "@bozdev/spill/plugins/autoplay";

const root = document.querySelector(".carousel--autoplay");
const play = root.querySelector(".carousel__button--play");
const fill = root.querySelector(".carousel__timer-fill");
const dots = root.querySelector(".carousel__dots");

const api = Spill(root.querySelector(".carousel__viewport"), { align: "start" }, [Autoplay({ delay: 4000 })]);
const autoplay = api.plugins().autoplay;

// Play / pause. The button shows the state, whatever changed it: a click here, a drag or a focus in the carousel
play.addEventListener("click", () => (autoplay.isPlaying() ? autoplay.stop() : autoplay.play()));
const showState = () => play.setAttribute("aria-pressed", String(autoplay.isPlaying()));

// The line fills while the timer counts down to the next slide, and empties when the timer stops
let filling = null;
const startFill = () => {
  filling?.cancel();
  const ms = autoplay.timeUntilNext();
  if (ms !== null) filling = fill.animate({ transform: ["scaleX(0)", "scaleX(1)"] }, { duration: ms, easing: "linear" });
};
const stopFill = () => filling?.cancel();

// A dot for each snap
const buildDots = () => {
  dots.replaceChildren(
    ...api.scrollSnapList().map((_, index) => {
      const dot = document.createElement("button");
      dot.type = "button";
      dot.setAttribute("aria-label", `Go to slide ${index + 1}`);
      dot.addEventListener("click", () => api.scrollTo(index));
      return dot;
    }),
  );
};
const selectDot = () => {
  [...dots.children].forEach((dot, index) => dot.setAttribute("aria-current", String(index === api.selectedScrollSnap())));
};

const refresh = () => {
  buildDots();
  selectDot();
  showState();
};
api
  .on("init", () => {
    refresh();
    startFill(); // autoplay started while the carousel was built, before these listeners
  })
  .on("reInit", refresh)
  .on("select", selectDot)
  .on("autoplay:play", showState)
  .on("autoplay:stop", showState)
  .on("autoplay:timerset", startFill)
  .on("autoplay:timerstopped", stopFill);

Class names

ClassNames marks the selected snap's slide, and the CSS lets the others step back — only once the class is there, so nothing is dimmed without JavaScript.

import Spill from "@bozdev/spill";
import ClassNames from "@bozdev/spill/plugins/class-names";

const root = document.querySelector(".carousel--class-names");
const prev = root.querySelector(".carousel__button--prev");
const next = root.querySelector(".carousel__button--next");
const dots = root.querySelector(".carousel__dots");

// Every snap centered (containScroll: false leaves room at the ends); ClassNames marks the selected one's slide
const api = Spill(root.querySelector(".carousel__viewport"), { align: "center", containScroll: false }, [ClassNames()]);

// Previous and next: a snap each, off at the edges
prev.addEventListener("click", () => api.scrollPrev());
next.addEventListener("click", () => api.scrollNext());
const toggleButtons = () => {
  prev.disabled = !api.canScrollPrev();
  next.disabled = !api.canScrollNext();
};

// A dot for each snap
const buildDots = () => {
  dots.replaceChildren(
    ...api.scrollSnapList().map((_, index) => {
      const dot = document.createElement("button");
      dot.type = "button";
      dot.setAttribute("aria-label", `Go to slide ${index + 1}`);
      dot.addEventListener("click", () => api.scrollTo(index));
      return dot;
    }),
  );
};
const selectDot = () => {
  [...dots.children].forEach((dot, index) => dot.setAttribute("aria-current", String(index === api.selectedScrollSnap())));
};

const refresh = () => {
  buildDots();
  selectDot();
  toggleButtons();
};
api.on("init", refresh).on("reInit", refresh).on("select", () => {
  selectDot();
  toggleButtons();
});

Auto height

Slides of different heights: the carousel is as tall as the selected one and follows it, smoothly unless you ask for reduced motion.

import Spill from "@bozdev/spill";
import AutoHeight from "@bozdev/spill/plugins/auto-height";

const root = document.querySelector(".carousel--auto-height");
const prev = root.querySelector(".carousel__button--prev");
const next = root.querySelector(".carousel__button--next");
const dots = root.querySelector(".carousel__dots");

// AutoHeight sets the container's height to the selected slide's; the CSS makes the change smooth
const api = Spill(root.querySelector(".carousel__viewport"), { align: "start" }, [AutoHeight()]);

// Previous and next: a snap each, off at the edges
prev.addEventListener("click", () => api.scrollPrev());
next.addEventListener("click", () => api.scrollNext());
const toggleButtons = () => {
  prev.disabled = !api.canScrollPrev();
  next.disabled = !api.canScrollNext();
};

// A dot for each snap
const buildDots = () => {
  dots.replaceChildren(
    ...api.scrollSnapList().map((_, index) => {
      const dot = document.createElement("button");
      dot.type = "button";
      dot.setAttribute("aria-label", `Go to slide ${index + 1}`);
      dot.addEventListener("click", () => api.scrollTo(index));
      return dot;
    }),
  );
};
const selectDot = () => {
  [...dots.children].forEach((dot, index) => dot.setAttribute("aria-current", String(index === api.selectedScrollSnap())));
};

const refresh = () => {
  buildDots();
  selectDot();
  toggleButtons();
};
api.on("init", refresh).on("reInit", refresh).on("select", () => {
  selectDot();
  toggleButtons();
});

Thumbnails

Two carousels: a thumbnail's click scrolls the main one there, and the main one's select marks its thumbnail and brings it into view.

import Spill from "@bozdev/spill";

const root = document.querySelector(".carousel--thumbnails");

// Two carousels: the main one (the first viewport), and the thumbnails, gliding freely with a snap for each
const main = Spill(root.querySelector(".carousel__viewport"));
const thumbs = Spill(root.querySelector(".carousel__thumbs"), { containScroll: "keepSnaps", dragFree: true });

// A thumbnail's click: the main carousel goes to its slide (a snap a slide here, so the indexes match)
thumbs.slideNodes().forEach((thumb, index) => thumb.addEventListener("click", () => main.scrollTo(index)));

// The main carousel's select: its thumbnail marked, and the row scrolled to bring it into view
const sync = () => {
  const selected = main.selectedScrollSnap();
  thumbs.slideNodes().forEach((thumb, index) => thumb.setAttribute("aria-current", String(index === selected)));
  thumbs.scrollTo(selected);
};
main.on("init", sync).on("select", sync);

Scale

The slide in the middle at full size, the others smaller as they move away — a CSS scroll-driven animation, with no script for it.

import Spill from "@bozdev/spill";

const root = document.querySelector(".carousel--scale");

// The scale is all CSS (style.css): the script only builds the carousel. Every slide can sit in the middle (the
// default align, "center"), the first and the last too: containScroll false leaves room before and after them.
// It starts on the second slide, with a smaller one on each side.
Spill(root.querySelector(".carousel__viewport"), { containScroll: false, startIndex: 1 });

Opacity

The slides fade as they leave the middle, worked out on every scroll event from scrollSnapList() and scrollProgress() — Embla's tween way.

import Spill from "@bozdev/spill";

const root = document.querySelector(".carousel--opacity");

// Every slide can sit in the middle (the default align, "center"), the first and the last too: containScroll false
// leaves room before and after them. It starts on the second slide, with a faded one on each side.
const api = Spill(root.querySelector(".carousel__viewport"), { containScroll: false, startIndex: 1 });

// The faintest a slide gets: never invisible, so the next ones still show they're there. Opacity moves nothing,
// so it stays with prefers-reduced-motion: reduce (Spill's own moves jump there by themselves).
const MIN_OPACITY = 0.35;

// Embla's tween: each slide's opacity from how far its snap is from the scroll. scrollSnapList() and
// scrollProgress() both run 0 to 1 across the carousel; the distance is counted in the way to the next snap on
// the scroll's side: 0 with the slide in place, 1 once the scroll reaches that next snap.
const tween = () => {
  const snaps = api.scrollSnapList();
  const progress = api.scrollProgress();
  const slides = api.slideNodes(); // one slide a snap here (several a snap: api.internalEngine().slideRegistry)
  snaps.forEach((snap, index) => {
    const next = snaps[progress < snap ? index - 1 : index + 1] ?? snap;
    const distance = Math.min(1, Math.abs(snap - progress) / (Math.abs(next - snap) || 1));
    slides[index].style.opacity = (1 - distance * (1 - MIN_OPACITY)).toFixed(3);
  });
};

// Every frame it scrolls, and when it's built or measured again
api.on("init", tween).on("reInit", tween).on("scroll", tween);