04인덱스 거리 값이 눕히는 스택 패널
활성 탭과의 인덱스 거리를 정수로 구해 그 값이 클수록 패널의 scale 과 밝기를 낮추고 z축 뒤로 밀어, 나가는 패널이 쌓인 카드처럼 한 겹씩 뒤로 눕는다.
쓰임이미지 갤러리 캡션 탭·포트폴리오 카테고리
--dSCALE_STEPtranslateZ()

<main class="stage">
<div class="sd is-demo" id="sd">
<div class="sd__tabs" role="tablist" aria-label="카테고리 탭">
<button type="button" class="sd__tab is-active" role="tab" id="sd-tab-0" aria-selected="true" aria-controls="sd-panel-0" data-idx="0">풍경</button>
<button type="button" class="sd__tab" role="tab" id="sd-tab-1" aria-selected="false" aria-controls="sd-panel-1" data-idx="1" tabindex="-1">인물</button>
<button type="button" class="sd__tab" role="tab" id="sd-tab-2" aria-selected="false" aria-controls="sd-panel-2" data-idx="2" tabindex="-1">정물</button>
</div>
<div class="sd__wrap" id="sdWrap">
<div class="sd__panel" id="sd-panel-0" role="tabpanel" aria-labelledby="sd-tab-0" tabindex="0" style="--d:0"><span class="sd__num">01</span><p class="sd__line">풍경 12장</p></div>
<div class="sd__panel" id="sd-panel-1" role="tabpanel" aria-labelledby="sd-tab-1" tabindex="0" aria-hidden="true" style="--d:1"><span class="sd__num">02</span><p class="sd__line">인물 8장</p></div>
<div class="sd__panel" id="sd-panel-2" role="tabpanel" aria-labelledby="sd-tab-2" tabindex="0" aria-hidden="true" style="--d:2"><span class="sd__num">03</span><p class="sd__line">정물 5장</p></div>
</div>
<p class="sd__meta">거리 <b id="sdDist">0</b>칸 · scale <b id="sdScale">1.00</b></p>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 계산 겹: index.html apply() 가 |탭 인덱스 - 활성 인덱스| 로 정수 거리(--d)를 구해 각 패널에 심는다.
// 이 scss 는 그 --d 하나로 scale·translateY·translateZ·opacity·filter 를 calc() 로 유도한다 —
// 값이 바뀔 때마다 transform·opacity·filter 가 transition 대상이라 자동으로 부드럽게 물러난다.
@include stage($stage-paper);
.sd { width: min(100%, 300px); min-width: 0; display: flex; flex-direction: column; gap: $sp-3; }
.sd__tabs { display: flex; gap: $sp-1; background: rgba($subject-ink, .06); padding: $sp-1; border-radius: $r-pill; }
.sd__tab {
flex: 1 1 0; border: none; background: transparent; border-radius: $r-pill; cursor: pointer;
padding: $sp-2 $sp-2; font-size: 11px; font-weight: 700; color: rgba($subject-ink, .68);
}
.sd__tab.is-active { background: $subject-blue-deep; color: $subject-cream; }
.sd__tab:focus-visible { outline: 2px solid $subject-blue-deep; outline-offset: 2px; }
.sd__wrap { position: relative; min-width: 0; height: 150px; perspective: 900px; }
.sd__panel {
position: absolute; inset: 0; box-sizing: border-box; padding: $sp-4;
display: flex; flex-direction: column; align-items: center; justify-content: center; gap: $sp-2;
border-radius: $r-card; background: $subject-cream; box-shadow: 0 6px 18px rgba($subject-ink, .12);
transform-origin: center 70%;
transform:
translateY(calc(var(--d) * 10px))
translateZ(calc(var(--d) * -40px))
scale(calc(1 - var(--d) * 0.08));
opacity: calc(1 - var(--d) * 0.22);
filter: brightness(calc(1 - var(--d) * 0.12));
z-index: calc(3 - var(--d));
transition: transform $dur-base $ease-out, opacity $dur-base $ease-out, filter $dur-base $ease-out;
}
.sd__panel[aria-hidden="true"] { pointer-events: none; }
.sd__num { font-size: 30px; font-weight: 800; color: $subject-blue-deep; }
.sd__line { margin: 0; font-size: 12px; color: rgba($subject-ink, .72); }
.sd__meta { margin: 0; font-size: 11px; color: rgba($subject-ink, .68); text-align: center; }
.sd__meta b { color: $subject-ink; font-weight: 700; }
@media (max-width: 340px) {
.sd { width: 100%; gap: $sp-2; }
.sd__tab { padding: $sp-1 $sp-2; }
.sd__wrap { height: 96px; }
.sd__num { font-size: 24px; }
}
@media (prefers-reduced-motion: reduce) {
.sd__panel { transition: none; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #ffffff;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.sd {
width: min(100%, 300px);
min-width: 0;
display: flex;
flex-direction: column;
gap: 12px;
}
.sd__tabs {
display: flex;
gap: 4px;
background: rgba(23, 20, 26, 0.06);
padding: 4px;
border-radius: 999px;
}
.sd__tab {
flex: 1 1 0;
border: none;
background: transparent;
border-radius: 999px;
cursor: pointer;
padding: 8px 8px;
font-size: 11px;
font-weight: 700;
color: rgba(23, 20, 26, 0.68);
}
.sd__tab.is-active {
background: #0a4fe8;
color: #fff7e6;
}
.sd__tab:focus-visible {
outline: 2px solid #0a4fe8;
outline-offset: 2px;
}
.sd__wrap {
position: relative;
min-width: 0;
height: 150px;
perspective: 900px;
}
.sd__panel {
position: absolute;
inset: 0;
box-sizing: border-box;
padding: 16px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 8px;
border-radius: 16px;
background: #fff7e6;
box-shadow: 0 6px 18px rgba(23, 20, 26, 0.12);
transform-origin: center 70%;
transform: translateY(calc(var(--d) * 10px)) translateZ(calc(var(--d) * -40px)) scale(calc(1 - var(--d) * 0.08));
opacity: calc(1 - var(--d) * 0.22);
filter: brightness(calc(1 - var(--d) * 0.12));
z-index: calc(3 - var(--d));
transition: transform 300ms ease-out, opacity 300ms ease-out, filter 300ms ease-out;
}
.sd__panel[aria-hidden=true] {
pointer-events: none;
}
.sd__num {
font-size: 30px;
font-weight: 800;
color: #0a4fe8;
}
.sd__line {
margin: 0;
font-size: 12px;
color: rgba(23, 20, 26, 0.72);
}
.sd__meta {
margin: 0;
font-size: 11px;
color: rgba(23, 20, 26, 0.68);
text-align: center;
}
.sd__meta b {
color: #17141a;
font-weight: 700;
}
@media (max-width: 340px) {
.sd {
width: 100%;
gap: 8px;
}
.sd__tab {
padding: 4px 8px;
}
.sd__wrap {
height: 96px;
}
.sd__num {
font-size: 24px;
}
}
@media (prefers-reduced-motion: reduce) {
.sd__panel {
transition: none;
}
}(function () {
var SCALE_STEP = 0.08; // 계산 겹 상수: 인덱스 거리 1칸마다 scale 이 이만큼 줄어든다
var HOLD_MS = 780;
var COUNT = 3;
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var root = document.getElementById('sd');
var distEl = document.getElementById('sdDist');
var scaleEl = document.getElementById('sdScale');
var tabs = [0, 1, 2].map(function (i) { return document.getElementById('sd-tab-' + i); });
var panels = [0, 1, 2].map(function (i) { return document.getElementById('sd-panel-' + i); });
var isDemo = true;
var active = 0;
function human() {
if (isDemo) { root.classList.remove('is-demo'); isDemo = false; }
}
function apply() {
panels.forEach(function (p, i) {
var d = Math.abs(i - active); // 계산 겹: 활성 탭과의 정수 인덱스 거리
p.style.setProperty('--d', String(d));
p.setAttribute('aria-hidden', d === 0 ? 'false' : 'true');
});
var scale = (1 - SCALE_STEP).toFixed(2); // 바로 다음 겹(거리1)의 scale 값을 대표로 표시
distEl.textContent = '1';
scaleEl.textContent = scale;
}
function select(idx) {
if (idx === active) return;
tabs[active].classList.remove('is-active');
tabs[active].setAttribute('aria-selected', 'false');
tabs[active].setAttribute('tabindex', '-1');
tabs[idx].classList.add('is-active');
tabs[idx].setAttribute('aria-selected', 'true');
tabs[idx].setAttribute('tabindex', '0');
active = idx;
apply();
}
tabs.forEach(function (t, i) {
t.addEventListener('click', function () { human(); select(i); });
});
root.addEventListener('keydown', function (e) {
var idx = tabs.indexOf(document.activeElement);
if (idx === -1) return;
var next = null;
if (e.key === 'ArrowRight') next = (idx + 1) % COUNT;
else if (e.key === 'ArrowLeft') next = (idx - 1 + COUNT) % COUNT;
else if (e.key === 'Home') next = 0;
else if (e.key === 'End') next = COUNT - 1;
if (next === null) return;
e.preventDefault();
human();
tabs[next].focus({ preventScroll: true });
select(next);
});
function loop() {
if (!isDemo) return;
select((active + 1) % COUNT);
window.setTimeout(loop, HOLD_MS);
}
if (!reduce) {
window.setTimeout(loop, HOLD_MS);
} else {
root.classList.remove('is-demo');
}
})();// TabStackDepthScale — vanilla 판(run/394/demo/04-tab-panel-stack-depth-scale/index.html) 의 React 포트.
// apply() 가 |탭 인덱스 - 활성 인덱스| 로 정수 거리(--d)를 구해 각 패널에 심으면, module.scss 의
// calc(var(--d) * ...) 가 scale·translateY·translateZ·opacity·filter 를 유도해 물러난다.
import { useEffect, useRef, CSSProperties } from "react";
import styles from "./TabStackDepthScale.module.scss";
type Box<T> = { current: T };
const SCALE_STEP = 0.08; // 계산 겹 상수: 인덱스 거리 1칸마다 scale 이 이만큼 줄어든다
const HOLD_MS = 780;
const COUNT = 3;
export default function TabStackDepthScale() {
const rootRef = useRef(null) as unknown as Box<HTMLDivElement>;
const distRef = useRef(null) as unknown as Box<HTMLElement>;
const scaleRef = useRef(null) as unknown as Box<HTMLElement>;
const tabRefs = useRef([null, null, null]) as unknown as Box<(HTMLButtonElement | null)[]>;
const panelRefs = useRef([null, null, null]) as unknown as Box<(HTMLDivElement | null)[]>;
const timerRef = useRef(0) as unknown as Box<number>;
useEffect(() => {
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let isDemo = true;
let active = 0;
function human() {
if (isDemo) { rootRef.current?.classList.remove(styles.isDemo); isDemo = false; }
}
function apply() {
panelRefs.current.forEach((p, i) => {
if (!p) return;
const d = Math.abs(i - active); // 계산 겹: 활성 탭과의 정수 인덱스 거리
p.style.setProperty("--d", String(d));
p.setAttribute("aria-hidden", d === 0 ? "false" : "true");
});
if (distRef.current) distRef.current.textContent = "1";
if (scaleRef.current) scaleRef.current.textContent = (1 - SCALE_STEP).toFixed(2);
}
function select(idx: number) {
if (idx === active) return;
const prevTab = tabRefs.current[active], nextTab = tabRefs.current[idx];
prevTab?.classList.remove(styles.isActive);
prevTab?.setAttribute("aria-selected", "false");
prevTab?.setAttribute("tabindex", "-1");
nextTab?.classList.add(styles.isActive);
nextTab?.setAttribute("aria-selected", "true");
nextTab?.setAttribute("tabindex", "0");
active = idx;
apply();
}
function onKeydown(e: KeyboardEvent) {
const idx = tabRefs.current.indexOf(document.activeElement as HTMLButtonElement);
if (idx === -1) return;
let next: number | null = null;
if (e.key === "ArrowRight") next = (idx + 1) % COUNT;
else if (e.key === "ArrowLeft") next = (idx - 1 + COUNT) % COUNT;
else if (e.key === "Home") next = 0;
else if (e.key === "End") next = COUNT - 1;
if (next === null) return;
e.preventDefault();
human();
tabRefs.current[next]?.focus({ preventScroll: true });
select(next);
}
function onTabClick(i: number) { return () => { human(); select(i); }; }
const clickHandlers = [0, 1, 2].map(onTabClick);
tabRefs.current.forEach((t, i) => t?.addEventListener("click", clickHandlers[i]));
rootRef.current?.addEventListener("keydown", onKeydown);
function loop() {
if (!isDemo) return;
select((active + 1) % COUNT);
timerRef.current = window.setTimeout(loop, HOLD_MS);
}
if (!reduce) {
timerRef.current = window.setTimeout(loop, HOLD_MS);
} else {
rootRef.current?.classList.remove(styles.isDemo);
}
return () => {
window.clearTimeout(timerRef.current);
tabRefs.current.forEach((t, i) => t?.removeEventListener("click", clickHandlers[i]));
rootRef.current?.removeEventListener("keydown", onKeydown);
};
}, []);
const cats = [
{ name: "풍경", num: "01", line: "풍경 12장" },
{ name: "인물", num: "02", line: "인물 8장" },
{ name: "정물", num: "03", line: "정물 5장" },
];
return (
<main className={styles.stage}>
<div className={styles.sd + " " + styles.isDemo} ref={rootRef}>
<div className={styles.tabs} role="tablist" aria-label="카테고리 탭">
{cats.map((c, i) => (
<button
key={c.name}
type="button"
className={styles.tab + (i === 0 ? " " + styles.isActive : "")}
role="tab"
aria-selected={i === 0 ? "true" : "false"}
tabIndex={i === 0 ? 0 : -1}
ref={(el: HTMLButtonElement | null) => { tabRefs.current[i] = el; }}
>{c.name}</button>
))}
</div>
<div className={styles.wrap}>
{cats.map((c, i) => (
<div
key={c.name}
className={styles.panel}
role="tabpanel"
tabIndex={0}
aria-hidden={i === 0 ? "false" : "true"}
style={{ ["--d" as string]: String(i) } as CSSProperties}
ref={(el: HTMLDivElement | null) => { panelRefs.current[i] = el; }}
>
<span className={styles.num}>{c.num}</span>
<p className={styles.line}>{c.line}</p>
</div>
))}
</div>
<p className={styles.meta}>
거리 <b ref={distRef}>0</b>칸 · scale <b ref={scaleRef}>1.00</b>
</p>
</div>
</main>
);
}04 인덱스 거리 값이 눕히는 스택 패널 — 측정 누적면적 37.975% · 움직인 프레임 11/23GODRICH 원작 · zip 에 SCSS·React 포함 · 탭 전환 애니메이션 9가지 패턴 — 코드 그대로
이 데모가 속한 글
탭 전환 애니메이션 9가지 패턴 — 코드 그대로탭 전환 애니메이션 아홉 개를 나란히 놓고 보면 높이 전환은 두 패널의 실측 높이 차이(px)로 늘어나는 시간을 정하는데, 스택 전환은 활성 탭과의 인덱스 거리로 축소 비율을 정하고, 탄성 슬라이드는 스프링 상수로 이 글의 데모 전부 보기


