Getting Started
Components
Special Effects
Animations
Text Animations
Backgrounds
Animate numbers to count up or down to a target number
motion-sv
0
number-ticker.svelte
<script lang="ts"> import { NumberTicker } from "$lib/components/magic/number-ticker/index.js";</script>
<NumberTicker value={100} class="text-8xl font-medium tracking-tighter whitespace-pre-wrap text-black dark:text-white"/>Installation
pnpm dlx shadcn-svelte@latest add https://sv-animations.vercel.app/r/number-ticker.json
Install dependencies
Install the required packages for this component:
pnpm add motion-sv
Copy the Source Code
Copy and paste the following code into your project:
number-ticker.svelte
<script lang="ts"> import type { ClassValue } from "svelte/elements"; import { onMount } from "svelte"; import { useInView } from "motion-sv"; import { cn } from "#lib/utils.js";
interface NumberTickerProps { value: number; startValue?: number; direction?: "up" | "down"; delay?: number; decimalPlaces?: number; class?: ClassValue; prefix?: string; suffix?: string; once?: boolean; }
let { value, startValue = 0, direction = "up", delay = 0, decimalPlaces = 0, class: className, prefix = "", suffix = "", once = true, }: NumberTickerProps = $props();
let spanRef: HTMLSpanElement | null = $state(null);
// Spring configuration const damping = 60; const stiffness = 100;
// Spring physics simulation function animateValue(from: number, to: number) { const startTime = performance.now(); const duration = 2000; // Duration in ms
let velocity = 0; let position = from; const target = to;
function step(currentTime: number) { const elapsed = currentTime - startTime; const dt = 16.67 / 1000; // ~60fps in seconds
// Spring physics const springForce = -stiffness * (position - target); const dampingForce = -damping * velocity; const acceleration = springForce + dampingForce;
velocity += acceleration * dt; position += velocity * dt;
// Update display if (spanRef) { spanRef.textContent = `${prefix}${Intl.NumberFormat("en-US", { minimumFractionDigits: decimalPlaces, maximumFractionDigits: decimalPlaces, }).format(Number(position.toFixed(decimalPlaces)))}${suffix}`; }
// Continue animation if not settled const isSettled = Math.abs(velocity) < 0.01 && Math.abs(position - target) < 0.01; if (!isSettled && elapsed < duration * 2) { requestAnimationFrame(step); } else if (spanRef) { // Ensure final value is exact spanRef.textContent = `${prefix}${Intl.NumberFormat("en-US", { minimumFractionDigits: decimalPlaces, maximumFractionDigits: decimalPlaces, }).format(Number(target.toFixed(decimalPlaces)))}${suffix}`; } }
requestAnimationFrame(step); }
const view = useInView( () => spanRef!, () => ({ once: once, margin: "0px", }) as any ); $effect(() => { let timer: ReturnType<typeof setTimeout> | null = null; if (view.current) { timer = setTimeout(() => { animateValue( direction === "down" ? value : startValue, direction === "down" ? startValue : value ); }, delay); } return () => { if (timer !== null) { clearTimeout(timer); } }; });</script>
<span bind:this={spanRef} class={cn("inline-block tracking-wider text-black tabular-nums dark:text-white", className)}> {prefix} {Intl.NumberFormat("en-US", { minimumFractionDigits: decimalPlaces, maximumFractionDigits: decimalPlaces, }).format(Number((direction === "down" ? value : startValue).toFixed(decimalPlaces)))} {suffix}</span> index.ts
import NumberTicker from "./number-ticker.svelte";export { NumberTicker };Folder Structure
Folder Structure
src/└── lib/ └── components/ └── magic/ └── number-ticker/ ├── number-ticker.svelte └── index.tsInstall and integrate the Svelte component documented below.https://sv-animations.vercel.app/magic/docs/components/number-ticker/llms.txt
Follow the guide's installation and usage instructions.Match the project's existing structure, imports, and styles.Keep changes scoped to the component and verify it works.Reply with a short bullet list of completed changes.Usage
<script lang="ts"> import { NumberTicker } from "#lib/components/magic/number-ticker/index.js";</script>
<NumberTicker value={100} />Examples
With Decimal Places
0.00
number-ticker.svelte
<script> import { NumberTicker } from "$lib/components/magic/number-ticker/index.js";</script>
<NumberTicker value={5.67} decimalPlaces={2} class="text-8xl font-medium tracking-tighter whitespace-pre-wrap text-black dark:text-white"/>With Start Value
80
number-ticker.svelte
<script lang="ts"> import { NumberTicker } from "$lib/components/magic/number-ticker/index.js";</script>
<NumberTicker value={100} startValue={80} class="text-8xl font-medium tracking-tighter whitespace-pre-wrap text-black dark:text-white"/>With Prefix and Suffix
$
70.00
USD
number-ticker.svelte
<script lang="ts"> import { NumberTicker } from "$lib/components/magic/number-ticker/index.js";</script>
<NumberTicker value={100.5} decimalPlaces={2} startValue={70} prefix="$" suffix="USD" class="text-8xl font-medium tracking-tighter whitespace-pre-wrap text-black dark:text-white"/>Trigger On View (without once)
0.00
number-ticker.svelte
<script> import { NumberTicker } from "$lib/components/magic/number-ticker/index.js";</script>
<NumberTicker value={5.67} decimalPlaces={2} once={false} class="text-4xl font-medium tracking-tighter whitespace-pre-wrap text-black md:text-6xl dark:text-white"/>Props
Number Ticker
A component for animating numbers to count up or down to a target value.
| Name | Type | Default | Description |
|---|---|---|---|
value | number | undefined | The target number to animate to. |
startValue | number | 0 | The starting number for the animation. |
direction | "up" | "down" | "up" | The direction of the animation. |
delay | number | 0 | Delay before starting the animation in seconds. |
decimalPlaces | number | 0 | Number of decimal places to display. |
class | ClassValue | "" | Additional CSS classes to apply. |
prefix | string | "" | Prefix to display before the number. |
suffix | string | "" | Suffix to display after the number. |
once | boolean | true | Whether to animate only the first time the component comes into view. |