Skip to content

Counter

A component that displays an animated numerical counter that counts up to a specified value. It’s useful for displaying statistics, achievements, or any numerical data in an engaging way.

  • Metro UI Core
  • Dom
<!-- Basic counter that counts from 0 to 100 -->
<div data-role="counter" data-value="100">0</div>
<!-- Counter with custom starting value -->
<div data-role="counter" data-value="100" data-from="50">50</div>
<!-- Counter with prefix and suffix -->
<div data-role="counter" data-value="100" data-prefix="$" data-suffix="+">0</div>
<!-- Counter with 5 second animation duration -->
<div data-role="counter" data-value="1000" data-duration="5000">0</div>
<!-- Counter that starts when it enters the viewport -->
<div data-role="counter" data-value="500" data-start-on-viewport="true">0</div>
ParameterTypeDefaultDescription
startOnViewportbooleanfalseWhen true, the counter starts when it enters the viewport
counterDeferrednumber0Delay in milliseconds before the counter starts
durationnumber2000Duration of the counting animation in milliseconds
valuenumber0The target value to count to
fromnumber0The starting value to count from
timeoutnumber0Additional timeout before starting the animation
delimiterstring”,“Character used as the thousands separator
prefixstring""Text to display before the number
suffixstring""Text to display after the number
sizenumber16Font size in pixels
onStartfunctionMetro.noopCallback function when counter animation starts
onStopfunctionMetro.noopCallback function when counter animation completes
onTickfunctionMetro.noopCallback function on each frame of the animation with the current value
onCounterCreatefunctionMetro.noopCallback function when counter is created
  • start(val, from) - Starts the counter animation. Optional parameters: val (target value), from (starting value).
  • reset() - Resets the counter to its initial state.
  • changeAttribute(attr, val) - Changes the specified attribute with the given value.
  • destroy() - Removes the counter element from the DOM.
const counter = Metro.getPlugin('#myCounter', 'counter');
// Start counting to the value specified in the options
counter.start();
// Start counting to a specific value
counter.start(200);
// Start counting from a specific value to a target value
counter.start(200, 50);
// Reset the counter
counter.reset();
EventDescription
onStartTriggered when the counter animation starts
onStopTriggered when the counter animation completes
onTickTriggered on each frame of the animation with the current value
onCounterCreateTriggered when the counter is created

The Counter component primarily uses the size parameter to control the font size. You can also style the counter using standard CSS properties.

/* Custom styling for a counter */
#myCounter {
font-weight: bold;
color: #0078D7;
font-family: 'Segoe UI', sans-serif;
}

The Counter component doesn’t add specific CSS classes to the element. It directly manipulates the font size of the element based on the size parameter.

You can set global defaults for all counter components:

Metro.counterSetup({
duration: 3000,
delimiter: ' ',
prefix: '# '
});
<div id="statsCounter"
data-role="counter"
data-value="1000"
data-from="0"
data-duration="3000"
data-start-on-viewport="true"
data-prefix="$"
data-suffix=" USD"
data-delimiter=","
data-size="24">
0
</div>

You can also initialize the counter component via JavaScript:

Metro.makePlugin(element, 'counter', {
value: 1000,
duration: 3000,
prefix: '$',
suffix: ' USD',
onStop: function() {
console.log('Counter finished!');
}
});