03값이 오를 때마다 뒤 카드가 깊이 순서를 바꿔 앞으로 넘어오는 카드 스택 로더
카드마다 가진 깊이 값을 기준으로 앞뒤 순서를 정렬해 두고, 진행률 값이 한 단계 오를 때마다 가장 뒤에 있던 카드의 깊이 값을 가장 앞으로 바꿔 전체 순서를 다시 계산한다.
쓰임다단계 결제 진행, 온보딩 단계 표시
ZGAPSHRINKOPACITY_STEP

<main class="stage">
<div class="ld" id="ld">
<p class="ld__hint">클릭하거나 스페이스바를 누르면 멈추거나 다시 갑니다</p>
<div class="ld__field" id="field" role="application" aria-label="카드 스택 깊이 순환 진행 실험판" tabindex="0">
<div class="c3__deck" id="deck">
<div class="c3__card" id="c0">1</div>
<div class="c3__card" id="c1">2</div>
<div class="c3__card" id="c2">3</div>
<div class="c3__card" id="c3">4</div>
<div class="c3__card" id="c4">5</div>
</div>
<b class="ld__badge" aria-hidden="true"><span class="ld__badgeNum" id="pct">0%</span><span class="ld__badgeUnit">진행</span></b>
</div>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 카드 스택 깊이 순환 로더 — 카드마다 깊이 값(rankPos, 0=맨 앞)을 진행률의 연속 함수로 계산해
// translateZ·translateY·scale·opacity·z-index 를 매 프레임 다시 매긴다(화가 알고리즘 계열, 이산 스냅이 아니라 연속 순환).
// 395(캔버스 rAF 물리)·393(포인터 tilt) 과 다른 축 — 깊이 순위 자체를 진행률의 모듈러 함수로 계산한다.
$duration: $dur-loop;
$easing: $ease-out;
$color: $subject-cream;
@include stage($stage-orange);
.ld { display: flex; flex-direction: column; gap: $sp-2; width: 100%; max-width: 360px; }
.ld__hint {
margin: 0;
font-weight: 700; font-size: clamp(10px, 2.8vw, 12px); line-height: 1.35;
color: $subject-ink;
text-align: center;
}
.ld__field {
position: relative; height: 208px;
border-radius: $r-card; overflow: hidden;
background: darken($stage-orange, 6%);
box-shadow: inset 0 0 0 1px rgba($subject-ink, .16);
display: grid; place-items: center;
cursor: pointer;
perspective: clamp(320px, 70vw, 480px);
}
.ld__field:focus-visible { outline: 2px solid $subject-ink; outline-offset: 2px; }
.c3__deck { position: relative; width: 116px; height: 74px; transform-style: preserve-3d; }
.c3__card {
position: absolute; inset: 0;
display: grid; place-items: center;
border-radius: $r-sm;
background: $color;
color: $subject-ink;
font-weight: 700; font-size: 20px; line-height: 1;
box-shadow: $shadow-raised;
will-change: transform, opacity;
}
.ld__badge {
position: absolute; right: $sp-2; top: $sp-2;
display: flex; align-items: baseline; gap: $sp-1;
padding: $sp-1 $sp-2; border-radius: $r-pill;
background: rgba($subject-ink, .78);
color: $subject-cream; font-weight: 700; font-size: 11px;
}
.ld__badgeNum { font-size: 13px; color: $stage-yellow; }
.ld__badgeUnit { font-weight: 700; }
@media (max-width: 340px) {
.ld { gap: $sp-1; }
.ld__field { height: 124px; }
.c3__deck { width: 84px; height: 54px; }
.ld__badge { font-size: 10px; padding: 0 $sp-1; }
.ld__badgeNum { font-size: 11px; }
}
@media (prefers-reduced-motion: reduce) {
.ld.is-still .ld__field { cursor: default !important; }
.ld.is-still .ld__badge { opacity: .92 !important; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #ff4d1f;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.ld {
display: flex;
flex-direction: column;
gap: 8px;
width: 100%;
max-width: 360px;
}
.ld__hint {
margin: 0;
font-weight: 700;
font-size: clamp(10px, 2.8vw, 12px);
line-height: 1.35;
color: #17141a;
text-align: center;
}
.ld__field {
position: relative;
height: 208px;
border-radius: 16px;
overflow: hidden;
background: rgb(100%, 20.6603641457%, 0.1568627451%);
box-shadow: inset 0 0 0 1px rgba(23, 20, 26, 0.16);
display: grid;
place-items: center;
cursor: pointer;
perspective: clamp(320px, 70vw, 480px);
}
.ld__field:focus-visible {
outline: 2px solid #17141a;
outline-offset: 2px;
}
.c3__deck {
position: relative;
width: 116px;
height: 74px;
transform-style: preserve-3d;
}
.c3__card {
position: absolute;
inset: 0;
display: grid;
place-items: center;
border-radius: 8px;
background: #fff7e6;
color: #17141a;
font-weight: 700;
font-size: 20px;
line-height: 1;
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
will-change: transform, opacity;
}
.ld__badge {
position: absolute;
right: 8px;
top: 8px;
display: flex;
align-items: baseline;
gap: 4px;
padding: 4px 8px;
border-radius: 999px;
background: rgba(23, 20, 26, 0.78);
color: #fff7e6;
font-weight: 700;
font-size: 11px;
}
.ld__badgeNum {
font-size: 13px;
color: #ffd23f;
}
.ld__badgeUnit {
font-weight: 700;
}
@media (max-width: 340px) {
.ld {
gap: 4px;
}
.ld__field {
height: 124px;
}
.c3__deck {
width: 84px;
height: 54px;
}
.ld__badge {
font-size: 10px;
padding: 0 4px;
}
.ld__badgeNum {
font-size: 11px;
}
}
@media (prefers-reduced-motion: reduce) {
.ld.is-still .ld__field {
cursor: default !important;
}
.ld.is-still .ld__badge {
opacity: 0.92 !important;
}
}(function () {
'use strict';
var root = document.getElementById('ld');
var field = document.getElementById('field');
var pctEl = document.getElementById('pct');
var cards = [];
for (var ci = 0; ci < 5; ci++) cards.push(document.getElementById('c' + ci));
var CARD_COUNT = 5;
var ZGAP = 22; // 깊이 한 칸당 뒤로 물러나는 거리(px)
var YOFF = 10; // 깊이 한 칸당 아래로 내려가는 거리(px)
var SHRINK = 0.09; // 깊이 한 칸당 축소 비율
var OPACITY_STEP = 0.15;
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var LOOP = 2;
var REDUCE_P = 0.7;
var t0 = 0, raf = 0, paused = false, lastP = 0;
// 카드 i 의 현재 깊이 순위(0=맨 앞)를 진행률의 연속 모듈러 함수로 계산 — 상태를 누적하지 않고
// 매 프레임 p 하나로부터 전체 순서를 다시 구한다(화가 알고리즘과 같은 원리).
function rankOf(i, s) {
var r = (i - s) % CARD_COUNT;
if (r < 0) r += CARD_COUNT;
return r;
}
function render(p) {
lastP = p;
var s = p * CARD_COUNT;
for (var i = 0; i < CARD_COUNT; i++) {
var rank = rankOf(i, s);
var z = -rank * ZGAP;
var y = rank * YOFF;
var scale = 1 - rank * SHRINK;
var op = 1 - rank * OPACITY_STEP;
cards[i].style.transform = 'translateY(' + y + 'px) translateZ(' + z + 'px) scale(' + scale + ')';
cards[i].style.opacity = String(Math.max(0.25, op));
cards[i].style.zIndex = String(Math.round((CARD_COUNT - rank) * 1000));
}
pctEl.textContent = Math.round(p * 100) + '%';
}
function frame(now) {
if (!paused) { var t = (now - t0) / 1000; render((t % LOOP) / LOOP); }
raf = requestAnimationFrame(frame);
}
function togglePause() {
paused = !paused;
root.classList.toggle('is-paused', paused);
render(reduce ? REDUCE_P : lastP);
}
function restart() {
t0 = performance.now();
render(reduce ? REDUCE_P : 0);
}
field.addEventListener('click', function () { togglePause(); });
field.addEventListener('keydown', function (e) {
if (e.key === ' ' || e.key === 'Enter') { e.preventDefault(); togglePause(); }
else if (e.key === 'r' || e.key === 'R') { e.preventDefault(); restart(); }
});
window.addEventListener('resize', function () {
if (paused || reduce) render(reduce ? REDUCE_P : lastP);
});
t0 = performance.now();
if (reduce) {
root.classList.add('is-still');
render(REDUCE_P);
} else {
raf = requestAnimationFrame(frame);
}
})();// CardStackDepthCycle — vanilla 판(run/399/demo/03-loader-3d-card-stack-depth-cycle/index.html) 의 React 포트.
// 카드 i 의 깊이 순위를 진행률의 연속 모듈러 함수로 계산해 매 프레임 transform·opacity·z-index 를 다시 매긴다.
// 395(캔버스 rAF 물리)·393(포인터 tilt) 과 다른 축 — 깊이 순위 자체를 진행률의 모듈러 함수로 계산한다.
import { useEffect, useRef } from "react";
import type { CSSProperties } from "react";
import styles from "./CardStackDepthCycle.module.scss";
export type CardStackDepthCycleProps = {
duration?: string;
easing?: string;
color?: string;
};
type Vars = CSSProperties & { "--duration"?: string; "--easing"?: string; "--color"?: string };
type Box<T> = { current: T };
type KeyEvt = { key: string; preventDefault(): void };
const CARD_COUNT = 5;
const ZGAP = 22;
const YOFF = 10;
const SHRINK = 0.09;
const OPACITY_STEP = 0.15;
const LOOP = 2;
const REDUCE_P = 0.7;
const FIELD_LABEL = "카드 스택 깊이 순환 진행 실험판";
function rankOf(i: number, s: number): number {
let r = (i - s) % CARD_COUNT;
if (r < 0) r += CARD_COUNT;
return r;
}
export default function CardStackDepthCycle({
duration = "2s",
easing = "cubic-bezier(.2,.8,.2,1)",
color = "#fff7e6",
}: CardStackDepthCycleProps) {
const rootRef = useRef<HTMLDivElement | null>(null);
const fieldRef = useRef<HTMLDivElement | null>(null);
const pctRef = useRef<HTMLSpanElement | null>(null);
const cardsRef = useRef<(HTMLDivElement | null)[]>([null, null, null, null, null]) as Box<(HTMLDivElement | null)[]>;
const pausedRef = useRef(false) as Box<boolean>;
const lastPRef = useRef(0) as Box<number>;
const t0Ref = useRef(0) as Box<number>;
const rafRef = useRef(0) as Box<number>;
const vars: Vars = { "--duration": duration, "--easing": easing, "--color": color };
function reduced(): boolean {
return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
}
function render(p: number): void {
lastPRef.current = p;
const s = p * CARD_COUNT;
const cards = cardsRef.current;
for (let i = 0; i < CARD_COUNT; i++) {
const el = cards[i];
if (!el) continue;
const rank = rankOf(i, s);
const z = -rank * ZGAP;
const y = rank * YOFF;
const scale = 1 - rank * SHRINK;
const op = Math.max(0.25, 1 - rank * OPACITY_STEP);
el.style.transform = "translateY(" + y + "px) translateZ(" + z + "px) scale(" + scale + ")";
el.style.opacity = String(op);
el.style.zIndex = String(Math.round((CARD_COUNT - rank) * 1000));
}
if (pctRef.current) pctRef.current.textContent = Math.round(p * 100) + "%";
}
function togglePause(): void {
pausedRef.current = !pausedRef.current;
const root = rootRef.current;
if (root) root.classList.toggle("is-paused", pausedRef.current);
render(reduced() ? REDUCE_P : lastPRef.current);
}
function restart(): void {
t0Ref.current = performance.now();
render(reduced() ? REDUCE_P : 0);
}
function onKey(e: KeyEvt): void {
if (e.key === " " || e.key === "Enter") { e.preventDefault(); togglePause(); }
else if (e.key === "r" || e.key === "R") { e.preventDefault(); restart(); }
}
function onClick(): void {
togglePause();
}
function onResize(): void {
if (pausedRef.current || reduced()) render(reduced() ? REDUCE_P : lastPRef.current);
}
function frame(now: number): void {
if (!pausedRef.current) {
const t = (now - t0Ref.current) / 1000;
render((t % LOOP) / LOOP);
}
rafRef.current = requestAnimationFrame(frame);
}
useEffect(() => {
t0Ref.current = performance.now();
window.addEventListener("resize", onResize);
if (reduced()) {
const root = rootRef.current;
if (root) root.classList.add("is-still");
render(REDUCE_P);
} else {
rafRef.current = requestAnimationFrame(frame);
}
return () => {
window.removeEventListener("resize", onResize);
cancelAnimationFrame(rafRef.current);
};
}, []);
return (
<main className={styles.stage}>
<div className={styles.root} ref={rootRef} style={vars}>
<p className={styles.hint}>클릭하거나 스페이스바를 누르면 멈추거나 다시 갑니다</p>
<div
className={styles.field}
ref={fieldRef}
role="application"
aria-label={FIELD_LABEL}
tabIndex={0}
onClick={onClick}
onKeyDown={onKey}
>
<div className={styles.deck}>
{[1, 2, 3, 4, 5].map((n, i) => (
<div key={n} className={styles.card} ref={(el: HTMLDivElement | null) => { cardsRef.current[i] = el; }}>{n}</div>
))}
</div>
<b className={styles.badge} aria-hidden="true"><span className={styles.badgeNum} ref={pctRef}>0%</span><span className={styles.badgeUnit}>진행</span></b>
</div>
</div>
</main>
);
}03 값이 오를 때마다 뒤 카드가 깊이 순서를 바꿔 앞으로 넘어오는 카드 스택 로더 — 측정 누적면적 5.401% · 움직인 프레임 23/23GODRICH 원작 · zip 에 SCSS·React 포함 · css 로딩바 애니메이션 9가지 — 3D 계산형 로더
이 데모가 속한 글
css 로딩바 애니메이션 9가지 — 3D 계산형 로더css 로딩바 애니메이션도 아홉 개 모두 트윈이 아니라 계산으로 돕니다. 로딩이 시작되는 첫 프레임에는 평평한 정면 한 장만 보이지만, 값이 문턱을 넘을 때마다 정육면체 면이 넘어가고 와이어프레임 정점이 제자리로 모 이 글의 데모 전부 보기

