Media

Number Count

Numbers count up as soon as they come into view — in exactly the notation already written in the Designer.

v1.1.04,1 kB gzipfree

View live demoGet the cloneableTutorial video
On this page

Add the script

Head code · inside <head>
<!-- FlowPowerUser · Number Count -->
<script defer src="https://cdn.jsdelivr.net/npm/@flowpoweruser/attributes@1/number-count.js"></script>

Once per project is enough, even if you use the solution on several pages: Project Settings → Custom Code → Head Code.

The script loads alongside the page without holding up rendering; it runs once the page is in place and does nothing while no fpu attribute is there.

Set the attribute

Write the number into the Designer the way it should read in the end — with separators, with the percent sign, with everything that belongs to it.

Then set the attribute on it. The target value is read from exactly that text; a separate value is only needed if it should differ from the text.

text only inside

Turns the solution on for this element.

In the Navigator
Text Block   fpu-count-element="number"
             Content: 1,250 +

Check

Publish the page and scroll far enough for the number to come into view.

Two things have to hold: it starts counting once visible, and in the end exactly the Designer text is back — the same separators, the same decimals, the same prefix and suffix. If it stays put, fpu-count-logging="true" helps: it writes one line to the browser console on start.

Optional

Start several numbers staggeredDefault: 150 ms offset

When several metrics sit side by side, they should rarely all jump at once.

Set the attribute on the surrounding container: it starts all counters inside it staggered as soon as it becomes visible, and passes its options down to its members — whatever you set on the container applies to every number within.

On the container

Starts all number counters inside it staggered as soon as the container becomes visible, and passes its options down to its members.

On the container

Offset in milliseconds between two counters of the group.

Default: 150
Values, duration and repeatingDefault: 0 → text, 2000 ms

Without any setting the number counts from 0 to the value in the text, taking two seconds.

These four attributes set each part of that individually. If the start value is greater than the target value, the number counts down instead of up.

on the group it applies to all

Target value override, in case it should not come from the text content.

Default: value from the text
on the group it applies to all

Start value. If it is above the target value, it counts down.

Default: 0
on the group it applies to all

Animation duration in milliseconds.

Default: 2000
on the group it applies to all

Counts again on every viewport entry.

Default: false
When it starts countingDefault: 40% of the element

Without any setting the number starts counting once 40 percent of it is in view.

To make that happen earlier or later, this attribute sets the point yourself — either as a share of the element or as a fixed distance. With “px”, “vh” and “vw” what counts is how much of the element is visible; if the element itself is shorter than the required value, having it fully in view is enough, so a small counter never waits for a distance it can never reach.

for groups the container counts

When counting begins.

Default: 40%
Thousands separator and consoleOtherwise as in the text

The thousands separator normally comes from the original text.

If it should be a different one, this attribute sets it — either as a keyword or literally as the character to appear.

On the number or the group

Thousands separator override.

Default: as in the original text
On the number or the group

Writes one line to the browser console on start.

Default: false
Defaults for the whole projectthe attribute always wins

If every number in the project should share the same duration or separator, that can be set centrally once instead of writing it onto every element.

An attribute on the element always beats this value.

Before the script in the head code
<script>
  window.fpuNumberCountConfig = {
    duration: 2000,
    stagger: 150,
    repeat: false,
    separator: ",",
    threshold: "40%",
    logging: false,
  };
</script>
The principle · Why Number Count
The principle · Why Number Count

The target value is not configured, it is read. Whatever stands in the Designer as text — 1.234, 1,234, 1'234 or 1 234, with “€ ” in front or “ Mio.” behind — is taken apart: the number for the animation, everything else as the shape it returns to. That is why the final state matches the Designer text exactly, and when the number changes in the CMS, the counter changes with it.

  1. ReadThe text content is parsed: thousands separator, decimal separator, prefix and suffix are recognised and preserved.
  2. WaitAn IntersectionObserver waits until enough of the number is in view — 40 percent out of the box, a value of your own on request. Once, unless it is meant to repeat.
  3. CountThe animation runs via requestAnimationFrame with easeOutCubic; formatting goes through Intl.NumberFormat using the language from <html lang>.
Additional features
  • Anyone asking for less motion gets noneWith “reduce motion” set in the operating system there is no counting: the number sits at its final value right away. The content is therefore fully readable from the first second.
  • Tall elements are measured differentlyAn element taller than the viewport never reaches a share of 40 percent — it simply does not fit. There the visible portion of the window decides instead of the share of the element, so large number blocks start reliably too.
  • CMS counters loaded later come alongThe solution listens for the “fpu:items-changed” event and initialises newly rendered counters afterwards. Numbers that only appear through filtering, sorting or loading more therefore count just the same.
  • Your own code can hook inThe running counters, a fresh start and the cleanup are reachable from outside: instances, init and destroyAll.
  • The element should contain text onlyDuring the animation the content is replaced via textContent. Child elements inside the number are lost in the process — prefix and suffix therefore belong in the same text content, not in separate spans.

Access currently by invitation only