Skip to content
Focsd homeFocsd homeFocsd
Forms

Animated number

A number that counts to its value, for figures that change on screen.

  • Tag: focsd-animated-number
  • Pattern: Counting text (hidden) + visually hidden final value
  • Category: Utility

Example

Total: orders

Add 120 orders
HTML
<p>
Total:
<focsd-animated-number
id="count-demo"
value="1280"
format="decimal 0"
duration="900"></focsd-animated-number>
orders
</p>
<focsd-button id="count-demo-button">Add 120 orders</focsd-button>
<script type="module">
const number = document.getElementById('count-demo');
document.getElementById('count-demo-button').addEventListener('click', () => {
number.value += 120;
});
</script>

Usage

Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.

<script type="module">
import '@focsd-dev/components/focsd-animated-number.js';
</script>
<focsd-animated-number value="1280" format="decimal 0" duration="900"></focsd-animated-number>

API

Attributes

AttributePropertyTypeDefault
valuevaluenumber0
formatformatIntl.NumberFormatOptions | undefined—
fromfromnumber | undefined—
durationdurationnumber | undefined—

CSS parts

Style the inside of the component with ::part(), for example focsd-animated-number::part(number).

PartDescription
numberThe counting text (hidden from assistive technology).

Accessibility

  • The counting digits are hidden from assistive technology. A visually hidden copy of the final value is what is read, once, so a screen reader never hears 1,281, 1,282, 1,283.
  • Under prefers-reduced-motion: reduce the text is the final value in the same frame: there is no counting.
  • format takes a short form (decimal 0, currency USD, percent 1, compact) or Intl.NumberFormat options (JSON as an attribute); an invalid format warns once and falls back to the plain number. from sets where a count starts and duration how long it takes, in milliseconds.
  • The change is not announced as a live region. If people need to hear it, announce it from your own code.