npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

countup-animation

v1.0.15

Published

A JavaScript function to display incrementing numbers.

Downloads

39

Readme

JavaScript Count-up Animation 🔢 🚀

A JavaScript function to display incrementing numbers. See a live website with this function and complete documentation here.

animateCountUp(element, duration, stepSize, startingValue)

Using animateCountUp()

First, import the module:

JavaScript

 import animateCountUp from "./dist/index.js";

Then call the function by targeting an HTML element:

HTML

<h1>JavaScript Count-up Animation...<span id="one-hundred"> 100</span></h1>

JavaScript

// count to 100 on page load
document.addEventListener("DOMContentLoaded", () => {
    const countToOneHundred = document.getElementById("one-hundred");
    animateCountUp(countToOneHundred, 5000, null, null, null);
});

More examples...

You can also use animateCountUp() with multiple numbers within a container element. For this example, we will use a container <div> and run the animation on each number within that container that has the .countup class. Note that you can have other elements with other classnames that won't interfere with the functionality, so we have added additional <span> tags that contain .before-number and .after-number classes. This is to show that we can have other characters such as dollar signs or any other type of text that you don't want to count up:

HTML

<div id="container">
  <ul>
    <li>
      <span class="before-number">$</span>
      <span class="countup">45.23</span>
      <span class="after-number">+</span>
    </li>
    <li>
      <span class="countup">91.123</span>
      <span class="after-number">%</span>
    </li>
    <li>
      <span class="before-number">₣</span>
      <span class="countup">42</span>
      <span class="after-number">!</span>
    </li>
  </ul>
</div>

<div>
    <button id="animateButton">Animate</button>
</div>

JavaScript

 document
.getElementById("animateButton")
.addEventListener("click", () => {
  animateButton.disabled = true;
  const container = document.querySelector("#container");
  const countupEls = container.querySelectorAll(".countup");
  const totalElements = countupEls.length;
  let completedAnimations = 0;

  countupEls.forEach((el, index) => {
    setTimeout(() => {
      animateCountUp(el, 7000, 7, null, () => {
        completedAnimations++;

        // Check if all animations are completed
        if (completedAnimations === totalElements) {
            console.log("Animation finished!");
            animateButton.disabled = false;
        }
      });
    }, index * 700);
  });
});

HTML

<div id="container-two" class="example-two">
  <div class="card">
    <span class="before-number">$</span>
    <span id="countupTwo" class="countup">200</span>
    <span class="after-number">+</span>
  </div>
  <div class="card">
    <span id="countupThree" class="countup">18.76</span>
    <span class="after-number">%</span>
  </div>
  <div class="card">
    <span class="before-number">😎</span>
    <span id="countupFour" class="countup">185.32</span>
    <span class="after-number">!</span>
  </div>
</div>

<button id="animateButtonTwo">Animate</button>

JavaScript

 document
.getElementById("animateButtonTwo")
.addEventListener("click", () => {
  animateButtonTwo.disabled = true;
  const container = document.querySelector("#container-two");
  const countupEls = container.querySelectorAll(".countup");
  const totalElements = countupEls.length;
  let completedAnimations = 0;
          
  countupEls.forEach((el, index) => {
    setTimeout(() => {
      animateCountUp(el, 3500, 1, 0, () => {
        completedAnimations++;
        if (completedAnimations === totalElements) {
          animateButtonTwo.disabled = false;
        }
      });
    }, index * 500);
  });
});

On Scroll

This function can also be triggered on scroll using JavaScript's Intersection Observer API:

HTML

<div id="container-three">
    <h2>This example is triggered on scroll</h2>
    <ul>
      <li>
        <span class="before-number">$</span>
        <span id="countupTwo" class="countup">200</span>
        <span class="after-number">+</span>
      </li>
      <li>
        <span id="countupThree" class="countup">304.7</span>
        <span class="after-number">%</span>
      </li>
      <li>
        <span class="before-number">+</span>
        <span id="countupFour" class="countup">430.32</span>
        <span class="after-number">!</span>
      </li>
    </ul>
  </div>

JavaScript

const containerObserver = new IntersectionObserver((entries) => {
    entries.forEach((entry) => {
      if (entry.isIntersecting) {
        const countupEls = entry.target.querySelectorAll(".countup");
        countupEls.forEach((el, index) => {
          setTimeout(() => {
            animateCountUp(el, 4000, 1, 0);
          }, index * 700);
        });
  
        containerThreeObserver.unobserve(entry.target);
      }
    });
  });
  
  // Observe the container div
  containerObserver.observe(
    document.getElementById("container-three")
  );

Live site with examples and documentation: https://jon424.github.io/javascript-countup-animation/