Skip to content

Animated Number

Animate a balance or a counter while it changes, with the digits in the format of the locale. An sv-text-flow in the number slot gets each formatted number as its text and rolls the digits.

Reduced Motion

Your system is set to reduce motion, so the animations shown on this page will not play for you.

Code

The following example assumes that you have imported the components and set up the theme.

Animated Number
<div class="number-formatter-animated">
8 collapsed lines
<sv-number-formatter currency="USD" format-style="currency" value="1250">
<sv-text-flow class="number-formatter-animated-balance" slot="number"></sv-text-flow>
</sv-number-formatter>
<div class="number-formatter-animated-actions">
<button type="button" class="btn" data-amount="-75.5">Pay $75.50</button>
<button type="button" class="btn btn-primary" data-amount="1200">Add $1,200</button>
</div>
</div>
<script type="application/x-typescript" module>
10 collapsed lines
const numberFormatter = document.querySelector<NumberFormatter>(
".number-formatter-animated sv-number-formatter"
)!;
const buttons = document.querySelectorAll<HTMLButtonElement>(".number-formatter-animated button");
for (const button of buttons) {
button.addEventListener("click", () => {
numberFormatter.value = (numberFormatter.value ?? 0) + Number(button.dataset.amount);
});
}
</script>
<style>
17 collapsed lines
.number-formatter-animated {
display: grid;
gap: 1em;
place-content: center;
justify-items: center;
}
.number-formatter-animated-balance::part(text) {
font-size: 2.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
.number-formatter-animated-actions {
display: flex;
gap: 0.5em;
}
</style>