CountUp.js is a simple, dependency-free, vanilla JavaScript library written by me. While it’s still under development it has basic features that may come in handy when you just don’t want to download jQuery to animate that one number.
All you need to do is create a new instance, like this:
new CountUp(config);
// we can also assign it to a variable for later use
const countUp = new CountUp();
Now we are good to go!
config
object is used as a way to define own global settings to all counters (in other words settings that will be inherited if no other, single-counter-specific values will be provided). If no value is present the default settings are used.
Supported values:
"easeInOutQuad"
, "easeInOutQuint"
, "easeInOutSine"
(default)
Examples:
new CountUp(); // everything default
new CountUp({ duration: 8 }); // everything default but duration
new CountUp({ easingFunc: "easeInOutQuad", duration: 10 }); // and so on...
new CountUp({ easingFunc: easeOutBounce, duration: 10, decimals: 1 });
To initialize with HTML you need to add data-counter
attribute:
```html
You can pass multiple attributes, adding their corresponding data-option names and values:
```html
<div data-counter="numberToCountTo" data-start="startingNumber" data-decimals="decimalPlaces" data-duration="duration"></div>
Same naming conceptions are used both in HTML data attributes and JavaScript.
Unfortunately if you want to use a custom easing function you need to initialize with JavaScript.
Also, for obvious reasons a data-selector
attribute is not supported. That’d be stupid, right?
const countUp = new CountUp();
countUp.initCounter(options);
options
is an object which currently supports following keys:
Examples:
const countUp = new CountUp();
countUp.initCounter({ selector: document.querySelector("#example1"), easingFunc: "easeInOutQuad", duration: 10, counter: 1580 });
countUp.initCounter({ selector: document.querySelector("#example2"), easingFunc: easeOutBounce, duration: 10, counter: 1580 });
To be continued…