09Multi-pair comparison carousel
Three before/after sliders sit side by side in a scroll-snap carousel, and IntersectionObserver enables dragging only on whichever slide is fully in view.
Best forA portfolio gathering several construction cases on one page, a page bundling before/after reviews by treatment area
scroll-snap-type:xIntersectionObserver슬라이더 다중 관리

<main class="stage">
<div class="track" id="track" aria-label="Before/after comparison carousel">
<div class="ba is-demo is-active" data-i="1">
<div class="ba__after"></div>
<div class="ba__before"></div>
</div>
<div class="ba is-demo" data-i="2">
<div class="ba__after"></div>
<div class="ba__before"></div>
</div>
<div class="ba is-demo" data-i="3">
<div class="ba__after"></div>
<div class="ba__before"></div>
</div>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
$duration: $dur-loop;
$easing: $ease-spring;
$color: $subject-blue;
@property --reveal {
syntax: "<percentage>";
inherits: true;
initial-value: 50%;
}
@include stage($stage-ink);
.track {
display: flex;
gap: $sp-3;
width: 260px;
max-width: 100%;
overflow-x: auto;
scroll-snap-type: x mandatory;
border-radius: $r-card;
}
.ba {
position: relative;
flex: 0 0 200px;
aspect-ratio: 4 / 3;
scroll-snap-align: center;
border-radius: $r-card;
overflow: hidden;
box-shadow: $shadow-press;
touch-action: pan-x;
--reveal: 50%;
}
.ba.is-active { box-shadow: $shadow-raised, 0 0 0 $sp-1 $subject-cream; cursor: ew-resize; }
.ba__after,
.ba__before {
position: absolute;
inset: 0;
}
.ba__after { background: linear-gradient(155deg, $color 0%, $subject-cream 100%); }
.ba__before {
background: linear-gradient(155deg, $stage-ink 0%, $color 70%);
clip-path: inset(0 calc(100% - var(--reveal)) 0 0);
}
.ba.is-demo { animation: reveal-loop $dur-loop $easing infinite; }
.ba.is-demo[data-i="2"] { animation-delay: -0.6s; }
.ba.is-demo[data-i="3"] { animation-delay: -1.2s; }
@keyframes reveal-loop {
0%, 8% { --reveal: 20%; }
50% { --reveal: 80%; }
92%, 100% { --reveal: 20%; }
}
@media (prefers-reduced-motion: reduce) {
.ba.is-demo { animation: none; --reveal: 50%; }
}@property --reveal {
syntax: "<percentage>";
inherits: true;
initial-value: 50%;
}
html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #17141a;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.track {
display: flex;
gap: 12px;
width: 260px;
max-width: 100%;
overflow-x: auto;
scroll-snap-type: x mandatory;
border-radius: 16px;
}
.ba {
position: relative;
flex: 0 0 200px;
aspect-ratio: 4/3;
scroll-snap-align: center;
border-radius: 16px;
overflow: hidden;
box-shadow: 0 6px 12px rgba(23, 20, 26, 0.32);
touch-action: pan-x;
--reveal: 50%;
}
.ba.is-active {
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32), 0 0 0 4px #fff7e6;
cursor: ew-resize;
}
.ba__after,
.ba__before {
position: absolute;
inset: 0;
}
.ba__after {
background: linear-gradient(155deg, #2f6df6 0%, #fff7e6 100%);
}
.ba__before {
background: linear-gradient(155deg, #17141a 0%, #2f6df6 70%);
clip-path: inset(0 calc(100% - var(--reveal)) 0 0);
}
.ba.is-demo {
animation: reveal-loop 2s cubic-bezier(0.2, 0.8, 0.2, 1) infinite;
}
.ba.is-demo[data-i="2"] {
animation-delay: -0.6s;
}
.ba.is-demo[data-i="3"] {
animation-delay: -1.2s;
}
@keyframes reveal-loop {
0%, 8% {
--reveal: 20%;
}
50% {
--reveal: 80%;
}
92%, 100% {
--reveal: 20%;
}
}
@media (prefers-reduced-motion: reduce) {
.ba.is-demo {
animation: none;
--reveal: 50%;
}
}(function () {
var track = document.getElementById("track");
var panes = Array.prototype.slice.call(track.querySelectorAll(".ba"));
panes.forEach(function (pane) {
var dragging = false;
function set(pct) {
pct = Math.max(0, Math.min(100, pct));
pane.style.setProperty("--reveal", pct + "%");
}
function fromX(x) {
var r = pane.getBoundingClientRect();
return ((x - r.left) / r.width) * 100;
}
pane.addEventListener("pointerdown", function (e) {
if (!pane.classList.contains("is-active")) return;
pane.classList.remove("is-demo");
dragging = true;
pane.setPointerCapture(e.pointerId);
set(fromX(e.clientX));
});
pane.addEventListener("pointermove", function (e) { if (dragging) set(fromX(e.clientX)); });
pane.addEventListener("pointerup", function () { dragging = false; });
});
if ("IntersectionObserver" in window) {
var io = new IntersectionObserver(function (entries) {
entries.forEach(function (en) {
en.target.classList.toggle("is-active", en.intersectionRatio > 0.6);
});
}, { root: track, threshold: [0.6] });
panes.forEach(function (p) { io.observe(p); });
}
})();import { useRef, useState, useEffect, useCallback } from "react";
import type { CSSProperties, PointerEvent } from "react";
import styles from "./BeforeAfterMultiCarousel.module.scss";
export type BeforeAfterMultiCarouselProps = {
duration?: string;
easing?: string;
color?: string;
pairs?: number;
};
type Vars = CSSProperties & { "--reveal"?: string };
/** 전후 슬라이더 여러 쌍을 scroll-snap 캐러셀에 나란히 두고, IntersectionObserver 로 화면 중앙에 온 것만 드래그를 켠다. */
export default function BeforeAfterMultiCarousel({
duration = "2s",
easing = "cubic-bezier(.2,.8,.2,1)",
color = "#2f6df6",
pairs = 3,
}: BeforeAfterMultiCarouselProps) {
const trackRef = useRef<HTMLDivElement | null>(null);
const paneRefs = useRef<(HTMLDivElement | null)[]>([]);
const [reveals, setReveals] = useState<number[]>(() => Array(pairs).fill(50));
const [demoIdx, setDemoIdx] = useState<Set<number>>(() => new Set(Array.from({ length: pairs }, (_, i) => i)));
const [activeIdx, setActiveIdx] = useState(0);
const dragging = useRef<number | null>(null);
useEffect(() => {
const track = trackRef.current;
if (!track || !("IntersectionObserver" in window)) return;
const io = new IntersectionObserver(
(entries) => {
entries.forEach((en) => {
if (en.intersectionRatio > 0.6) {
const i = (paneRefs.current || []).indexOf(en.target as HTMLDivElement);
if (i >= 0) setActiveIdx(i);
}
});
},
{ root: track, threshold: [0.6] }
);
(paneRefs.current || []).forEach((p) => p && io.observe(p));
return () => io.disconnect();
}, [pairs]);
const setFromX = useCallback((i: number, x: number) => {
const el = (paneRefs.current || [])[i];
if (!el) return;
const r = el.getBoundingClientRect();
const pct = Math.max(0, Math.min(100, ((x - r.left) / r.width) * 100));
setReveals((arr) => arr.map((v, idx) => (idx === i ? pct : v)));
}, []);
const onDown = useCallback((i: number) => (e: PointerEvent<HTMLDivElement>) => {
if (activeIdx !== i) return;
setDemoIdx((s) => { const n = new Set(s); n.delete(i); return n; });
dragging.current = i;
(e.currentTarget as HTMLDivElement).setPointerCapture(e.pointerId);
setFromX(i, e.clientX);
}, [activeIdx, setFromX]);
const onMove = useCallback((i: number) => (e: PointerEvent<HTMLDivElement>) => {
if (dragging.current === i) setFromX(i, e.clientX);
}, [setFromX]);
const onUp = useCallback(() => { dragging.current = null; }, []);
return (
<div className={styles.track} ref={trackRef}>
{reveals.map((reveal, i) => {
const vars: Vars = { "--reveal": `${reveal}%`, ["--duration" as string]: duration, ["--easing" as string]: easing, ["--color" as string]: color };
const isDemo = demoIdx.has(i);
const isActive = activeIdx === i;
const cls = [styles.ba, isDemo ? styles.isDemo : "", isActive ? styles.isActive : ""].filter(Boolean).join(" ");
return (
<div
key={i}
ref={(el: HTMLDivElement | null) => { if (paneRefs.current) paneRefs.current[i] = el; }}
className={cls}
style={vars}
onPointerDown={onDown(i)}
onPointerMove={onMove(i)}
onPointerUp={onUp}
>
<div className={styles.after} />
<div className={styles.before} />
</div>
);
})}
</div>
);
}09 Multi-pair comparison carousel — Measured area 13.271% · moving frames 21/23Original by GODRICH · SCSS and React ship in the zip · 9 Before After Slider UIs — Drag to Compare
The post this demo lives in
9 Before After Slider UIs — Drag to CompareA before after slider stacks two photos and splits them at a line you can move, so before and after show side by side. T See all demos in this post


