- Docs
- Working examples
Working examples
Every live Counter preview with its explanation and the complete, copyable source files of each one.
Each preview is the same one shown on Home and Examples, and runs the real package. The copied files load the published browser bundle, so they work as they are.
Count a stats band when it scrolls in
The HTML holds the final values, so they read correctly without JavaScript. Each number counts once, when half of it is visible, with the decimals of its target.
Scroll down
- Customers
- 12,480
- Uptime
- 99.95%
- Countries
- +42
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Count a stats band when it scrolls in</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div id="page">
<p class="spacer">Scroll down</p>
<dl class="stats">
<div>
<dt>Customers</dt>
<dd><span data-counter="12480">12,480</span></dd>
</div>
<div>
<dt>Uptime</dt>
<dd><span data-counter="99.95">99.95</span>%</dd>
</div>
<div>
<dt>Countries</dt>
<dd>+<span data-counter="42">42</span></dd>
</div>
</dl>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
#page {
height: 16rem;
overflow-y: auto;
padding: 1rem;
border: 1px solid #ccc;
}
.spacer {
min-height: 20rem;
margin: 0;
}
.stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(8rem, 1fr));
gap: 1rem;
margin: 0;
}
dd {
margin: 0;
font-size: 2rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
JavaScript
// The box scrolls instead of the page, so pass it as the root.
// Called before the DOM is ready, this replaces the automatic start.
Counter.auto({ root: document.querySelector('#page') })
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Count a stats band when it scrolls in</title>
<style>
#page {
height: 16rem;
overflow-y: auto;
padding: 1rem;
border: 1px solid #ccc;
}
.spacer {
min-height: 20rem;
margin: 0;
}
.stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(8rem, 1fr));
gap: 1rem;
margin: 0;
}
dd {
margin: 0;
font-size: 2rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
</style>
</head>
<body>
<main>
<div id="page">
<p class="spacer">Scroll down</p>
<dl class="stats">
<div>
<dt>Customers</dt>
<dd><span data-counter="12480">12,480</span></dd>
</div>
<div>
<dt>Uptime</dt>
<dd><span data-counter="99.95">99.95</span>%</dd>
</div>
<div>
<dt>Countries</dt>
<dd>+<span data-counter="42">42</span></dd>
</div>
</dl>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script>
// The box scrolls instead of the page, so pass it as the root.
// Called before the DOM is ready, this replaces the automatic start.
Counter.auto({ root: document.querySelector('#page') })
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Count a stats band when it scrolls in</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div id="page">
<p class="spacer">Scroll down</p>
<dl class="stats">
<div>
<dt>Customers</dt>
<dd><span data-counter="12480">12,480</span></dd>
</div>
<div>
<dt>Uptime</dt>
<dd><span data-counter="99.95">99.95</span>%</dd>
</div>
<div>
<dt>Countries</dt>
<dd>+<span data-counter="42">42</span></dd>
</div>
</dl>
</div>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
#page {
height: 16rem;
overflow-y: auto;
padding: 1rem;
border: 1px solid #ccc;
}
.spacer {
min-height: 20rem;
margin: 0;
}
.stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(8rem, 1fr));
gap: 1rem;
margin: 0;
}
dd {
margin: 0;
font-size: 2rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
JavaScript
import { autoCounter, counter } from '@samline/counter'
// The box scrolls instead of the page, so pass it as the root.
autoCounter({ root: document.querySelector('#page') })
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Count a stats band when it scrolls in</title>
<style>
#page {
height: 16rem;
overflow-y: auto;
padding: 1rem;
border: 1px solid #ccc;
}
.spacer {
min-height: 20rem;
margin: 0;
}
.stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(8rem, 1fr));
gap: 1rem;
margin: 0;
}
dd {
margin: 0;
font-size: 2rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
</style>
</head>
<body>
<main>
<div id="page">
<p class="spacer">Scroll down</p>
<dl class="stats">
<div>
<dt>Customers</dt>
<dd><span data-counter="12480">12,480</span></dd>
</div>
<div>
<dt>Uptime</dt>
<dd><span data-counter="99.95">99.95</span>%</dd>
</div>
<div>
<dt>Countries</dt>
<dd>+<span data-counter="42">42</span></dd>
</div>
</dl>
</div>
</main>
<script type="module">
import { autoCounter, counter } from '@samline/counter'
// The box scrolls instead of the page, so pass it as the root.
autoCounter({ root: document.querySelector('#page') })
</script>
</body>
</html>
Currencies and number formats per language
Without data-counter, the final value is read from the text in the language of the element. Intl.NumberFormat options add currencies, and grouping follows the locale.
- $1,234.50 en-US
- 1.234.567,89 de-DE
- 1 234 567 fr-FR
- 12,34,567 en-IN
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Currencies and number formats per language</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<ul class="prices">
<li lang="en-US"><span id="usd">$1,234.50</span> en-US</li>
<li lang="de-DE"><span class="plain">1.234.567,89</span> de-DE</li>
<li lang="fr-FR"><span class="plain">1 234 567</span> fr-FR</li>
<li lang="en-IN"><span class="plain">12,34,567</span> en-IN</li>
</ul>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
.prices {
list-style: none;
margin: 0;
padding: 0;
display: grid;
gap: 0.5rem;
}
.prices span {
display: inline-block;
min-width: 9rem;
font-size: 1.5rem;
font-variant-numeric: tabular-nums;
}
JavaScript
Counter.counter('#usd', { format: { style: 'currency', currency: 'USD' } })
Counter.counter('.plain')
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Currencies and number formats per language</title>
<style>
.prices {
list-style: none;
margin: 0;
padding: 0;
display: grid;
gap: 0.5rem;
}
.prices span {
display: inline-block;
min-width: 9rem;
font-size: 1.5rem;
font-variant-numeric: tabular-nums;
}
</style>
</head>
<body>
<main>
<ul class="prices">
<li lang="en-US"><span id="usd">$1,234.50</span> en-US</li>
<li lang="de-DE"><span class="plain">1.234.567,89</span> de-DE</li>
<li lang="fr-FR"><span class="plain">1 234 567</span> fr-FR</li>
<li lang="en-IN"><span class="plain">12,34,567</span> en-IN</li>
</ul>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script>
Counter.counter('#usd', { format: { style: 'currency', currency: 'USD' } })
Counter.counter('.plain')
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Currencies and number formats per language</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<ul class="prices">
<li lang="en-US"><span id="usd">$1,234.50</span> en-US</li>
<li lang="de-DE"><span class="plain">1.234.567,89</span> de-DE</li>
<li lang="fr-FR"><span class="plain">1 234 567</span> fr-FR</li>
<li lang="en-IN"><span class="plain">12,34,567</span> en-IN</li>
</ul>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
.prices {
list-style: none;
margin: 0;
padding: 0;
display: grid;
gap: 0.5rem;
}
.prices span {
display: inline-block;
min-width: 9rem;
font-size: 1.5rem;
font-variant-numeric: tabular-nums;
}
JavaScript
import { autoCounter, counter } from '@samline/counter'
counter('#usd', { format: { style: 'currency', currency: 'USD' } })
counter('.plain')
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Currencies and number formats per language</title>
<style>
.prices {
list-style: none;
margin: 0;
padding: 0;
display: grid;
gap: 0.5rem;
}
.prices span {
display: inline-block;
min-width: 9rem;
font-size: 1.5rem;
font-variant-numeric: tabular-nums;
}
</style>
</head>
<body>
<main>
<ul class="prices">
<li lang="en-US"><span id="usd">$1,234.50</span> en-US</li>
<li lang="de-DE"><span class="plain">1.234.567,89</span> de-DE</li>
<li lang="fr-FR"><span class="plain">1 234 567</span> fr-FR</li>
<li lang="en-IN"><span class="plain">12,34,567</span> en-IN</li>
</ul>
</main>
<script type="module">
import { autoCounter, counter } from '@samline/counter'
counter('#usd', { format: { style: 'currency', currency: 'USD' } })
counter('.plain')
</script>
</body>
</html>
Grouping, decimals and the text around the number
By default a number counts the way the HTML writes it: 8000 stays 8000 and 8,000 stays 8,000. data-counter-grouping="true" adds the separators of the language, decimals fixes the decimals, and text around the number stays unless a currency, percent or unit style adds its own.
- As written: no separators
- 8000
- As written: separators and decimals
- 8,000.45
- grouping="true"
- 8000
- grouping="true" keeps the $
- $8000
- grouping="true" decimals="2"
- 8000
- grouping="false"
- 8,000
- grouping="true" lang="de"
- 8000,5
- format: compact, suffix kept
- 8000 visits
- format: currency + grouping: true
- 8000
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Grouping, decimals and the text around the number</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="formats">
<dl lang="en">
<div>
<dt>As written: no separators</dt>
<dd><span data-counter>8000</span></dd>
</div>
<div>
<dt>As written: separators and decimals</dt>
<dd><span data-counter>8,000.45</span></dd>
</div>
<div>
<dt>grouping="true"</dt>
<dd><span data-counter data-counter-grouping="true">8000</span></dd>
</div>
<div>
<dt>grouping="true" keeps the $</dt>
<dd><span data-counter data-counter-grouping="true">$8000</span></dd>
</div>
<div>
<dt>grouping="true" decimals="2"</dt>
<dd>
<span data-counter data-counter-grouping="true" data-counter-decimals="2">8000</span>
</dd>
</div>
<div>
<dt>grouping="false"</dt>
<dd><span data-counter data-counter-grouping="false">8,000</span></dd>
</div>
<div>
<dt>grouping="true" lang="de"</dt>
<dd><span data-counter data-counter-grouping="true" lang="de">8000,5</span></dd>
</div>
<div>
<dt>format: compact, suffix kept</dt>
<dd><span id="compact">8000 visits</span></dd>
</div>
<div>
<dt>format: currency + grouping: true</dt>
<dd><span id="usd">8000</span></dd>
</div>
</dl>
<button type="button" id="again">Count again</button>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
.formats {
display: grid;
gap: 1rem;
justify-items: start;
}
.formats dl {
display: grid;
gap: 0.5rem;
margin: 0;
}
.formats dl div {
display: grid;
grid-template-columns: minmax(12rem, 1fr) auto;
gap: 1rem;
align-items: baseline;
}
.formats dt {
font:
0.875rem/1.4 ui-monospace,
monospace;
}
.formats dd {
margin: 0;
font-size: 1.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
JavaScript
// Every [data-counter] counts on its own; these two take JavaScript options.
Counter.counter('#compact', { format: { notation: 'compact' } })
Counter.counter('#usd', { grouping: true, format: { style: 'currency', currency: 'USD' } })
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.formats dd span')
.forEach(number => Counter.getCounter(number)?.start())
)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Grouping, decimals and the text around the number</title>
<style>
.formats {
display: grid;
gap: 1rem;
justify-items: start;
}
.formats dl {
display: grid;
gap: 0.5rem;
margin: 0;
}
.formats dl div {
display: grid;
grid-template-columns: minmax(12rem, 1fr) auto;
gap: 1rem;
align-items: baseline;
}
.formats dt {
font:
0.875rem/1.4 ui-monospace,
monospace;
}
.formats dd {
margin: 0;
font-size: 1.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
</style>
</head>
<body>
<main>
<div class="formats">
<dl lang="en">
<div>
<dt>As written: no separators</dt>
<dd><span data-counter>8000</span></dd>
</div>
<div>
<dt>As written: separators and decimals</dt>
<dd><span data-counter>8,000.45</span></dd>
</div>
<div>
<dt>grouping="true"</dt>
<dd><span data-counter data-counter-grouping="true">8000</span></dd>
</div>
<div>
<dt>grouping="true" keeps the $</dt>
<dd><span data-counter data-counter-grouping="true">$8000</span></dd>
</div>
<div>
<dt>grouping="true" decimals="2"</dt>
<dd>
<span data-counter data-counter-grouping="true" data-counter-decimals="2">8000</span>
</dd>
</div>
<div>
<dt>grouping="false"</dt>
<dd><span data-counter data-counter-grouping="false">8,000</span></dd>
</div>
<div>
<dt>grouping="true" lang="de"</dt>
<dd><span data-counter data-counter-grouping="true" lang="de">8000,5</span></dd>
</div>
<div>
<dt>format: compact, suffix kept</dt>
<dd><span id="compact">8000 visits</span></dd>
</div>
<div>
<dt>format: currency + grouping: true</dt>
<dd><span id="usd">8000</span></dd>
</div>
</dl>
<button type="button" id="again">Count again</button>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script>
// Every [data-counter] counts on its own; these two take JavaScript options.
Counter.counter('#compact', { format: { notation: 'compact' } })
Counter.counter('#usd', { grouping: true, format: { style: 'currency', currency: 'USD' } })
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.formats dd span')
.forEach(number => Counter.getCounter(number)?.start())
)
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Grouping, decimals and the text around the number</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="formats">
<dl lang="en">
<div>
<dt>As written: no separators</dt>
<dd><span data-counter>8000</span></dd>
</div>
<div>
<dt>As written: separators and decimals</dt>
<dd><span data-counter>8,000.45</span></dd>
</div>
<div>
<dt>grouping="true"</dt>
<dd><span data-counter data-counter-grouping="true">8000</span></dd>
</div>
<div>
<dt>grouping="true" keeps the $</dt>
<dd><span data-counter data-counter-grouping="true">$8000</span></dd>
</div>
<div>
<dt>grouping="true" decimals="2"</dt>
<dd>
<span data-counter data-counter-grouping="true" data-counter-decimals="2">8000</span>
</dd>
</div>
<div>
<dt>grouping="false"</dt>
<dd><span data-counter data-counter-grouping="false">8,000</span></dd>
</div>
<div>
<dt>grouping="true" lang="de"</dt>
<dd><span data-counter data-counter-grouping="true" lang="de">8000,5</span></dd>
</div>
<div>
<dt>format: compact, suffix kept</dt>
<dd><span id="compact">8000 visits</span></dd>
</div>
<div>
<dt>format: currency + grouping: true</dt>
<dd><span id="usd">8000</span></dd>
</div>
</dl>
<button type="button" id="again">Count again</button>
</div>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
.formats {
display: grid;
gap: 1rem;
justify-items: start;
}
.formats dl {
display: grid;
gap: 0.5rem;
margin: 0;
}
.formats dl div {
display: grid;
grid-template-columns: minmax(12rem, 1fr) auto;
gap: 1rem;
align-items: baseline;
}
.formats dt {
font:
0.875rem/1.4 ui-monospace,
monospace;
}
.formats dd {
margin: 0;
font-size: 1.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
JavaScript
import { autoCounter, counter, getCounter } from '@samline/counter'
autoCounter()
counter('#compact', { format: { notation: 'compact' } })
counter('#usd', { grouping: true, format: { style: 'currency', currency: 'USD' } })
document
.querySelector('#again')
.addEventListener('click', () =>
document.querySelectorAll('.formats dd span').forEach(number => getCounter(number)?.start())
)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Grouping, decimals and the text around the number</title>
<style>
.formats {
display: grid;
gap: 1rem;
justify-items: start;
}
.formats dl {
display: grid;
gap: 0.5rem;
margin: 0;
}
.formats dl div {
display: grid;
grid-template-columns: minmax(12rem, 1fr) auto;
gap: 1rem;
align-items: baseline;
}
.formats dt {
font:
0.875rem/1.4 ui-monospace,
monospace;
}
.formats dd {
margin: 0;
font-size: 1.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
</style>
</head>
<body>
<main>
<div class="formats">
<dl lang="en">
<div>
<dt>As written: no separators</dt>
<dd><span data-counter>8000</span></dd>
</div>
<div>
<dt>As written: separators and decimals</dt>
<dd><span data-counter>8,000.45</span></dd>
</div>
<div>
<dt>grouping="true"</dt>
<dd><span data-counter data-counter-grouping="true">8000</span></dd>
</div>
<div>
<dt>grouping="true" keeps the $</dt>
<dd><span data-counter data-counter-grouping="true">$8000</span></dd>
</div>
<div>
<dt>grouping="true" decimals="2"</dt>
<dd>
<span data-counter data-counter-grouping="true" data-counter-decimals="2">8000</span>
</dd>
</div>
<div>
<dt>grouping="false"</dt>
<dd><span data-counter data-counter-grouping="false">8,000</span></dd>
</div>
<div>
<dt>grouping="true" lang="de"</dt>
<dd><span data-counter data-counter-grouping="true" lang="de">8000,5</span></dd>
</div>
<div>
<dt>format: compact, suffix kept</dt>
<dd><span id="compact">8000 visits</span></dd>
</div>
<div>
<dt>format: currency + grouping: true</dt>
<dd><span id="usd">8000</span></dd>
</div>
</dl>
<button type="button" id="again">Count again</button>
</div>
</main>
<script type="module">
import { autoCounter, counter, getCounter } from '@samline/counter'
autoCounter()
counter('#compact', { format: { notation: 'compact' } })
counter('#usd', { grouping: true, format: { style: 'currency', currency: 'USD' } })
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.formats dd span')
.forEach(number => getCounter(number)?.start())
)
</script>
</body>
</html>
Integers past 2^53 that end on every digit
JavaScript numbers keep 15 to 17 digits, so a number like 18,446,744,073,709,551,615 would end as ...552,000. Final values past 2^53, from the attribute or the text, are kept as BigInt and written with Intl.NumberFormat, so the count ends on the exact digits, in any script.
- The largest 64-bit number
- 18,446,744,073,709,551,615
- 12.5 ether in wei
- 12500000000000000000
- सबसे बड़ी 64-बिट संख्या
- १,८४,४६,७४,४०,७३,७०,९५,५१,६१५
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Integers past 2^53 that end on every digit</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<dl class="big" lang="en">
<div>
<dt>The largest 64-bit number</dt>
<dd><span data-counter>18,446,744,073,709,551,615</span></dd>
</div>
<div>
<dt>12.5 ether in wei</dt>
<dd>
<span data-counter="12500000000000000000" data-counter-grouping="true"
>12500000000000000000</span
>
</dd>
</div>
<div lang="hi">
<dt>सबसे बड़ी 64-बिट संख्या</dt>
<dd><span data-counter>१,८४,४६,७४,४०,७३,७०,९५,५१,६१५</span></dd>
</div>
</dl>
<button type="button" id="again">Count again</button>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
.big {
display: grid;
gap: 1rem;
margin: 0 0 1rem;
}
.big dt {
font-size: 0.875rem;
}
.big dd {
margin: 0;
font-size: 1.5rem;
font-weight: 700;
font-variant-numeric: tabular-nums;
overflow-wrap: anywhere;
}
JavaScript
// Start now, so getCounter finds each number's controller.
Counter.auto()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.big [data-counter]')
.forEach(number => Counter.getCounter(number)?.start())
)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Integers past 2^53 that end on every digit</title>
<style>
.big {
display: grid;
gap: 1rem;
margin: 0 0 1rem;
}
.big dt {
font-size: 0.875rem;
}
.big dd {
margin: 0;
font-size: 1.5rem;
font-weight: 700;
font-variant-numeric: tabular-nums;
overflow-wrap: anywhere;
}
</style>
</head>
<body>
<main>
<dl class="big" lang="en">
<div>
<dt>The largest 64-bit number</dt>
<dd><span data-counter>18,446,744,073,709,551,615</span></dd>
</div>
<div>
<dt>12.5 ether in wei</dt>
<dd>
<span data-counter="12500000000000000000" data-counter-grouping="true"
>12500000000000000000</span
>
</dd>
</div>
<div lang="hi">
<dt>सबसे बड़ी 64-बिट संख्या</dt>
<dd><span data-counter>१,८४,४६,७४,४०,७३,७०,९५,५१,६१५</span></dd>
</div>
</dl>
<button type="button" id="again">Count again</button>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script>
// Start now, so getCounter finds each number's controller.
Counter.auto()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.big [data-counter]')
.forEach(number => Counter.getCounter(number)?.start())
)
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Integers past 2^53 that end on every digit</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<dl class="big" lang="en">
<div>
<dt>The largest 64-bit number</dt>
<dd><span data-counter>18,446,744,073,709,551,615</span></dd>
</div>
<div>
<dt>12.5 ether in wei</dt>
<dd>
<span data-counter="12500000000000000000" data-counter-grouping="true"
>12500000000000000000</span
>
</dd>
</div>
<div lang="hi">
<dt>सबसे बड़ी 64-बिट संख्या</dt>
<dd><span data-counter>१,८४,४६,७४,४०,७३,७०,९५,५१,६१५</span></dd>
</div>
</dl>
<button type="button" id="again">Count again</button>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
.big {
display: grid;
gap: 1rem;
margin: 0 0 1rem;
}
.big dt {
font-size: 0.875rem;
}
.big dd {
margin: 0;
font-size: 1.5rem;
font-weight: 700;
font-variant-numeric: tabular-nums;
overflow-wrap: anywhere;
}
JavaScript
import { autoCounter, getCounter } from '@samline/counter'
autoCounter()
document
.querySelector('#again')
.addEventListener('click', () =>
document.querySelectorAll('.big [data-counter]').forEach(number => getCounter(number)?.start())
)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Integers past 2^53 that end on every digit</title>
<style>
.big {
display: grid;
gap: 1rem;
margin: 0 0 1rem;
}
.big dt {
font-size: 0.875rem;
}
.big dd {
margin: 0;
font-size: 1.5rem;
font-weight: 700;
font-variant-numeric: tabular-nums;
overflow-wrap: anywhere;
}
</style>
</head>
<body>
<main>
<dl class="big" lang="en">
<div>
<dt>The largest 64-bit number</dt>
<dd><span data-counter>18,446,744,073,709,551,615</span></dd>
</div>
<div>
<dt>12.5 ether in wei</dt>
<dd>
<span data-counter="12500000000000000000" data-counter-grouping="true"
>12500000000000000000</span
>
</dd>
</div>
<div lang="hi">
<dt>सबसे बड़ी 64-बिट संख्या</dt>
<dd><span data-counter>१,८४,४६,७४,४०,७३,७०,९५,५१,६१५</span></dd>
</div>
</dl>
<button type="button" id="again">Count again</button>
</main>
<script type="module">
import { autoCounter, getCounter } from '@samline/counter'
autoCounter()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.big [data-counter]')
.forEach(number => getCounter(number)?.start())
)
</script>
</body>
</html>
Currencies, compact numbers and your own formats from HTML
data-counter-format takes words for Intl.NumberFormat, so plain HTML can count compact numbers, percentages, currencies and units. A format you name with defineFormat works the same way, here for file sizes.
- Downloads
- 1.5M
- Satisfied
- 86%
- Umsatz
- 1.234,50 €
- Top speed
- 120 km/h
- Stored
- 2.4 GB
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Currencies, compact numbers and your own formats from HTML</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<dl class="named" lang="en">
<div>
<dt>Downloads</dt>
<dd><span data-counter="1500000" data-counter-format="compact">1.5M</span></dd>
</div>
<div>
<dt>Satisfied</dt>
<dd><span data-counter="0.86" data-counter-format="percent">86%</span></dd>
</div>
<div lang="de">
<dt>Umsatz</dt>
<dd><span data-counter="1234.50" data-counter-format="currency:EUR">1.234,50 €</span></dd>
</div>
<div>
<dt>Top speed</dt>
<dd>
<span data-counter="120" data-counter-format="unit:kilometer-per-hour">120 km/h</span>
</dd>
</div>
<div>
<dt>Stored</dt>
<dd><span data-counter="2400000000" data-counter-format="bytes">2.4 GB</span></dd>
</div>
</dl>
<button type="button" id="again">Count again</button>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
.named {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr));
gap: 1rem;
margin: 0 0 1rem;
}
.named dt {
font-size: 0.875rem;
}
.named dd {
margin: 0;
font-size: 1.75rem;
font-weight: 700;
font-variant-numeric: tabular-nums;
}
JavaScript
const units = ['B', 'KB', 'MB', 'GB', 'TB']
const bytes = value => {
let unit = 0
while (value >= 1000 && unit < units.length - 1) {
value /= 1000
unit++
}
return `${value.toFixed(unit ? 1 : 0)} ${units[unit]}`
}
// Name the format before counting starts.
Counter.defineFormat('bytes', bytes)
Counter.auto()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.named [data-counter]')
.forEach(number => Counter.getCounter(number)?.start())
)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Currencies, compact numbers and your own formats from HTML</title>
<style>
.named {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr));
gap: 1rem;
margin: 0 0 1rem;
}
.named dt {
font-size: 0.875rem;
}
.named dd {
margin: 0;
font-size: 1.75rem;
font-weight: 700;
font-variant-numeric: tabular-nums;
}
</style>
</head>
<body>
<main>
<dl class="named" lang="en">
<div>
<dt>Downloads</dt>
<dd><span data-counter="1500000" data-counter-format="compact">1.5M</span></dd>
</div>
<div>
<dt>Satisfied</dt>
<dd><span data-counter="0.86" data-counter-format="percent">86%</span></dd>
</div>
<div lang="de">
<dt>Umsatz</dt>
<dd><span data-counter="1234.50" data-counter-format="currency:EUR">1.234,50 €</span></dd>
</div>
<div>
<dt>Top speed</dt>
<dd>
<span data-counter="120" data-counter-format="unit:kilometer-per-hour">120 km/h</span>
</dd>
</div>
<div>
<dt>Stored</dt>
<dd><span data-counter="2400000000" data-counter-format="bytes">2.4 GB</span></dd>
</div>
</dl>
<button type="button" id="again">Count again</button>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script>
const units = ['B', 'KB', 'MB', 'GB', 'TB']
const bytes = value => {
let unit = 0
while (value >= 1000 && unit < units.length - 1) {
value /= 1000
unit++
}
return `${value.toFixed(unit ? 1 : 0)} ${units[unit]}`
}
// Name the format before counting starts.
Counter.defineFormat('bytes', bytes)
Counter.auto()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.named [data-counter]')
.forEach(number => Counter.getCounter(number)?.start())
)
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Currencies, compact numbers and your own formats from HTML</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<dl class="named" lang="en">
<div>
<dt>Downloads</dt>
<dd><span data-counter="1500000" data-counter-format="compact">1.5M</span></dd>
</div>
<div>
<dt>Satisfied</dt>
<dd><span data-counter="0.86" data-counter-format="percent">86%</span></dd>
</div>
<div lang="de">
<dt>Umsatz</dt>
<dd><span data-counter="1234.50" data-counter-format="currency:EUR">1.234,50 €</span></dd>
</div>
<div>
<dt>Top speed</dt>
<dd>
<span data-counter="120" data-counter-format="unit:kilometer-per-hour">120 km/h</span>
</dd>
</div>
<div>
<dt>Stored</dt>
<dd><span data-counter="2400000000" data-counter-format="bytes">2.4 GB</span></dd>
</div>
</dl>
<button type="button" id="again">Count again</button>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
.named {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr));
gap: 1rem;
margin: 0 0 1rem;
}
.named dt {
font-size: 0.875rem;
}
.named dd {
margin: 0;
font-size: 1.75rem;
font-weight: 700;
font-variant-numeric: tabular-nums;
}
JavaScript
import { autoCounter, defineFormat, getCounter } from '@samline/counter'
const units = ['B', 'KB', 'MB', 'GB', 'TB']
const bytes = value => {
let unit = 0
while (value >= 1000 && unit < units.length - 1) {
value /= 1000
unit++
}
return `${value.toFixed(unit ? 1 : 0)} ${units[unit]}`
}
defineFormat('bytes', bytes)
autoCounter()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.named [data-counter]')
.forEach(number => getCounter(number)?.start())
)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Currencies, compact numbers and your own formats from HTML</title>
<style>
.named {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr));
gap: 1rem;
margin: 0 0 1rem;
}
.named dt {
font-size: 0.875rem;
}
.named dd {
margin: 0;
font-size: 1.75rem;
font-weight: 700;
font-variant-numeric: tabular-nums;
}
</style>
</head>
<body>
<main>
<dl class="named" lang="en">
<div>
<dt>Downloads</dt>
<dd><span data-counter="1500000" data-counter-format="compact">1.5M</span></dd>
</div>
<div>
<dt>Satisfied</dt>
<dd><span data-counter="0.86" data-counter-format="percent">86%</span></dd>
</div>
<div lang="de">
<dt>Umsatz</dt>
<dd><span data-counter="1234.50" data-counter-format="currency:EUR">1.234,50 €</span></dd>
</div>
<div>
<dt>Top speed</dt>
<dd>
<span data-counter="120" data-counter-format="unit:kilometer-per-hour">120 km/h</span>
</dd>
</div>
<div>
<dt>Stored</dt>
<dd><span data-counter="2400000000" data-counter-format="bytes">2.4 GB</span></dd>
</div>
</dl>
<button type="button" id="again">Count again</button>
</main>
<script type="module">
import { autoCounter, defineFormat, getCounter } from '@samline/counter'
const units = ['B', 'KB', 'MB', 'GB', 'TB']
const bytes = value => {
let unit = 0
while (value >= 1000 && unit < units.length - 1) {
value /= 1000
unit++
}
return `${value.toFixed(unit ? 1 : 0)} ${units[unit]}`
}
defineFormat('bytes', bytes)
autoCounter()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.named [data-counter]')
.forEach(number => getCounter(number)?.start())
)
</script>
</body>
</html>
Ordinals that stay right while they count
A suffix of English or French that matches the final value is recognized, so the rank climbs from 340th to 21st through 22nd and 23rd, never 22st, and 2e counts from 1er. Other languages set their suffixes with data-counter-ordinal, by plural category.
- Ranked 21st of 340 teams
- Our 103rd release
- 2e édition
- 5.ª temporada
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Ordinals that stay right while they count</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<ul class="ranks">
<li lang="en">
Ranked <span data-counter data-counter-from="340">21st</span> of 340 teams
</li>
<li lang="en">Our <span data-counter data-counter-from="1">103rd</span> release</li>
<li lang="fr"><span data-counter data-counter-from="1">2e</span> édition</li>
<li lang="es">
<span data-counter="5" data-counter-from="1" data-counter-ordinal="other=.ª">5.ª</span>
temporada
</li>
</ul>
<button type="button" id="again">Count again</button>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
.ranks {
display: grid;
gap: 0.5rem;
margin: 0 0 1rem;
padding: 0;
list-style: none;
font-size: 1.5rem;
}
.ranks span {
font-weight: 700;
font-variant-numeric: tabular-nums;
}
JavaScript
// Start now, so getCounter finds each number's controller.
Counter.auto()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.ranks [data-counter]')
.forEach(number => Counter.getCounter(number)?.start())
)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Ordinals that stay right while they count</title>
<style>
.ranks {
display: grid;
gap: 0.5rem;
margin: 0 0 1rem;
padding: 0;
list-style: none;
font-size: 1.5rem;
}
.ranks span {
font-weight: 700;
font-variant-numeric: tabular-nums;
}
</style>
</head>
<body>
<main>
<ul class="ranks">
<li lang="en">
Ranked <span data-counter data-counter-from="340">21st</span> of 340 teams
</li>
<li lang="en">Our <span data-counter data-counter-from="1">103rd</span> release</li>
<li lang="fr"><span data-counter data-counter-from="1">2e</span> édition</li>
<li lang="es">
<span data-counter="5" data-counter-from="1" data-counter-ordinal="other=.ª">5.ª</span>
temporada
</li>
</ul>
<button type="button" id="again">Count again</button>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script>
// Start now, so getCounter finds each number's controller.
Counter.auto()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.ranks [data-counter]')
.forEach(number => Counter.getCounter(number)?.start())
)
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Ordinals that stay right while they count</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<ul class="ranks">
<li lang="en">
Ranked <span data-counter data-counter-from="340">21st</span> of 340 teams
</li>
<li lang="en">Our <span data-counter data-counter-from="1">103rd</span> release</li>
<li lang="fr"><span data-counter data-counter-from="1">2e</span> édition</li>
<li lang="es">
<span data-counter="5" data-counter-from="1" data-counter-ordinal="other=.ª">5.ª</span>
temporada
</li>
</ul>
<button type="button" id="again">Count again</button>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
.ranks {
display: grid;
gap: 0.5rem;
margin: 0 0 1rem;
padding: 0;
list-style: none;
font-size: 1.5rem;
}
.ranks span {
font-weight: 700;
font-variant-numeric: tabular-nums;
}
JavaScript
import { autoCounter, getCounter } from '@samline/counter'
autoCounter()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.ranks [data-counter]')
.forEach(number => getCounter(number)?.start())
)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Ordinals that stay right while they count</title>
<style>
.ranks {
display: grid;
gap: 0.5rem;
margin: 0 0 1rem;
padding: 0;
list-style: none;
font-size: 1.5rem;
}
.ranks span {
font-weight: 700;
font-variant-numeric: tabular-nums;
}
</style>
</head>
<body>
<main>
<ul class="ranks">
<li lang="en">
Ranked <span data-counter data-counter-from="340">21st</span> of 340 teams
</li>
<li lang="en">Our <span data-counter data-counter-from="1">103rd</span> release</li>
<li lang="fr"><span data-counter data-counter-from="1">2e</span> édition</li>
<li lang="es">
<span data-counter="5" data-counter-from="1" data-counter-ordinal="other=.ª">5.ª</span>
temporada
</li>
</ul>
<button type="button" id="again">Count again</button>
</main>
<script type="module">
import { autoCounter, getCounter } from '@samline/counter'
autoCounter()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.ranks [data-counter]')
.forEach(number => getCounter(number)?.start())
)
</script>
</body>
</html>
Numbers that follow the scroll
With data-counter-start="scroll" the value follows the scroll position instead of time: it starts when the number enters at the bottom and lands when it reaches the middle, and goes back when you scroll back. It works with rolling digits and with --counter-progress for a bar.
Scroll down slowly, then back up
8,000,000,000 people
71% of the surface is water
The end
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Numbers that follow the scroll</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div id="story" lang="en">
<p class="spacer">Scroll down slowly, then back up</p>
<p class="fact">
<span data-counter="8000000000" data-counter-start="scroll" data-counter-roll
>8,000,000,000</span
>
people
</p>
<p class="fact">
<span data-counter="71" data-counter-start="scroll" data-counter-easing="linear">71</span
>% of the surface is water
</p>
<div class="fact" data-water>
<span
data-counter="384400"
data-counter-start="scroll"
data-counter-css-vars="[data-water]"
>384,400</span
>
km to the Moon
<i class="bar"></i>
</div>
<p class="spacer">The end</p>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
#story {
height: 18rem;
overflow-y: auto;
padding: 0 1rem;
border: 1px solid #ccc;
}
.spacer {
min-height: 16rem;
margin: 0;
display: grid;
place-items: center;
font-size: 0.875rem;
}
.fact {
margin: 0 0 8rem;
font-size: 1.5rem;
}
.fact span {
font-weight: 700;
font-variant-numeric: tabular-nums;
}
[data-water] {
--counter-progress: 1;
}
.bar {
display: block;
height: 0.5rem;
margin-top: 0.5rem;
width: calc(var(--counter-progress) * 100%);
border-radius: 1rem;
background: #c2410c;
}
JavaScript
// The box scrolls instead of the page, so pass it as the root.
// Called before the DOM is ready, this replaces the automatic start.
Counter.auto({ root: document.querySelector('#story') })
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Numbers that follow the scroll</title>
<style>
#story {
height: 18rem;
overflow-y: auto;
padding: 0 1rem;
border: 1px solid #ccc;
}
.spacer {
min-height: 16rem;
margin: 0;
display: grid;
place-items: center;
font-size: 0.875rem;
}
.fact {
margin: 0 0 8rem;
font-size: 1.5rem;
}
.fact span {
font-weight: 700;
font-variant-numeric: tabular-nums;
}
[data-water] {
--counter-progress: 1;
}
.bar {
display: block;
height: 0.5rem;
margin-top: 0.5rem;
width: calc(var(--counter-progress) * 100%);
border-radius: 1rem;
background: #c2410c;
}
</style>
</head>
<body>
<main>
<div id="story" lang="en">
<p class="spacer">Scroll down slowly, then back up</p>
<p class="fact">
<span data-counter="8000000000" data-counter-start="scroll" data-counter-roll
>8,000,000,000</span
>
people
</p>
<p class="fact">
<span data-counter="71" data-counter-start="scroll" data-counter-easing="linear">71</span
>% of the surface is water
</p>
<div class="fact" data-water>
<span
data-counter="384400"
data-counter-start="scroll"
data-counter-css-vars="[data-water]"
>384,400</span
>
km to the Moon
<i class="bar"></i>
</div>
<p class="spacer">The end</p>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script>
// The box scrolls instead of the page, so pass it as the root.
// Called before the DOM is ready, this replaces the automatic start.
Counter.auto({ root: document.querySelector('#story') })
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Numbers that follow the scroll</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div id="story" lang="en">
<p class="spacer">Scroll down slowly, then back up</p>
<p class="fact">
<span data-counter="8000000000" data-counter-start="scroll" data-counter-roll
>8,000,000,000</span
>
people
</p>
<p class="fact">
<span data-counter="71" data-counter-start="scroll" data-counter-easing="linear">71</span
>% of the surface is water
</p>
<div class="fact" data-water>
<span
data-counter="384400"
data-counter-start="scroll"
data-counter-css-vars="[data-water]"
>384,400</span
>
km to the Moon
<i class="bar"></i>
</div>
<p class="spacer">The end</p>
</div>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
#story {
height: 18rem;
overflow-y: auto;
padding: 0 1rem;
border: 1px solid #ccc;
}
.spacer {
min-height: 16rem;
margin: 0;
display: grid;
place-items: center;
font-size: 0.875rem;
}
.fact {
margin: 0 0 8rem;
font-size: 1.5rem;
}
.fact span {
font-weight: 700;
font-variant-numeric: tabular-nums;
}
[data-water] {
--counter-progress: 1;
}
.bar {
display: block;
height: 0.5rem;
margin-top: 0.5rem;
width: calc(var(--counter-progress) * 100%);
border-radius: 1rem;
background: #c2410c;
}
JavaScript
import { autoCounter } from '@samline/counter'
// The box scrolls instead of the page, so pass it as the root.
autoCounter({ root: document.querySelector('#story') })
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Numbers that follow the scroll</title>
<style>
#story {
height: 18rem;
overflow-y: auto;
padding: 0 1rem;
border: 1px solid #ccc;
}
.spacer {
min-height: 16rem;
margin: 0;
display: grid;
place-items: center;
font-size: 0.875rem;
}
.fact {
margin: 0 0 8rem;
font-size: 1.5rem;
}
.fact span {
font-weight: 700;
font-variant-numeric: tabular-nums;
}
[data-water] {
--counter-progress: 1;
}
.bar {
display: block;
height: 0.5rem;
margin-top: 0.5rem;
width: calc(var(--counter-progress) * 100%);
border-radius: 1rem;
background: #c2410c;
}
</style>
</head>
<body>
<main>
<div id="story" lang="en">
<p class="spacer">Scroll down slowly, then back up</p>
<p class="fact">
<span data-counter="8000000000" data-counter-start="scroll" data-counter-roll
>8,000,000,000</span
>
people
</p>
<p class="fact">
<span data-counter="71" data-counter-start="scroll" data-counter-easing="linear">71</span
>% of the surface is water
</p>
<div class="fact" data-water>
<span
data-counter="384400"
data-counter-start="scroll"
data-counter-css-vars="[data-water]"
>384,400</span
>
km to the Moon
<i class="bar"></i>
</div>
<p class="spacer">The end</p>
</div>
</main>
<script type="module">
import { autoCounter } from '@samline/counter'
// The box scrolls instead of the page, so pass it as the root.
autoCounter({ root: document.querySelector('#story') })
</script>
</body>
</html>
Digits that roll like an odometer
data-counter-roll turns each digit into a column that rolls to the value: the last digit spins fastest, and every column shows the real digit of its place. Prefixes, separators, decimals and Arabic-Indic digits stay in place, and the count ends on the original text. Here the score also keeps its width with data-counter-steady.
- Revenue
- $1,250,000
- Uptime
- 99.95%
- الزوار
- ١٬٢٣٤
120 points
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Digits that roll like an odometer</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="roll" lang="en">
<dl class="board">
<div>
<dt>Revenue</dt>
<dd><span data-counter="1250000" data-counter-roll>$1,250,000</span></dd>
</div>
<div>
<dt>Uptime</dt>
<dd><span data-counter="99.95" data-counter-roll>99.95%</span></dd>
</div>
<div lang="ar" dir="rtl">
<dt>الزوار</dt>
<dd><span data-counter data-counter-roll>١٬٢٣٤</span></dd>
</div>
</dl>
<p class="score">
<span
id="score"
data-counter="120"
data-counter-roll
data-counter-steady
data-counter-duration="800"
>120</span
>
points
</p>
<p class="actions">
<button type="button" id="add">Add 75</button>
<button type="button" id="take">Take 40</button>
</p>
</div>
<button type="button" id="again">Count again</button>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
.roll {
display: grid;
gap: 1rem;
}
.board {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
gap: 1rem;
margin: 0;
}
.board dt {
font-size: 0.875rem;
}
.board dd {
margin: 0;
font-size: 2rem;
font-weight: 700;
}
.score {
margin: 0;
font-size: 1.5rem;
}
.score span {
font-weight: 700;
}
.actions {
display: flex;
gap: 0.5rem;
margin: 0;
}
JavaScript
// Start now, so getCounter finds each number's controller.
Counter.auto()
const score = Counter.getCounter(document.querySelector('#score'))
document.querySelector('#add').addEventListener('click', () => score.increment(75))
document.querySelector('#take').addEventListener('click', () => score.decrement(40))
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.board [data-counter]')
.forEach(number => Counter.getCounter(number)?.start())
)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Digits that roll like an odometer</title>
<style>
.roll {
display: grid;
gap: 1rem;
}
.board {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
gap: 1rem;
margin: 0;
}
.board dt {
font-size: 0.875rem;
}
.board dd {
margin: 0;
font-size: 2rem;
font-weight: 700;
}
.score {
margin: 0;
font-size: 1.5rem;
}
.score span {
font-weight: 700;
}
.actions {
display: flex;
gap: 0.5rem;
margin: 0;
}
</style>
</head>
<body>
<main>
<div class="roll" lang="en">
<dl class="board">
<div>
<dt>Revenue</dt>
<dd><span data-counter="1250000" data-counter-roll>$1,250,000</span></dd>
</div>
<div>
<dt>Uptime</dt>
<dd><span data-counter="99.95" data-counter-roll>99.95%</span></dd>
</div>
<div lang="ar" dir="rtl">
<dt>الزوار</dt>
<dd><span data-counter data-counter-roll>١٬٢٣٤</span></dd>
</div>
</dl>
<p class="score">
<span
id="score"
data-counter="120"
data-counter-roll
data-counter-steady
data-counter-duration="800"
>120</span
>
points
</p>
<p class="actions">
<button type="button" id="add">Add 75</button>
<button type="button" id="take">Take 40</button>
</p>
</div>
<button type="button" id="again">Count again</button>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script>
// Start now, so getCounter finds each number's controller.
Counter.auto()
const score = Counter.getCounter(document.querySelector('#score'))
document.querySelector('#add').addEventListener('click', () => score.increment(75))
document.querySelector('#take').addEventListener('click', () => score.decrement(40))
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.board [data-counter]')
.forEach(number => Counter.getCounter(number)?.start())
)
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Digits that roll like an odometer</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="roll" lang="en">
<dl class="board">
<div>
<dt>Revenue</dt>
<dd><span data-counter="1250000" data-counter-roll>$1,250,000</span></dd>
</div>
<div>
<dt>Uptime</dt>
<dd><span data-counter="99.95" data-counter-roll>99.95%</span></dd>
</div>
<div lang="ar" dir="rtl">
<dt>الزوار</dt>
<dd><span data-counter data-counter-roll>١٬٢٣٤</span></dd>
</div>
</dl>
<p class="score">
<span
id="score"
data-counter="120"
data-counter-roll
data-counter-steady
data-counter-duration="800"
>120</span
>
points
</p>
<p class="actions">
<button type="button" id="add">Add 75</button>
<button type="button" id="take">Take 40</button>
</p>
</div>
<button type="button" id="again">Count again</button>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
.roll {
display: grid;
gap: 1rem;
}
.board {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
gap: 1rem;
margin: 0;
}
.board dt {
font-size: 0.875rem;
}
.board dd {
margin: 0;
font-size: 2rem;
font-weight: 700;
}
.score {
margin: 0;
font-size: 1.5rem;
}
.score span {
font-weight: 700;
}
.actions {
display: flex;
gap: 0.5rem;
margin: 0;
}
JavaScript
import { autoCounter, getCounter } from '@samline/counter'
autoCounter()
const score = getCounter(document.querySelector('#score'))
document.querySelector('#add').addEventListener('click', () => score.increment(75))
document.querySelector('#take').addEventListener('click', () => score.decrement(40))
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.board [data-counter]')
.forEach(number => getCounter(number)?.start())
)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Digits that roll like an odometer</title>
<style>
.roll {
display: grid;
gap: 1rem;
}
.board {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
gap: 1rem;
margin: 0;
}
.board dt {
font-size: 0.875rem;
}
.board dd {
margin: 0;
font-size: 2rem;
font-weight: 700;
}
.score {
margin: 0;
font-size: 1.5rem;
}
.score span {
font-weight: 700;
}
.actions {
display: flex;
gap: 0.5rem;
margin: 0;
}
</style>
</head>
<body>
<main>
<div class="roll" lang="en">
<dl class="board">
<div>
<dt>Revenue</dt>
<dd><span data-counter="1250000" data-counter-roll>$1,250,000</span></dd>
</div>
<div>
<dt>Uptime</dt>
<dd><span data-counter="99.95" data-counter-roll>99.95%</span></dd>
</div>
<div lang="ar" dir="rtl">
<dt>الزوار</dt>
<dd><span data-counter data-counter-roll>١٬٢٣٤</span></dd>
</div>
</dl>
<p class="score">
<span
id="score"
data-counter="120"
data-counter-roll
data-counter-steady
data-counter-duration="800"
>120</span
>
points
</p>
<p class="actions">
<button type="button" id="add">Add 75</button>
<button type="button" id="take">Take 40</button>
</p>
</div>
<button type="button" id="again">Count again</button>
</main>
<script type="module">
import { autoCounter, getCounter } from '@samline/counter'
autoCounter()
const score = getCounter(document.querySelector('#score'))
document.querySelector('#add').addEventListener('click', () => score.increment(75))
document.querySelector('#take').addEventListener('click', () => score.decrement(40))
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.board [data-counter]')
.forEach(number => getCounter(number)?.start())
)
</script>
</body>
</html>
Digits that settle like a code being cracked
data-counter-scramble shows the final text with random digits while it counts, and settles them from left to right. It keeps the prefix, separators and suffix, works with clocks, and ends on the original text. data-counter-steady holds the width, so nothing jumps when the random digits appear.
- Order
- #20481736
- Revenue
- $1,250,000
- Flight time
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Digits that settle like a code being cracked</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<dl class="codes" lang="en">
<div>
<dt>Order</dt>
<dd>
<span
data-counter="20481736"
data-counter-grouping="false"
data-counter-scramble
data-counter-steady
>#20481736</span
>
</dd>
</div>
<div>
<dt>Revenue</dt>
<dd>
<span data-counter="1250000" data-counter-scramble data-counter-steady>$1,250,000</span>
</dd>
</div>
<div>
<dt>Flight time</dt>
<dd>
<time data-counter data-counter-as="time" data-counter-scramble data-counter-steady
>02:45:30</time
>
</dd>
</div>
</dl>
<button type="button" id="again">Decode again</button>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
.codes {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
gap: 1rem;
margin: 0 0 1rem;
}
.codes dt {
font-size: 0.875rem;
}
.codes dd {
margin: 0;
font-size: 2rem;
font-weight: 700;
}
JavaScript
// Start now, so getCounter finds each number's controller.
Counter.auto()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.codes [data-counter]')
.forEach(number => Counter.getCounter(number)?.start())
)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Digits that settle like a code being cracked</title>
<style>
.codes {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
gap: 1rem;
margin: 0 0 1rem;
}
.codes dt {
font-size: 0.875rem;
}
.codes dd {
margin: 0;
font-size: 2rem;
font-weight: 700;
}
</style>
</head>
<body>
<main>
<dl class="codes" lang="en">
<div>
<dt>Order</dt>
<dd>
<span
data-counter="20481736"
data-counter-grouping="false"
data-counter-scramble
data-counter-steady
>#20481736</span
>
</dd>
</div>
<div>
<dt>Revenue</dt>
<dd>
<span data-counter="1250000" data-counter-scramble data-counter-steady>$1,250,000</span>
</dd>
</div>
<div>
<dt>Flight time</dt>
<dd>
<time data-counter data-counter-as="time" data-counter-scramble data-counter-steady
>02:45:30</time
>
</dd>
</div>
</dl>
<button type="button" id="again">Decode again</button>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script>
// Start now, so getCounter finds each number's controller.
Counter.auto()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.codes [data-counter]')
.forEach(number => Counter.getCounter(number)?.start())
)
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Digits that settle like a code being cracked</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<dl class="codes" lang="en">
<div>
<dt>Order</dt>
<dd>
<span
data-counter="20481736"
data-counter-grouping="false"
data-counter-scramble
data-counter-steady
>#20481736</span
>
</dd>
</div>
<div>
<dt>Revenue</dt>
<dd>
<span data-counter="1250000" data-counter-scramble data-counter-steady>$1,250,000</span>
</dd>
</div>
<div>
<dt>Flight time</dt>
<dd>
<time data-counter data-counter-as="time" data-counter-scramble data-counter-steady
>02:45:30</time
>
</dd>
</div>
</dl>
<button type="button" id="again">Decode again</button>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
.codes {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
gap: 1rem;
margin: 0 0 1rem;
}
.codes dt {
font-size: 0.875rem;
}
.codes dd {
margin: 0;
font-size: 2rem;
font-weight: 700;
}
JavaScript
import { autoCounter, getCounter } from '@samline/counter'
autoCounter()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.codes [data-counter]')
.forEach(number => getCounter(number)?.start())
)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Digits that settle like a code being cracked</title>
<style>
.codes {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
gap: 1rem;
margin: 0 0 1rem;
}
.codes dt {
font-size: 0.875rem;
}
.codes dd {
margin: 0;
font-size: 2rem;
font-weight: 700;
}
</style>
</head>
<body>
<main>
<dl class="codes" lang="en">
<div>
<dt>Order</dt>
<dd>
<span
data-counter="20481736"
data-counter-grouping="false"
data-counter-scramble
data-counter-steady
>#20481736</span
>
</dd>
</div>
<div>
<dt>Revenue</dt>
<dd>
<span data-counter="1250000" data-counter-scramble data-counter-steady>$1,250,000</span>
</dd>
</div>
<div>
<dt>Flight time</dt>
<dd>
<time data-counter data-counter-as="time" data-counter-scramble data-counter-steady
>02:45:30</time
>
</dd>
</div>
</dl>
<button type="button" id="again">Decode again</button>
</main>
<script type="module">
import { autoCounter, getCounter } from '@samline/counter'
autoCounter()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.codes [data-counter]')
.forEach(number => getCounter(number)?.start())
)
</script>
</body>
</html>
A still layout and digits in any script
data-counter-steady gives the digits equal widths and reserves the width of the final text, so the words after a growing number stay where they are. Numbers written in Arabic-Indic, Devanagari or other digits count in those digits, and format() writes any value the way the counter shows it.
Without steady: the words after the number move
1,250,000 downloads this year
With data-counter-steady: the line keeps still
1,250,000 downloads this year
Digits in other scripts count in that script
١٬٢٥٠٬٠٠٠ تنزيل هذا العام
१२,५०,००० डाउनलोड इस साल
format(): the same text anywhere on the page
Next goal:
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>A still layout and digits in any script</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="steady">
<h3>Without steady: the words after the number move</h3>
<p class="line" lang="en"><span data-counter>1,250,000</span> downloads this year</p>
<h3>With data-counter-steady: the line keeps still</h3>
<p class="line" lang="en">
<span id="downloads" data-counter data-counter-steady>1,250,000</span> downloads this year
</p>
<h3>Digits in other scripts count in that script</h3>
<p class="line" lang="ar" dir="rtl">
<span data-counter data-counter-steady>١٬٢٥٠٬٠٠٠</span> تنزيل هذا العام
</p>
<p class="line" lang="hi">
<span data-counter data-counter-steady>१२,५०,०००</span> डाउनलोड इस साल
</p>
<h3>format(): the same text anywhere on the page</h3>
<p class="line" lang="en">Next goal: <output id="goal"></output></p>
</div>
<button type="button" id="again">Count again</button>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
.steady h3 {
margin: 1rem 0 0.25rem;
font-size: 0.875rem;
}
.steady .line {
margin: 0;
font-size: 1.5rem;
}
.steady [data-counter] {
font-weight: 600;
}
JavaScript
// Start now, so getCounter finds each number's controller.
Counter.auto()
// The goal is written like the number it belongs to: grouped, in its language.
const downloads = Counter.getCounter(document.querySelector('#downloads'))
document.querySelector('#goal').textContent = downloads.format(2000000)
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.steady [data-counter]')
.forEach(number => Counter.getCounter(number)?.start())
)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>A still layout and digits in any script</title>
<style>
.steady h3 {
margin: 1rem 0 0.25rem;
font-size: 0.875rem;
}
.steady .line {
margin: 0;
font-size: 1.5rem;
}
.steady [data-counter] {
font-weight: 600;
}
</style>
</head>
<body>
<main>
<div class="steady">
<h3>Without steady: the words after the number move</h3>
<p class="line" lang="en"><span data-counter>1,250,000</span> downloads this year</p>
<h3>With data-counter-steady: the line keeps still</h3>
<p class="line" lang="en">
<span id="downloads" data-counter data-counter-steady>1,250,000</span> downloads this year
</p>
<h3>Digits in other scripts count in that script</h3>
<p class="line" lang="ar" dir="rtl">
<span data-counter data-counter-steady>١٬٢٥٠٬٠٠٠</span> تنزيل هذا العام
</p>
<p class="line" lang="hi">
<span data-counter data-counter-steady>१२,५०,०००</span> डाउनलोड इस साल
</p>
<h3>format(): the same text anywhere on the page</h3>
<p class="line" lang="en">Next goal: <output id="goal"></output></p>
</div>
<button type="button" id="again">Count again</button>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script>
// Start now, so getCounter finds each number's controller.
Counter.auto()
// The goal is written like the number it belongs to: grouped, in its language.
const downloads = Counter.getCounter(document.querySelector('#downloads'))
document.querySelector('#goal').textContent = downloads.format(2000000)
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.steady [data-counter]')
.forEach(number => Counter.getCounter(number)?.start())
)
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>A still layout and digits in any script</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="steady">
<h3>Without steady: the words after the number move</h3>
<p class="line" lang="en"><span data-counter>1,250,000</span> downloads this year</p>
<h3>With data-counter-steady: the line keeps still</h3>
<p class="line" lang="en">
<span id="downloads" data-counter data-counter-steady>1,250,000</span> downloads this year
</p>
<h3>Digits in other scripts count in that script</h3>
<p class="line" lang="ar" dir="rtl">
<span data-counter data-counter-steady>١٬٢٥٠٬٠٠٠</span> تنزيل هذا العام
</p>
<p class="line" lang="hi">
<span data-counter data-counter-steady>१२,५०,०००</span> डाउनलोड इस साल
</p>
<h3>format(): the same text anywhere on the page</h3>
<p class="line" lang="en">Next goal: <output id="goal"></output></p>
</div>
<button type="button" id="again">Count again</button>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
.steady h3 {
margin: 1rem 0 0.25rem;
font-size: 0.875rem;
}
.steady .line {
margin: 0;
font-size: 1.5rem;
}
.steady [data-counter] {
font-weight: 600;
}
JavaScript
import { autoCounter, getCounter } from '@samline/counter'
autoCounter()
// The goal is written like the number it belongs to: grouped, in its language.
const downloads = getCounter(document.querySelector('#downloads'))
document.querySelector('#goal').textContent = downloads.format(2000000)
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.steady [data-counter]')
.forEach(number => getCounter(number)?.start())
)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>A still layout and digits in any script</title>
<style>
.steady h3 {
margin: 1rem 0 0.25rem;
font-size: 0.875rem;
}
.steady .line {
margin: 0;
font-size: 1.5rem;
}
.steady [data-counter] {
font-weight: 600;
}
</style>
</head>
<body>
<main>
<div class="steady">
<h3>Without steady: the words after the number move</h3>
<p class="line" lang="en"><span data-counter>1,250,000</span> downloads this year</p>
<h3>With data-counter-steady: the line keeps still</h3>
<p class="line" lang="en">
<span id="downloads" data-counter data-counter-steady>1,250,000</span> downloads this year
</p>
<h3>Digits in other scripts count in that script</h3>
<p class="line" lang="ar" dir="rtl">
<span data-counter data-counter-steady>١٬٢٥٠٬٠٠٠</span> تنزيل هذا العام
</p>
<p class="line" lang="hi">
<span data-counter data-counter-steady>१२,५०,०००</span> डाउनलोड इस साल
</p>
<h3>format(): the same text anywhere on the page</h3>
<p class="line" lang="en">Next goal: <output id="goal"></output></p>
</div>
<button type="button" id="again">Count again</button>
</main>
<script type="module">
import { autoCounter, getCounter } from '@samline/counter'
autoCounter()
// The goal is written like the number it belongs to: grouped, in its language.
const downloads = getCounter(document.querySelector('#downloads'))
document.querySelector('#goal').textContent = downloads.format(2000000)
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.steady [data-counter]')
.forEach(number => getCounter(number)?.start())
)
</script>
</body>
</html>
Count down, and again on every visit
data-counter-from larger than the target counts down. data-counter-repeat resets the number when it leaves and counts again when it comes back.
Scroll down, then back up
0 seconds left
365 days a year
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Count down, and again on every visit</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div id="page">
<p class="spacer">Scroll down, then back up</p>
<p class="row">
<span
data-counter="0"
data-counter-from="10"
data-counter-repeat
data-counter-easing="linear"
>0</span
>
seconds left
</p>
<p class="row">
<span data-counter="365" data-counter-repeat data-counter-duration="1200">365</span> days
a year
</p>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
#page {
height: 16rem;
overflow-y: auto;
padding: 1rem;
border: 1px solid #ccc;
}
.spacer {
min-height: 20rem;
margin: 0;
}
.row span {
font-size: 2rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
JavaScript
Counter.auto({ root: document.querySelector('#page') })
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Count down, and again on every visit</title>
<style>
#page {
height: 16rem;
overflow-y: auto;
padding: 1rem;
border: 1px solid #ccc;
}
.spacer {
min-height: 20rem;
margin: 0;
}
.row span {
font-size: 2rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
</style>
</head>
<body>
<main>
<div id="page">
<p class="spacer">Scroll down, then back up</p>
<p class="row">
<span
data-counter="0"
data-counter-from="10"
data-counter-repeat
data-counter-easing="linear"
>0</span
>
seconds left
</p>
<p class="row">
<span data-counter="365" data-counter-repeat data-counter-duration="1200">365</span> days
a year
</p>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script>
Counter.auto({ root: document.querySelector('#page') })
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Count down, and again on every visit</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div id="page">
<p class="spacer">Scroll down, then back up</p>
<p class="row">
<span
data-counter="0"
data-counter-from="10"
data-counter-repeat
data-counter-easing="linear"
>0</span
>
seconds left
</p>
<p class="row">
<span data-counter="365" data-counter-repeat data-counter-duration="1200">365</span> days
a year
</p>
</div>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
#page {
height: 16rem;
overflow-y: auto;
padding: 1rem;
border: 1px solid #ccc;
}
.spacer {
min-height: 20rem;
margin: 0;
}
.row span {
font-size: 2rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
JavaScript
import { autoCounter, counter } from '@samline/counter'
autoCounter({ root: document.querySelector('#page') })
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Count down, and again on every visit</title>
<style>
#page {
height: 16rem;
overflow-y: auto;
padding: 1rem;
border: 1px solid #ccc;
}
.spacer {
min-height: 20rem;
margin: 0;
}
.row span {
font-size: 2rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
</style>
</head>
<body>
<main>
<div id="page">
<p class="spacer">Scroll down, then back up</p>
<p class="row">
<span
data-counter="0"
data-counter-from="10"
data-counter-repeat
data-counter-easing="linear"
>0</span
>
seconds left
</p>
<p class="row">
<span data-counter="365" data-counter-repeat data-counter-duration="1200">365</span> days
a year
</p>
</div>
</main>
<script type="module">
import { autoCounter, counter } from '@samline/counter'
autoCounter({ root: document.querySelector('#page') })
</script>
</body>
</html>
Clocks and dates that carry on their own
data-counter-as="time" counts seconds in the pattern of the text (23:59:59 or 90:00), so seconds carry into minutes and hours. data-counter-as="date" counts day by day up to the datetime, and months and years roll over on their own. data-counter-step shows whole steps, such as one week at a time. No JavaScript options.
- A day
- A match
- A work day
- A year
- Week by week
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Clocks and dates that carry on their own</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<dl class="clocks" lang="en">
<div>
<dt>A day</dt>
<dd>
<time
data-counter
data-counter-as="time"
data-counter-duration="6000"
data-counter-easing="linear"
>23:59:59</time
>
</dd>
</div>
<div>
<dt>A match</dt>
<dd>
<time
data-counter
data-counter-as="time"
data-counter-duration="6000"
data-counter-easing="linear"
>90:00</time
>
</dd>
</div>
<div>
<dt>A work day</dt>
<dd>
<time
data-counter
data-counter-as="time"
data-counter-from="08:00:00"
data-counter-duration="6000"
>17:30:00</time
>
</dd>
</div>
<div>
<dt>A year</dt>
<dd>
<time
data-counter
data-counter-as="date"
datetime="2026-12-31"
data-counter-duration="6000"
data-counter-easing="linear"
>December 31, 2026</time
>
</dd>
</div>
<div>
<dt>Week by week</dt>
<dd>
<time
data-counter
data-counter-as="date"
datetime="2026-12-31"
data-counter-step="7"
data-counter-date-style="medium"
data-counter-duration="6000"
>Dec 31, 2026</time
>
</dd>
</div>
</dl>
<button type="button" id="again">Count again</button>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
.clocks {
display: grid;
gap: 0.5rem;
margin: 0 0 1rem;
}
.clocks div {
display: grid;
grid-template-columns: 8rem auto;
gap: 1rem;
align-items: baseline;
}
.clocks dd {
margin: 0;
font-size: 1.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
JavaScript
// Every [data-counter] counts on its own; the button starts them again.
document
.querySelector('#again')
.addEventListener('click', () =>
document.querySelectorAll('.clocks time').forEach(time => Counter.getCounter(time)?.start())
)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Clocks and dates that carry on their own</title>
<style>
.clocks {
display: grid;
gap: 0.5rem;
margin: 0 0 1rem;
}
.clocks div {
display: grid;
grid-template-columns: 8rem auto;
gap: 1rem;
align-items: baseline;
}
.clocks dd {
margin: 0;
font-size: 1.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
</style>
</head>
<body>
<main>
<dl class="clocks" lang="en">
<div>
<dt>A day</dt>
<dd>
<time
data-counter
data-counter-as="time"
data-counter-duration="6000"
data-counter-easing="linear"
>23:59:59</time
>
</dd>
</div>
<div>
<dt>A match</dt>
<dd>
<time
data-counter
data-counter-as="time"
data-counter-duration="6000"
data-counter-easing="linear"
>90:00</time
>
</dd>
</div>
<div>
<dt>A work day</dt>
<dd>
<time
data-counter
data-counter-as="time"
data-counter-from="08:00:00"
data-counter-duration="6000"
>17:30:00</time
>
</dd>
</div>
<div>
<dt>A year</dt>
<dd>
<time
data-counter
data-counter-as="date"
datetime="2026-12-31"
data-counter-duration="6000"
data-counter-easing="linear"
>December 31, 2026</time
>
</dd>
</div>
<div>
<dt>Week by week</dt>
<dd>
<time
data-counter
data-counter-as="date"
datetime="2026-12-31"
data-counter-step="7"
data-counter-date-style="medium"
data-counter-duration="6000"
>Dec 31, 2026</time
>
</dd>
</div>
</dl>
<button type="button" id="again">Count again</button>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script>
// Every [data-counter] counts on its own; the button starts them again.
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.clocks time')
.forEach(time => Counter.getCounter(time)?.start())
)
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Clocks and dates that carry on their own</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<dl class="clocks" lang="en">
<div>
<dt>A day</dt>
<dd>
<time
data-counter
data-counter-as="time"
data-counter-duration="6000"
data-counter-easing="linear"
>23:59:59</time
>
</dd>
</div>
<div>
<dt>A match</dt>
<dd>
<time
data-counter
data-counter-as="time"
data-counter-duration="6000"
data-counter-easing="linear"
>90:00</time
>
</dd>
</div>
<div>
<dt>A work day</dt>
<dd>
<time
data-counter
data-counter-as="time"
data-counter-from="08:00:00"
data-counter-duration="6000"
>17:30:00</time
>
</dd>
</div>
<div>
<dt>A year</dt>
<dd>
<time
data-counter
data-counter-as="date"
datetime="2026-12-31"
data-counter-duration="6000"
data-counter-easing="linear"
>December 31, 2026</time
>
</dd>
</div>
<div>
<dt>Week by week</dt>
<dd>
<time
data-counter
data-counter-as="date"
datetime="2026-12-31"
data-counter-step="7"
data-counter-date-style="medium"
data-counter-duration="6000"
>Dec 31, 2026</time
>
</dd>
</div>
</dl>
<button type="button" id="again">Count again</button>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
.clocks {
display: grid;
gap: 0.5rem;
margin: 0 0 1rem;
}
.clocks div {
display: grid;
grid-template-columns: 8rem auto;
gap: 1rem;
align-items: baseline;
}
.clocks dd {
margin: 0;
font-size: 1.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
JavaScript
import { autoCounter, getCounter } from '@samline/counter'
autoCounter()
document
.querySelector('#again')
.addEventListener('click', () =>
document.querySelectorAll('.clocks time').forEach(time => getCounter(time)?.start())
)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Clocks and dates that carry on their own</title>
<style>
.clocks {
display: grid;
gap: 0.5rem;
margin: 0 0 1rem;
}
.clocks div {
display: grid;
grid-template-columns: 8rem auto;
gap: 1rem;
align-items: baseline;
}
.clocks dd {
margin: 0;
font-size: 1.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
</style>
</head>
<body>
<main>
<dl class="clocks" lang="en">
<div>
<dt>A day</dt>
<dd>
<time
data-counter
data-counter-as="time"
data-counter-duration="6000"
data-counter-easing="linear"
>23:59:59</time
>
</dd>
</div>
<div>
<dt>A match</dt>
<dd>
<time
data-counter
data-counter-as="time"
data-counter-duration="6000"
data-counter-easing="linear"
>90:00</time
>
</dd>
</div>
<div>
<dt>A work day</dt>
<dd>
<time
data-counter
data-counter-as="time"
data-counter-from="08:00:00"
data-counter-duration="6000"
>17:30:00</time
>
</dd>
</div>
<div>
<dt>A year</dt>
<dd>
<time
data-counter
data-counter-as="date"
datetime="2026-12-31"
data-counter-duration="6000"
data-counter-easing="linear"
>December 31, 2026</time
>
</dd>
</div>
<div>
<dt>Week by week</dt>
<dd>
<time
data-counter
data-counter-as="date"
datetime="2026-12-31"
data-counter-step="7"
data-counter-date-style="medium"
data-counter-duration="6000"
>Dec 31, 2026</time
>
</dd>
</div>
</dl>
<button type="button" id="again">Count again</button>
</main>
<script type="module">
import { autoCounter, getCounter } from '@samline/counter'
autoCounter()
document
.querySelector('#again')
.addEventListener('click', () =>
document.querySelectorAll('.clocks time').forEach(time => getCounter(time)?.start())
)
</script>
</body>
</html>
Easings, stagger and speed
Seven curves side by side, including ease-out-back and a CSS cubic-bezier. data-counter-stagger on a container starts its numbers one after another, and data-counter-speed counts a set amount per second, so 500 takes half a second and 4,000 takes four.
Easing
- linear
- 1000
- ease-in
- 1000
- ease-out
- 1000
- ease-in-out
- 1000
- ease-out-expo
- 1000
- ease-out-back
- 1000
- cubic-bezier(0.2, 0.8, 0.2, 1)
- 1000
Stagger: one after another
120340560780
Speed: 1,000 per second
5004000
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Easings, stagger and speed</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="motion" lang="en">
<h3>Easing</h3>
<dl class="easings">
<div>
<dt>linear</dt>
<dd>
<span data-counter="1000" data-counter-easing="linear" data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>ease-in</dt>
<dd>
<span data-counter="1000" data-counter-easing="ease-in" data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>ease-out</dt>
<dd>
<span data-counter="1000" data-counter-easing="ease-out" data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>ease-in-out</dt>
<dd>
<span
data-counter="1000"
data-counter-easing="ease-in-out"
data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>ease-out-expo</dt>
<dd>
<span
data-counter="1000"
data-counter-easing="ease-out-expo"
data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>ease-out-back</dt>
<dd>
<span
data-counter="1000"
data-counter-easing="ease-out-back"
data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>cubic-bezier(0.2, 0.8, 0.2, 1)</dt>
<dd>
<span
data-counter="1000"
data-counter-easing="cubic-bezier(0.2, 0.8, 0.2, 1)"
data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
</dl>
<h3>Stagger: one after another</h3>
<p class="row" data-counter-stagger="300">
<span data-counter="120" data-counter-duration="800">120</span>
<span data-counter="340" data-counter-duration="800">340</span>
<span data-counter="560" data-counter-duration="800">560</span>
<span data-counter="780" data-counter-duration="800">780</span>
</p>
<h3>Speed: 1,000 per second</h3>
<p class="row">
<span data-counter="500" data-counter-speed="1000">500</span>
<span data-counter="4000" data-counter-speed="1000" data-counter-grouping="true"
>4000</span
>
</p>
</div>
<button type="button" id="again">Count again</button>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
.motion h3 {
margin: 1rem 0 0.5rem;
font-size: 0.875rem;
}
.easings {
display: grid;
gap: 0.25rem;
margin: 0;
}
.easings div {
--counter-progress: 1;
display: grid;
grid-template-columns: 13rem 1fr;
gap: 1rem;
align-items: center;
}
.easings dt {
font:
0.8125rem/1.4 ui-monospace,
monospace;
}
.easings dd {
display: grid;
grid-template-columns: 3.5rem 1fr;
gap: 0.5rem;
align-items: center;
margin: 0;
font-variant-numeric: tabular-nums;
}
/* The bar shows the curve: --counter-progress can go past 1 with ease-out-back. */
.bar {
height: 0.5rem;
width: calc(var(--counter-progress) * 100%);
max-width: 110%;
border-radius: 1rem;
background: #c2410c;
}
.row {
display: flex;
gap: 1.5rem;
margin: 0;
font-size: 1.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
JavaScript
// Start now, so getCounter finds each number's controller.
Counter.auto()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.motion [data-counter]')
.forEach(number => Counter.getCounter(number)?.start())
)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Easings, stagger and speed</title>
<style>
.motion h3 {
margin: 1rem 0 0.5rem;
font-size: 0.875rem;
}
.easings {
display: grid;
gap: 0.25rem;
margin: 0;
}
.easings div {
--counter-progress: 1;
display: grid;
grid-template-columns: 13rem 1fr;
gap: 1rem;
align-items: center;
}
.easings dt {
font:
0.8125rem/1.4 ui-monospace,
monospace;
}
.easings dd {
display: grid;
grid-template-columns: 3.5rem 1fr;
gap: 0.5rem;
align-items: center;
margin: 0;
font-variant-numeric: tabular-nums;
}
/* The bar shows the curve: --counter-progress can go past 1 with ease-out-back. */
.bar {
height: 0.5rem;
width: calc(var(--counter-progress) * 100%);
max-width: 110%;
border-radius: 1rem;
background: #c2410c;
}
.row {
display: flex;
gap: 1.5rem;
margin: 0;
font-size: 1.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
</style>
</head>
<body>
<main>
<div class="motion" lang="en">
<h3>Easing</h3>
<dl class="easings">
<div>
<dt>linear</dt>
<dd>
<span data-counter="1000" data-counter-easing="linear" data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>ease-in</dt>
<dd>
<span data-counter="1000" data-counter-easing="ease-in" data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>ease-out</dt>
<dd>
<span data-counter="1000" data-counter-easing="ease-out" data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>ease-in-out</dt>
<dd>
<span
data-counter="1000"
data-counter-easing="ease-in-out"
data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>ease-out-expo</dt>
<dd>
<span
data-counter="1000"
data-counter-easing="ease-out-expo"
data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>ease-out-back</dt>
<dd>
<span
data-counter="1000"
data-counter-easing="ease-out-back"
data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>cubic-bezier(0.2, 0.8, 0.2, 1)</dt>
<dd>
<span
data-counter="1000"
data-counter-easing="cubic-bezier(0.2, 0.8, 0.2, 1)"
data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
</dl>
<h3>Stagger: one after another</h3>
<p class="row" data-counter-stagger="300">
<span data-counter="120" data-counter-duration="800">120</span>
<span data-counter="340" data-counter-duration="800">340</span>
<span data-counter="560" data-counter-duration="800">560</span>
<span data-counter="780" data-counter-duration="800">780</span>
</p>
<h3>Speed: 1,000 per second</h3>
<p class="row">
<span data-counter="500" data-counter-speed="1000">500</span>
<span data-counter="4000" data-counter-speed="1000" data-counter-grouping="true"
>4000</span
>
</p>
</div>
<button type="button" id="again">Count again</button>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script>
// Start now, so getCounter finds each number's controller.
Counter.auto()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.motion [data-counter]')
.forEach(number => Counter.getCounter(number)?.start())
)
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Easings, stagger and speed</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="motion" lang="en">
<h3>Easing</h3>
<dl class="easings">
<div>
<dt>linear</dt>
<dd>
<span data-counter="1000" data-counter-easing="linear" data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>ease-in</dt>
<dd>
<span data-counter="1000" data-counter-easing="ease-in" data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>ease-out</dt>
<dd>
<span data-counter="1000" data-counter-easing="ease-out" data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>ease-in-out</dt>
<dd>
<span
data-counter="1000"
data-counter-easing="ease-in-out"
data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>ease-out-expo</dt>
<dd>
<span
data-counter="1000"
data-counter-easing="ease-out-expo"
data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>ease-out-back</dt>
<dd>
<span
data-counter="1000"
data-counter-easing="ease-out-back"
data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>cubic-bezier(0.2, 0.8, 0.2, 1)</dt>
<dd>
<span
data-counter="1000"
data-counter-easing="cubic-bezier(0.2, 0.8, 0.2, 1)"
data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
</dl>
<h3>Stagger: one after another</h3>
<p class="row" data-counter-stagger="300">
<span data-counter="120" data-counter-duration="800">120</span>
<span data-counter="340" data-counter-duration="800">340</span>
<span data-counter="560" data-counter-duration="800">560</span>
<span data-counter="780" data-counter-duration="800">780</span>
</p>
<h3>Speed: 1,000 per second</h3>
<p class="row">
<span data-counter="500" data-counter-speed="1000">500</span>
<span data-counter="4000" data-counter-speed="1000" data-counter-grouping="true"
>4000</span
>
</p>
</div>
<button type="button" id="again">Count again</button>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
.motion h3 {
margin: 1rem 0 0.5rem;
font-size: 0.875rem;
}
.easings {
display: grid;
gap: 0.25rem;
margin: 0;
}
.easings div {
--counter-progress: 1;
display: grid;
grid-template-columns: 13rem 1fr;
gap: 1rem;
align-items: center;
}
.easings dt {
font:
0.8125rem/1.4 ui-monospace,
monospace;
}
.easings dd {
display: grid;
grid-template-columns: 3.5rem 1fr;
gap: 0.5rem;
align-items: center;
margin: 0;
font-variant-numeric: tabular-nums;
}
/* The bar shows the curve: --counter-progress can go past 1 with ease-out-back. */
.bar {
height: 0.5rem;
width: calc(var(--counter-progress) * 100%);
max-width: 110%;
border-radius: 1rem;
background: #c2410c;
}
.row {
display: flex;
gap: 1.5rem;
margin: 0;
font-size: 1.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
JavaScript
import { autoCounter, getCounter } from '@samline/counter'
autoCounter()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.motion [data-counter]')
.forEach(number => getCounter(number)?.start())
)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Easings, stagger and speed</title>
<style>
.motion h3 {
margin: 1rem 0 0.5rem;
font-size: 0.875rem;
}
.easings {
display: grid;
gap: 0.25rem;
margin: 0;
}
.easings div {
--counter-progress: 1;
display: grid;
grid-template-columns: 13rem 1fr;
gap: 1rem;
align-items: center;
}
.easings dt {
font:
0.8125rem/1.4 ui-monospace,
monospace;
}
.easings dd {
display: grid;
grid-template-columns: 3.5rem 1fr;
gap: 0.5rem;
align-items: center;
margin: 0;
font-variant-numeric: tabular-nums;
}
/* The bar shows the curve: --counter-progress can go past 1 with ease-out-back. */
.bar {
height: 0.5rem;
width: calc(var(--counter-progress) * 100%);
max-width: 110%;
border-radius: 1rem;
background: #c2410c;
}
.row {
display: flex;
gap: 1.5rem;
margin: 0;
font-size: 1.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
</style>
</head>
<body>
<main>
<div class="motion" lang="en">
<h3>Easing</h3>
<dl class="easings">
<div>
<dt>linear</dt>
<dd>
<span data-counter="1000" data-counter-easing="linear" data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>ease-in</dt>
<dd>
<span data-counter="1000" data-counter-easing="ease-in" data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>ease-out</dt>
<dd>
<span data-counter="1000" data-counter-easing="ease-out" data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>ease-in-out</dt>
<dd>
<span
data-counter="1000"
data-counter-easing="ease-in-out"
data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>ease-out-expo</dt>
<dd>
<span
data-counter="1000"
data-counter-easing="ease-out-expo"
data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>ease-out-back</dt>
<dd>
<span
data-counter="1000"
data-counter-easing="ease-out-back"
data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
<div>
<dt>cubic-bezier(0.2, 0.8, 0.2, 1)</dt>
<dd>
<span
data-counter="1000"
data-counter-easing="cubic-bezier(0.2, 0.8, 0.2, 1)"
data-counter-css-vars="div"
>1000</span
><i class="bar"></i>
</dd>
</div>
</dl>
<h3>Stagger: one after another</h3>
<p class="row" data-counter-stagger="300">
<span data-counter="120" data-counter-duration="800">120</span>
<span data-counter="340" data-counter-duration="800">340</span>
<span data-counter="560" data-counter-duration="800">560</span>
<span data-counter="780" data-counter-duration="800">780</span>
</p>
<h3>Speed: 1,000 per second</h3>
<p class="row">
<span data-counter="500" data-counter-speed="1000">500</span>
<span data-counter="4000" data-counter-speed="1000" data-counter-grouping="true"
>4000</span
>
</p>
</div>
<button type="button" id="again">Count again</button>
</main>
<script type="module">
import { autoCounter, getCounter } from '@samline/counter'
autoCounter()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.motion [data-counter]')
.forEach(number => getCounter(number)?.start())
)
</script>
</body>
</html>
Progress bars, meters and CSS that follow the count
data-counter-output writes the value to a progress, meter or input as it counts. data-counter-css-vars sets --counter-value and --counter-progress, from 0 to 1, on the number or on its closest match, so a ring or a bar follows it with CSS alone.
Progress bar
72%
Meter
38.5 GB of 50 GB
Ring from --counter-value
Goal from --counter-progress
2000 steps
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Progress bars, meters and CSS that follow the count</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="meters" lang="en">
<section>
<h3>Progress bar</h3>
<p class="big"><span data-counter="72" data-counter-output="#html-skill">72</span>%</p>
<progress id="html-skill" max="100" value="72">72%</progress>
</section>
<section>
<h3>Meter</h3>
<p class="big">
<span data-counter="38.5" data-counter-output="#storage">38.5</span> GB of 50 GB
</p>
<meter id="storage" min="0" max="50" high="45" value="38.5">38.5 GB</meter>
</section>
<section>
<h3>Ring from --counter-value</h3>
<div class="ring">
<span class="big" data-counter data-counter-css-vars=".ring">86%</span>
</div>
</section>
<section>
<h3>Goal from --counter-progress</h3>
<div class="track"><div class="fill"></div></div>
<p class="big">
<span
data-counter="2000"
data-counter-grouping="true"
data-counter-css-vars=".meters section"
>2000</span
>
steps
</p>
</section>
</div>
<button type="button" id="again">Count again</button>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
.meters {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
gap: 1.5rem;
margin-bottom: 1rem;
}
.meters h3 {
margin: 0 0 0.25rem;
font-size: 0.875rem;
}
.big {
margin: 0 0 0.5rem;
font-size: 1.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
progress,
meter {
width: 100%;
}
/* --counter-value is the number on screen; --counter-progress goes from 0 to 1. */
.ring {
--counter-value: 86;
display: grid;
place-items: center;
width: 7rem;
aspect-ratio: 1;
border-radius: 50%;
background:
radial-gradient(closest-side, white 78%, transparent 80%),
conic-gradient(#c2410c calc(var(--counter-value) * 3.6deg), #e5e5e5 0);
}
.track {
height: 0.75rem;
border-radius: 1rem;
background: #e5e5e5;
overflow: hidden;
margin-bottom: 0.5rem;
}
.meters section {
--counter-progress: 1;
}
.fill {
height: 100%;
width: calc(var(--counter-progress) * 100%);
background: #c2410c;
}
JavaScript
// Every [data-counter] counts on its own. Start now, so getCounter finds them.
Counter.auto()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.meters [data-counter]')
.forEach(number => Counter.getCounter(number)?.start())
)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Progress bars, meters and CSS that follow the count</title>
<style>
.meters {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
gap: 1.5rem;
margin-bottom: 1rem;
}
.meters h3 {
margin: 0 0 0.25rem;
font-size: 0.875rem;
}
.big {
margin: 0 0 0.5rem;
font-size: 1.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
progress,
meter {
width: 100%;
}
/* --counter-value is the number on screen; --counter-progress goes from 0 to 1. */
.ring {
--counter-value: 86;
display: grid;
place-items: center;
width: 7rem;
aspect-ratio: 1;
border-radius: 50%;
background:
radial-gradient(closest-side, white 78%, transparent 80%),
conic-gradient(#c2410c calc(var(--counter-value) * 3.6deg), #e5e5e5 0);
}
.track {
height: 0.75rem;
border-radius: 1rem;
background: #e5e5e5;
overflow: hidden;
margin-bottom: 0.5rem;
}
.meters section {
--counter-progress: 1;
}
.fill {
height: 100%;
width: calc(var(--counter-progress) * 100%);
background: #c2410c;
}
</style>
</head>
<body>
<main>
<div class="meters" lang="en">
<section>
<h3>Progress bar</h3>
<p class="big"><span data-counter="72" data-counter-output="#html-skill">72</span>%</p>
<progress id="html-skill" max="100" value="72">72%</progress>
</section>
<section>
<h3>Meter</h3>
<p class="big">
<span data-counter="38.5" data-counter-output="#storage">38.5</span> GB of 50 GB
</p>
<meter id="storage" min="0" max="50" high="45" value="38.5">38.5 GB</meter>
</section>
<section>
<h3>Ring from --counter-value</h3>
<div class="ring">
<span class="big" data-counter data-counter-css-vars=".ring">86%</span>
</div>
</section>
<section>
<h3>Goal from --counter-progress</h3>
<div class="track"><div class="fill"></div></div>
<p class="big">
<span
data-counter="2000"
data-counter-grouping="true"
data-counter-css-vars=".meters section"
>2000</span
>
steps
</p>
</section>
</div>
<button type="button" id="again">Count again</button>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script>
// Every [data-counter] counts on its own. Start now, so getCounter finds them.
Counter.auto()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.meters [data-counter]')
.forEach(number => Counter.getCounter(number)?.start())
)
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Progress bars, meters and CSS that follow the count</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="meters" lang="en">
<section>
<h3>Progress bar</h3>
<p class="big"><span data-counter="72" data-counter-output="#html-skill">72</span>%</p>
<progress id="html-skill" max="100" value="72">72%</progress>
</section>
<section>
<h3>Meter</h3>
<p class="big">
<span data-counter="38.5" data-counter-output="#storage">38.5</span> GB of 50 GB
</p>
<meter id="storage" min="0" max="50" high="45" value="38.5">38.5 GB</meter>
</section>
<section>
<h3>Ring from --counter-value</h3>
<div class="ring">
<span class="big" data-counter data-counter-css-vars=".ring">86%</span>
</div>
</section>
<section>
<h3>Goal from --counter-progress</h3>
<div class="track"><div class="fill"></div></div>
<p class="big">
<span
data-counter="2000"
data-counter-grouping="true"
data-counter-css-vars=".meters section"
>2000</span
>
steps
</p>
</section>
</div>
<button type="button" id="again">Count again</button>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
.meters {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
gap: 1.5rem;
margin-bottom: 1rem;
}
.meters h3 {
margin: 0 0 0.25rem;
font-size: 0.875rem;
}
.big {
margin: 0 0 0.5rem;
font-size: 1.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
progress,
meter {
width: 100%;
}
/* --counter-value is the number on screen; --counter-progress goes from 0 to 1. */
.ring {
--counter-value: 86;
display: grid;
place-items: center;
width: 7rem;
aspect-ratio: 1;
border-radius: 50%;
background:
radial-gradient(closest-side, white 78%, transparent 80%),
conic-gradient(#c2410c calc(var(--counter-value) * 3.6deg), #e5e5e5 0);
}
.track {
height: 0.75rem;
border-radius: 1rem;
background: #e5e5e5;
overflow: hidden;
margin-bottom: 0.5rem;
}
.meters section {
--counter-progress: 1;
}
.fill {
height: 100%;
width: calc(var(--counter-progress) * 100%);
background: #c2410c;
}
JavaScript
import { autoCounter, getCounter } from '@samline/counter'
autoCounter()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.meters [data-counter]')
.forEach(number => getCounter(number)?.start())
)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Progress bars, meters and CSS that follow the count</title>
<style>
.meters {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
gap: 1.5rem;
margin-bottom: 1rem;
}
.meters h3 {
margin: 0 0 0.25rem;
font-size: 0.875rem;
}
.big {
margin: 0 0 0.5rem;
font-size: 1.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
progress,
meter {
width: 100%;
}
/* --counter-value is the number on screen; --counter-progress goes from 0 to 1. */
.ring {
--counter-value: 86;
display: grid;
place-items: center;
width: 7rem;
aspect-ratio: 1;
border-radius: 50%;
background:
radial-gradient(closest-side, white 78%, transparent 80%),
conic-gradient(#c2410c calc(var(--counter-value) * 3.6deg), #e5e5e5 0);
}
.track {
height: 0.75rem;
border-radius: 1rem;
background: #e5e5e5;
overflow: hidden;
margin-bottom: 0.5rem;
}
.meters section {
--counter-progress: 1;
}
.fill {
height: 100%;
width: calc(var(--counter-progress) * 100%);
background: #c2410c;
}
</style>
</head>
<body>
<main>
<div class="meters" lang="en">
<section>
<h3>Progress bar</h3>
<p class="big"><span data-counter="72" data-counter-output="#html-skill">72</span>%</p>
<progress id="html-skill" max="100" value="72">72%</progress>
</section>
<section>
<h3>Meter</h3>
<p class="big">
<span data-counter="38.5" data-counter-output="#storage">38.5</span> GB of 50 GB
</p>
<meter id="storage" min="0" max="50" high="45" value="38.5">38.5 GB</meter>
</section>
<section>
<h3>Ring from --counter-value</h3>
<div class="ring">
<span class="big" data-counter data-counter-css-vars=".ring">86%</span>
</div>
</section>
<section>
<h3>Goal from --counter-progress</h3>
<div class="track"><div class="fill"></div></div>
<p class="big">
<span
data-counter="2000"
data-counter-grouping="true"
data-counter-css-vars=".meters section"
>2000</span
>
steps
</p>
</section>
</div>
<button type="button" id="again">Count again</button>
</main>
<script type="module">
import { autoCounter, getCounter } from '@samline/counter'
autoCounter()
document
.querySelector('#again')
.addEventListener('click', () =>
document
.querySelectorAll('.meters [data-counter]')
.forEach(number => getCounter(number)?.start())
)
</script>
</body>
</html>
Numbers that change after they count
set() counts from the value on screen to a new one, increment() and decrement() add up quick clicks, and changing data-counter from your server or script counts to the new value on its own.
Price
$12 per month
Cart
1 items
Visitors right now
1,280
A new value arrives every two seconds.
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Numbers that change after they count</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="live" lang="en">
<section>
<h3>Price</h3>
<p class="big"><span id="price" data-counter="12">$12</span> per month</p>
<div class="actions" role="group" aria-label="Billing">
<button type="button" data-price="12" aria-pressed="true">Monthly</button>
<button type="button" data-price="10" aria-pressed="false">Yearly</button>
</div>
</section>
<section>
<h3>Cart</h3>
<p class="big"><span id="items" data-counter="1">1</span> items</p>
<div class="actions">
<button type="button" id="remove" aria-label="Remove an item">
<svg
aria-hidden="true"
focusable="false"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
>
<path d="M5 12h14" />
</svg>
</button>
<button type="button" id="add" aria-label="Add an item">
<svg
aria-hidden="true"
focusable="false"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
>
<path d="M12 5v14M5 12h14" />
</svg>
</button>
</div>
</section>
<section>
<h3>Visitors right now</h3>
<p class="big">
<span id="visitors" data-counter="1280" data-counter-duration="800">1,280</span>
</p>
<p class="note">A new value arrives every two seconds.</p>
</section>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
.live {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
gap: 1.5rem;
}
.live h3 {
margin: 0 0 0.25rem;
font-size: 0.875rem;
}
.big {
margin: 0 0 0.5rem;
font-size: 1.75rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
.actions {
display: flex;
gap: 0.5rem;
}
.actions button {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 2.5rem;
}
.actions button[aria-pressed='true'] {
font-weight: 700;
}
.note {
margin: 0;
font-size: 0.875rem;
}
JavaScript
// Start counting now (this replaces the automatic start), so getCounter finds
// each number's controller right away.
Counter.auto()
const price = Counter.getCounter(document.querySelector('#price'))
document.querySelectorAll('[data-price]').forEach(button =>
button.addEventListener('click', () => {
document
.querySelectorAll('[data-price]')
.forEach(other => other.setAttribute('aria-pressed', String(other === button)))
price.set(Number(button.dataset.price))
})
)
// increment and decrement add up, so quick clicks count to the right total.
const items = Counter.getCounter(document.querySelector('#items'))
document.querySelector('#add').addEventListener('click', () => items.increment())
document.querySelector('#remove').addEventListener('click', () => {
if (items.getSnapshot()[0].to > 0) items.decrement()
})
// Your server or script only changes the attribute; the number counts to it.
const visitors = document.querySelector('#visitors')
setInterval(() => {
const next = Number(visitors.dataset.counter) + Math.round(Math.random() * 60 - 25)
visitors.dataset.counter = String(Math.max(0, next))
}, 2000)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Numbers that change after they count</title>
<style>
.live {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
gap: 1.5rem;
}
.live h3 {
margin: 0 0 0.25rem;
font-size: 0.875rem;
}
.big {
margin: 0 0 0.5rem;
font-size: 1.75rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
.actions {
display: flex;
gap: 0.5rem;
}
.actions button {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 2.5rem;
}
.actions button[aria-pressed='true'] {
font-weight: 700;
}
.note {
margin: 0;
font-size: 0.875rem;
}
</style>
</head>
<body>
<main>
<div class="live" lang="en">
<section>
<h3>Price</h3>
<p class="big"><span id="price" data-counter="12">$12</span> per month</p>
<div class="actions" role="group" aria-label="Billing">
<button type="button" data-price="12" aria-pressed="true">Monthly</button>
<button type="button" data-price="10" aria-pressed="false">Yearly</button>
</div>
</section>
<section>
<h3>Cart</h3>
<p class="big"><span id="items" data-counter="1">1</span> items</p>
<div class="actions">
<button type="button" id="remove" aria-label="Remove an item">
<svg
aria-hidden="true"
focusable="false"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
>
<path d="M5 12h14" />
</svg>
</button>
<button type="button" id="add" aria-label="Add an item">
<svg
aria-hidden="true"
focusable="false"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
>
<path d="M12 5v14M5 12h14" />
</svg>
</button>
</div>
</section>
<section>
<h3>Visitors right now</h3>
<p class="big">
<span id="visitors" data-counter="1280" data-counter-duration="800">1,280</span>
</p>
<p class="note">A new value arrives every two seconds.</p>
</section>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script>
// Start counting now (this replaces the automatic start), so getCounter finds
// each number's controller right away.
Counter.auto()
const price = Counter.getCounter(document.querySelector('#price'))
document.querySelectorAll('[data-price]').forEach(button =>
button.addEventListener('click', () => {
document
.querySelectorAll('[data-price]')
.forEach(other => other.setAttribute('aria-pressed', String(other === button)))
price.set(Number(button.dataset.price))
})
)
// increment and decrement add up, so quick clicks count to the right total.
const items = Counter.getCounter(document.querySelector('#items'))
document.querySelector('#add').addEventListener('click', () => items.increment())
document.querySelector('#remove').addEventListener('click', () => {
if (items.getSnapshot()[0].to > 0) items.decrement()
})
// Your server or script only changes the attribute; the number counts to it.
const visitors = document.querySelector('#visitors')
setInterval(() => {
const next = Number(visitors.dataset.counter) + Math.round(Math.random() * 60 - 25)
visitors.dataset.counter = String(Math.max(0, next))
}, 2000)
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Numbers that change after they count</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="live" lang="en">
<section>
<h3>Price</h3>
<p class="big"><span id="price" data-counter="12">$12</span> per month</p>
<div class="actions" role="group" aria-label="Billing">
<button type="button" data-price="12" aria-pressed="true">Monthly</button>
<button type="button" data-price="10" aria-pressed="false">Yearly</button>
</div>
</section>
<section>
<h3>Cart</h3>
<p class="big"><span id="items" data-counter="1">1</span> items</p>
<div class="actions">
<button type="button" id="remove" aria-label="Remove an item">
<svg
aria-hidden="true"
focusable="false"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
>
<path d="M5 12h14" />
</svg>
</button>
<button type="button" id="add" aria-label="Add an item">
<svg
aria-hidden="true"
focusable="false"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
>
<path d="M12 5v14M5 12h14" />
</svg>
</button>
</div>
</section>
<section>
<h3>Visitors right now</h3>
<p class="big">
<span id="visitors" data-counter="1280" data-counter-duration="800">1,280</span>
</p>
<p class="note">A new value arrives every two seconds.</p>
</section>
</div>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
.live {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
gap: 1.5rem;
}
.live h3 {
margin: 0 0 0.25rem;
font-size: 0.875rem;
}
.big {
margin: 0 0 0.5rem;
font-size: 1.75rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
.actions {
display: flex;
gap: 0.5rem;
}
.actions button {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 2.5rem;
}
.actions button[aria-pressed='true'] {
font-weight: 700;
}
.note {
margin: 0;
font-size: 0.875rem;
}
JavaScript
import { autoCounter, getCounter } from '@samline/counter'
autoCounter()
const price = getCounter(document.querySelector('#price'))
document.querySelectorAll('[data-price]').forEach(button =>
button.addEventListener('click', () => {
document
.querySelectorAll('[data-price]')
.forEach(other => other.setAttribute('aria-pressed', String(other === button)))
price.set(Number(button.dataset.price))
})
)
// increment and decrement add up, so quick clicks count to the right total.
const items = getCounter(document.querySelector('#items'))
document.querySelector('#add').addEventListener('click', () => items.increment())
document.querySelector('#remove').addEventListener('click', () => {
if (items.getSnapshot()[0].to > 0) items.decrement()
})
// Your server or script only changes the attribute; the number counts to it.
const visitors = document.querySelector('#visitors')
setInterval(() => {
const next = Number(visitors.dataset.counter) + Math.round(Math.random() * 60 - 25)
visitors.dataset.counter = String(Math.max(0, next))
}, 2000)
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Numbers that change after they count</title>
<style>
.live {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
gap: 1.5rem;
}
.live h3 {
margin: 0 0 0.25rem;
font-size: 0.875rem;
}
.big {
margin: 0 0 0.5rem;
font-size: 1.75rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
.actions {
display: flex;
gap: 0.5rem;
}
.actions button {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 2.5rem;
}
.actions button[aria-pressed='true'] {
font-weight: 700;
}
.note {
margin: 0;
font-size: 0.875rem;
}
</style>
</head>
<body>
<main>
<div class="live" lang="en">
<section>
<h3>Price</h3>
<p class="big"><span id="price" data-counter="12">$12</span> per month</p>
<div class="actions" role="group" aria-label="Billing">
<button type="button" data-price="12" aria-pressed="true">Monthly</button>
<button type="button" data-price="10" aria-pressed="false">Yearly</button>
</div>
</section>
<section>
<h3>Cart</h3>
<p class="big"><span id="items" data-counter="1">1</span> items</p>
<div class="actions">
<button type="button" id="remove" aria-label="Remove an item">
<svg
aria-hidden="true"
focusable="false"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
>
<path d="M5 12h14" />
</svg>
</button>
<button type="button" id="add" aria-label="Add an item">
<svg
aria-hidden="true"
focusable="false"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
>
<path d="M12 5v14M5 12h14" />
</svg>
</button>
</div>
</section>
<section>
<h3>Visitors right now</h3>
<p class="big">
<span id="visitors" data-counter="1280" data-counter-duration="800">1,280</span>
</p>
<p class="note">A new value arrives every two seconds.</p>
</section>
</div>
</main>
<script type="module">
import { autoCounter, getCounter } from '@samline/counter'
autoCounter()
const price = getCounter(document.querySelector('#price'))
document.querySelectorAll('[data-price]').forEach(button =>
button.addEventListener('click', () => {
document
.querySelectorAll('[data-price]')
.forEach(other => other.setAttribute('aria-pressed', String(other === button)))
price.set(Number(button.dataset.price))
})
)
// increment and decrement add up, so quick clicks count to the right total.
const items = getCounter(document.querySelector('#items'))
document.querySelector('#add').addEventListener('click', () => items.increment())
document.querySelector('#remove').addEventListener('click', () => {
if (items.getSnapshot()[0].to > 0) items.decrement()
})
// Your server or script only changes the attribute; the number counts to it.
const visitors = document.querySelector('#visitors')
setInterval(() => {
const next = Number(visitors.dataset.counter) + Math.round(Math.random() * 60 - 25)
visitors.dataset.counter = String(Math.max(0, next))
}, 2000)
</script>
</body>
</html>
The final value for screen readers, and announced changes
While a number counts, screen readers get its final value instead of the digits in between, and plain text again once it is done. data-counter-announce reads new values in a polite live region, here with a template. data-counter-reduced-motion="count" keeps counting when visitors reduce motion, for numbers whose change is the content.
12,480 customers
A screen reader reads the number as:
38 people online
Last announcement:
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>The final value for screen readers, and announced changes</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="a11y" lang="en">
<p class="stat"><span id="customers" data-counter="12480">12,480</span> customers</p>
<p class="hears">A screen reader reads the number as: <q id="hears"></q></p>
<p class="stat">
<span
id="online"
data-counter="38"
data-counter-duration="600"
data-counter-announce="{value} people online"
>38</span
>
people online
</p>
<p class="actions">
<button type="button" id="join">Someone joins</button>
<button type="button" id="leave">Someone leaves</button>
</p>
<p class="hears">Last announcement: <q id="said"></q></p>
</div>
<button type="button" id="again">Count again</button>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
.a11y p {
margin: 0 0 0.75rem;
}
.a11y .stat {
font-size: 1.5rem;
}
.a11y .stat span {
font-weight: 600;
font-variant-numeric: tabular-nums;
}
.a11y .hears {
font-size: 0.875rem;
}
.a11y .actions {
display: flex;
gap: 0.5rem;
}
JavaScript
// Start now, so getCounter finds each number's controller.
Counter.auto()
const customers = document.querySelector('#customers')
const online = Counter.getCounter(document.querySelector('#online'))
// While it counts, the number is labelled with its final value; when done, it is plain text.
const hears = document.querySelector('#hears')
const showHeard = () =>
(hears.textContent = customers.getAttribute('aria-label') ?? customers.textContent)
new MutationObserver(showHeard).observe(customers, {
attributes: true,
childList: true,
characterData: true,
subtree: true,
})
showHeard()
// The polite live region Counter adds to the page, shown here so you can see it.
const said = document.querySelector('#said')
new MutationObserver(() => {
const text = document.querySelector('[data-counter-announcer]')?.textContent
if (text && text !== said.textContent) said.textContent = text
}).observe(document.body, { childList: true, characterData: true, subtree: true })
document.querySelector('#join').addEventListener('click', () => online.increment())
document.querySelector('#leave').addEventListener('click', () => online.decrement())
document
.querySelector('#again')
.addEventListener('click', () => Counter.getCounter(customers).start())
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>The final value for screen readers, and announced changes</title>
<style>
.a11y p {
margin: 0 0 0.75rem;
}
.a11y .stat {
font-size: 1.5rem;
}
.a11y .stat span {
font-weight: 600;
font-variant-numeric: tabular-nums;
}
.a11y .hears {
font-size: 0.875rem;
}
.a11y .actions {
display: flex;
gap: 0.5rem;
}
</style>
</head>
<body>
<main>
<div class="a11y" lang="en">
<p class="stat"><span id="customers" data-counter="12480">12,480</span> customers</p>
<p class="hears">A screen reader reads the number as: <q id="hears"></q></p>
<p class="stat">
<span
id="online"
data-counter="38"
data-counter-duration="600"
data-counter-announce="{value} people online"
>38</span
>
people online
</p>
<p class="actions">
<button type="button" id="join">Someone joins</button>
<button type="button" id="leave">Someone leaves</button>
</p>
<p class="hears">Last announcement: <q id="said"></q></p>
</div>
<button type="button" id="again">Count again</button>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script>
// Start now, so getCounter finds each number's controller.
Counter.auto()
const customers = document.querySelector('#customers')
const online = Counter.getCounter(document.querySelector('#online'))
// While it counts, the number is labelled with its final value; when done, it is plain text.
const hears = document.querySelector('#hears')
const showHeard = () =>
(hears.textContent = customers.getAttribute('aria-label') ?? customers.textContent)
new MutationObserver(showHeard).observe(customers, {
attributes: true,
childList: true,
characterData: true,
subtree: true,
})
showHeard()
// The polite live region Counter adds to the page, shown here so you can see it.
const said = document.querySelector('#said')
new MutationObserver(() => {
const text = document.querySelector('[data-counter-announcer]')?.textContent
if (text && text !== said.textContent) said.textContent = text
}).observe(document.body, { childList: true, characterData: true, subtree: true })
document.querySelector('#join').addEventListener('click', () => online.increment())
document.querySelector('#leave').addEventListener('click', () => online.decrement())
document
.querySelector('#again')
.addEventListener('click', () => Counter.getCounter(customers).start())
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>The final value for screen readers, and announced changes</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="a11y" lang="en">
<p class="stat"><span id="customers" data-counter="12480">12,480</span> customers</p>
<p class="hears">A screen reader reads the number as: <q id="hears"></q></p>
<p class="stat">
<span
id="online"
data-counter="38"
data-counter-duration="600"
data-counter-announce="{value} people online"
>38</span
>
people online
</p>
<p class="actions">
<button type="button" id="join">Someone joins</button>
<button type="button" id="leave">Someone leaves</button>
</p>
<p class="hears">Last announcement: <q id="said"></q></p>
</div>
<button type="button" id="again">Count again</button>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
.a11y p {
margin: 0 0 0.75rem;
}
.a11y .stat {
font-size: 1.5rem;
}
.a11y .stat span {
font-weight: 600;
font-variant-numeric: tabular-nums;
}
.a11y .hears {
font-size: 0.875rem;
}
.a11y .actions {
display: flex;
gap: 0.5rem;
}
JavaScript
import { autoCounter, getCounter } from '@samline/counter'
autoCounter()
const customers = document.querySelector('#customers')
const online = getCounter(document.querySelector('#online'))
// While it counts, the number is labelled with its final value; when done, it is plain text.
const hears = document.querySelector('#hears')
const showHeard = () =>
(hears.textContent = customers.getAttribute('aria-label') ?? customers.textContent)
new MutationObserver(showHeard).observe(customers, {
attributes: true,
childList: true,
characterData: true,
subtree: true,
})
showHeard()
// The polite live region Counter adds to the page, shown here so you can see it.
const said = document.querySelector('#said')
new MutationObserver(() => {
const text = document.querySelector('[data-counter-announcer]')?.textContent
if (text && text !== said.textContent) said.textContent = text
}).observe(document.body, { childList: true, characterData: true, subtree: true })
document.querySelector('#join').addEventListener('click', () => online.increment())
document.querySelector('#leave').addEventListener('click', () => online.decrement())
document.querySelector('#again').addEventListener('click', () => getCounter(customers).start())
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>The final value for screen readers, and announced changes</title>
<style>
.a11y p {
margin: 0 0 0.75rem;
}
.a11y .stat {
font-size: 1.5rem;
}
.a11y .stat span {
font-weight: 600;
font-variant-numeric: tabular-nums;
}
.a11y .hears {
font-size: 0.875rem;
}
.a11y .actions {
display: flex;
gap: 0.5rem;
}
</style>
</head>
<body>
<main>
<div class="a11y" lang="en">
<p class="stat"><span id="customers" data-counter="12480">12,480</span> customers</p>
<p class="hears">A screen reader reads the number as: <q id="hears"></q></p>
<p class="stat">
<span
id="online"
data-counter="38"
data-counter-duration="600"
data-counter-announce="{value} people online"
>38</span
>
people online
</p>
<p class="actions">
<button type="button" id="join">Someone joins</button>
<button type="button" id="leave">Someone leaves</button>
</p>
<p class="hears">Last announcement: <q id="said"></q></p>
</div>
<button type="button" id="again">Count again</button>
</main>
<script type="module">
import { autoCounter, getCounter } from '@samline/counter'
autoCounter()
const customers = document.querySelector('#customers')
const online = getCounter(document.querySelector('#online'))
// While it counts, the number is labelled with its final value; when done, it is plain text.
const hears = document.querySelector('#hears')
const showHeard = () =>
(hears.textContent = customers.getAttribute('aria-label') ?? customers.textContent)
new MutationObserver(showHeard).observe(customers, {
attributes: true,
childList: true,
characterData: true,
subtree: true,
})
showHeard()
// The polite live region Counter adds to the page, shown here so you can see it.
const said = document.querySelector('#said')
new MutationObserver(() => {
const text = document.querySelector('[data-counter-announcer]')?.textContent
if (text && text !== said.textContent) said.textContent = text
}).observe(document.body, { childList: true, characterData: true, subtree: true })
document.querySelector('#join').addEventListener('click', () => online.increment())
document.querySelector('#leave').addEventListener('click', () => online.decrement())
document
.querySelector('#again')
.addEventListener('click', () => getCounter(customers).start())
</script>
</body>
</html>
Start, pause and finish a simulated clock
start: manual waits for start(). The controller pauses, resumes, resets and finishes, and its events report the state. as: "time" counts seconds in the pattern of the text, so they carry into minutes and hours.
Ready
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Start, pause and finish a simulated clock</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="clock">
<time id="clock" class="big">23:59:59</time>
<p id="status" aria-live="polite">Ready</p>
<div class="actions">
<button type="button" data-action="start">Start</button>
<button type="button" data-action="pause">Pause</button>
<button type="button" data-action="resume">Resume</button>
<button type="button" data-action="finish">Finish</button>
<button type="button" data-action="reset">Reset</button>
</div>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
.clock {
display: grid;
gap: 0.75rem;
justify-items: start;
}
.big {
font-size: 2.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
#status {
margin: 0;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
JavaScript
// One day in eight seconds, read from the text: 23:59:59.
const clock = Counter.counter('#clock', {
as: 'time',
duration: 8000,
easing: 'linear',
start: 'manual',
})
const status = document.querySelector('#status')
const labels = {
start: 'Counting',
pause: 'Paused',
resume: 'Counting',
complete: 'Done',
reset: 'Ready',
}
Object.entries(labels).forEach(([event, label]) =>
clock.on(event, () => (status.textContent = label))
)
document
.querySelectorAll('[data-action]')
.forEach(button => button.addEventListener('click', () => clock[button.dataset.action]()))
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Start, pause and finish a simulated clock</title>
<style>
.clock {
display: grid;
gap: 0.75rem;
justify-items: start;
}
.big {
font-size: 2.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
#status {
margin: 0;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
</style>
</head>
<body>
<main>
<div class="clock">
<time id="clock" class="big">23:59:59</time>
<p id="status" aria-live="polite">Ready</p>
<div class="actions">
<button type="button" data-action="start">Start</button>
<button type="button" data-action="pause">Pause</button>
<button type="button" data-action="resume">Resume</button>
<button type="button" data-action="finish">Finish</button>
<button type="button" data-action="reset">Reset</button>
</div>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/counter@1.14.0/dist/browser/counter.global.js"></script>
<script>
// One day in eight seconds, read from the text: 23:59:59.
const clock = Counter.counter('#clock', {
as: 'time',
duration: 8000,
easing: 'linear',
start: 'manual',
})
const status = document.querySelector('#status')
const labels = {
start: 'Counting',
pause: 'Paused',
resume: 'Counting',
complete: 'Done',
reset: 'Ready',
}
Object.entries(labels).forEach(([event, label]) =>
clock.on(event, () => (status.textContent = label))
)
document
.querySelectorAll('[data-action]')
.forEach(button => button.addEventListener('click', () => clock[button.dataset.action]()))
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Start, pause and finish a simulated clock</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="clock">
<time id="clock" class="big">23:59:59</time>
<p id="status" aria-live="polite">Ready</p>
<div class="actions">
<button type="button" data-action="start">Start</button>
<button type="button" data-action="pause">Pause</button>
<button type="button" data-action="resume">Resume</button>
<button type="button" data-action="finish">Finish</button>
<button type="button" data-action="reset">Reset</button>
</div>
</div>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
.clock {
display: grid;
gap: 0.75rem;
justify-items: start;
}
.big {
font-size: 2.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
#status {
margin: 0;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
JavaScript
import { counter } from '@samline/counter'
// One day in eight seconds, read from the text: 23:59:59.
const clock = counter('#clock', { as: 'time', duration: 8000, easing: 'linear', start: 'manual' })
const status = document.querySelector('#status')
const labels = {
start: 'Counting',
pause: 'Paused',
resume: 'Counting',
complete: 'Done',
reset: 'Ready',
}
Object.entries(labels).forEach(([event, label]) =>
clock.on(event, () => (status.textContent = label))
)
document
.querySelectorAll('[data-action]')
.forEach(button => button.addEventListener('click', () => clock[button.dataset.action]()))
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Start, pause and finish a simulated clock</title>
<style>
.clock {
display: grid;
gap: 0.75rem;
justify-items: start;
}
.big {
font-size: 2.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
#status {
margin: 0;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
</style>
</head>
<body>
<main>
<div class="clock">
<time id="clock" class="big">23:59:59</time>
<p id="status" aria-live="polite">Ready</p>
<div class="actions">
<button type="button" data-action="start">Start</button>
<button type="button" data-action="pause">Pause</button>
<button type="button" data-action="resume">Resume</button>
<button type="button" data-action="finish">Finish</button>
<button type="button" data-action="reset">Reset</button>
</div>
</div>
</main>
<script type="module">
import { counter } from '@samline/counter'
// One day in eight seconds, read from the text: 23:59:59.
const clock = counter('#clock', {
as: 'time',
duration: 8000,
easing: 'linear',
start: 'manual',
})
const status = document.querySelector('#status')
const labels = {
start: 'Counting',
pause: 'Paused',
resume: 'Counting',
complete: 'Done',
reset: 'Ready',
}
Object.entries(labels).forEach(([event, label]) =>
clock.on(event, () => (status.textContent = label))
)
document
.querySelectorAll('[data-action]')
.forEach(button => button.addEventListener('click', () => clock[button.dataset.action]()))
</script>
</body>
</html>