How can I animate live stat numbers in Webflow using counterup2.js or a similar tool without animating the static numbers that are replaced?

Published on
September 22, 2023

To animate live stat numbers in Webflow using counterup2.js or a similar tool without animating the static numbers that are replaced, follow these steps:

  1. Prepare your static number: First, you need to set up a static number in Webflow to serve as the starting point for the animation. This number will be replaced by the live stat number later. You can use a text element or a number element to display the static number.

  2. Add custom code: Open the page settings panel in Webflow and go to the custom code section. Ensure that you select the page or specific page template where you want the animation to occur. In the <head> section, add the link to counterup2.js by copying and pasting the following code:

<script src="https://cdnjs.cloudflare.com/ajax/libs/counterup2/1.0.0/jquery.counterup.min.js"></script>
  1. Create a new script: Click the "+" button in the custom code section to add a new code block. Choose "Add a <script> tag" from the options. Inside the script tags, add the following code:
<script>$(document).ready(function() {  $('.stat-number').counterUp({    delay: 10, // the delay in milliseconds    time: 1000 // the animation duration in milliseconds  });});</script>
  1. Assign a class: Assign a class to the element that contains the static number by selecting the element and going to the settings panel on the right side of the editor. Under the "Settings" tab, find the "Class" section and enter a class name. Let's say we assign the class name "stat-number" to the element.

  2. Add dynamic data: Replace the static number with the live stat number using Webflow's dynamic data binding feature. You can do this by connecting the element to a collection or by using custom code with Webflow's CMS API.

  3. Styling and further customization: Lastly, you can style the live stat number according to your design preferences using Webflow's options. You can also customize the animation options by modifying the delay and time values in the script code.

By following these steps, you should be able to animate the live stat numbers in Webflow using counterup2.js or a similar tool without affecting the static numbers that are being replaced.

Example code:
HTML:

<div class="stat-number">1000</div>

JavaScript:

$(document).ready(function() {  $('.stat-number').counterUp({    delay: 10,    time: 1000  });});

Additional Questions:

  1. How do I add custom code to a Webflow project?
  2. How can I connect an element to a collection in Webflow?
  3. Can I customize the animation style of counterup2.js in Webflow?