07画面に入ったら開始
カードがビューポートに60%以上入るまでIntersectionObserverはカウント用クラスを付けず、見えるまで0のまま止まる。
使いどころスクロールの長いLPの実績セクション、ストーリーテリング型の紹介ページ
IntersectionObserverthreshold .6보이기 전 0

<main class="stage">
<div class="viewport is-demo">
<div class="track">
<div class="card">
<span class="card__num" aria-hidden="true"></span>
<span class="card__sr">1,280</span>
<span class="card__label">累計訪問者</span>
</div>
</div>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// ── 변수 3개 ──────────────────────────────────────────
$duration: $dur-loop;
$easing: $ease-out;
$color: $subject-cream;
@include stage($stage-orange);
@property --num {
syntax: '<integer>';
initial-value: 0;
inherits: false;
}
.viewport {
position: relative;
width: min(100%, 220px);
height: clamp(140px, 38vw, 170px);
overflow: hidden; // 뷰포트 밖 카드는 안 보인다 — 실제 화면 가장자리 역할
border-radius: $r-card;
background: rgba(255, 255, 255, .08);
}
.track {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.card {
text-align: center;
&__num {
--num: 0;
counter-reset: num var(--num);
display: block;
font-weight: 800; font-size: clamp(24px, 7vw, 34px); line-height: 1; font-family: inherit;
font-variant-numeric: tabular-nums;
color: $color;
&::before { content: counter(num); }
}
&__sr {
position: absolute;
width: 1px; height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
&__label {
display: block;
margin-top: 6px;
font-weight: 600; font-size: clamp(11px, 3.2vw, 13px); line-height: 1; font-family: inherit;
color: $subject-ink; // 주황 면 위 크림 글자는 2.28:1 — 잉크로 5.90:1
}
// 실제 페이지: JS 가 붙인 .is-counting 이 있어야 카운트가 돈다 (뷰포트 밖이면 0에 멈춰 있다)
&__num { animation-play-state: paused; }
&.is-counting &__num { animation-play-state: running; }
}
// ── 데모(.is-demo): 카드가 스크롤하듯 아래→가운데→위로 지나가고, 가운데 있는 동안만 카운트가 돈다 ──
.viewport.is-demo .track {
animation: scroll-pass $duration $easing infinite;
}
.viewport.is-demo .card__num {
animation: count-when-visible $duration $easing infinite;
}
@keyframes scroll-pass {
0% { transform: translateY(64px); }
20%, 75% { transform: translateY(0); }
100% { transform: translateY(-64px); }
}
@keyframes count-when-visible {
0%, 20% { --num: 0; }
60% { --num: 1280; }
100% { --num: 1280; }
}
@media (prefers-reduced-motion: reduce) {
.viewport.is-demo .track { animation: none; transform: translateY(0); }
.viewport.is-demo .card__num { animation: none; --num: 1280; }
}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);
}
@property --num {
syntax: "<integer>";
initial-value: 0;
inherits: false;
}
.viewport {
position: relative;
width: min(100%, 220px);
height: clamp(140px, 38vw, 170px);
overflow: hidden;
border-radius: 16px;
background: rgba(255, 255, 255, 0.08);
}
.track {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.card {
text-align: center;
}
.card__num {
--num: 0;
counter-reset: num var(--num);
display: block;
font-weight: 800;
font-size: clamp(24px, 7vw, 34px);
line-height: 1;
font-family: inherit;
font-variant-numeric: tabular-nums;
color: #fff7e6;
}
.card__num::before {
content: counter(num);
}
.card__sr {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
.card__label {
display: block;
margin-top: 6px;
font-weight: 600;
font-size: clamp(11px, 3.2vw, 13px);
line-height: 1;
font-family: inherit;
color: #17141a;
}
.card__num {
animation-play-state: paused;
}
.card.is-counting .card__num {
animation-play-state: running;
}
.viewport.is-demo .track {
animation: scroll-pass 2s ease-out infinite;
}
.viewport.is-demo .card__num {
animation: count-when-visible 2s ease-out infinite;
}
@keyframes scroll-pass {
0% {
transform: translateY(64px);
}
20%, 75% {
transform: translateY(0);
}
100% {
transform: translateY(-64px);
}
}
@keyframes count-when-visible {
0%, 20% {
--num: 0;
}
60% {
--num: 1280;
}
100% {
--num: 1280;
}
}
@media (prefers-reduced-motion: reduce) {
.viewport.is-demo .track {
animation: none;
transform: translateY(0);
}
.viewport.is-demo .card__num {
animation: none;
--num: 1280;
}
}// 실제 페이지: 카드가 뷰포트에 60% 이상 들어와야 카운트를 시작한다 (스크롤해서 보이기 전엔 0)
var card = document.querySelector('.card');
new IntersectionObserver(function (es) {
card.classList.toggle('is-counting', es[0].isIntersecting);
}, { threshold: .6 }).observe(card);import { useEffect, useRef, useState } from "react";
import type { CSSProperties } from "react";
import styles from "./CounterOnViewStart.module.scss";
export type CounterOnViewStartProps = {
value: number;
label: string;
duration?: number;
easing?: string;
color?: string;
};
type Vars = CSSProperties & { "--duration"?: string; "--easing"?: string; "--color"?: string; "--num-target"?: number };
export default function CounterOnViewStart({
value, label, duration = 1200, easing = "ease-out", color = "#fff7e6",
}: CounterOnViewStartProps) {
const ref = useRef<HTMLDivElement>(null);
const [counting, setCounting] = useState(false);
useEffect(() => {
const el = ref.current;
if (!el) return;
const io = new IntersectionObserver(
(entries) => setCounting(entries[0].isIntersecting),
{ threshold: .6 },
);
io.observe(el);
return () => io.disconnect();
}, []);
const vars: Vars = {
"--duration": `${duration}ms`,
"--easing": easing,
"--color": color,
"--num-target": value,
};
return (
<div ref={ref} className={`${styles.card} ${counting ? styles.isCounting : ""}`} style={vars}>
<span className={styles.num} aria-hidden="true" />
<span className={styles.sr}>{value.toLocaleString()}</span>
<span className={styles.label}>{label}</span>
</div>
);
}07 画面に入ったら開始 — 計測 累積面積 6.182% · 動いたフレーム 20/23GODRICH オリジナル · SCSS と React は zip に同梱 · CSS 数字 カウントアップ アニメーション9選 — 一行でコピペOK
このデモが属する記事
CSS 数字 カウントアップ アニメーション9選 — 一行でコピペOKcss 数字 カウントアップは、0や開始値から目標の数字まで上がっていくテキストを、JavaScriptなしでCSSのカスタムプロパティだけで数える手法です。9つのうち1つは逆に0へ向かって減っていき、別の1つはスクロールして画面に入るまで この記事のデモをすべて見る


