Insights and Inspiration – The Hostnicker Blog

Adding Animated Counters to Webflow Projects

January 1, 2024

Step 1: Setting Up Your Webflow Project
Begin by opening your Webflow project where you want to incorporate the animated counters. Ensure you have a designated section or container for the counters. You can either use a new section or an existing one based on where you want the counters to be visible.

Step 2: Creating the Counter Element
In the Webflow Designer, drag and drop a div block onto your canvas where you intend to place your counter. Assign this div block a class name such as "counter" for easy identification. Inside this div block, add a heading element (H1 or H2) for displaying the counter number and a paragraph element for the label. For example, set up "250" as the counter and "Projects Completed" as the label.

Step 3: Styling the Counter
Select the "counter" div block and style it according to your site’s aesthetics. Modify the font size, color, background, padding, and margins to make it visually appealing. Style the heading for the number and the paragraph for the label separately, giving each element its own distinct characteristics.

Step 4: Adding the Animation Logic with Custom Code
To animate the counter, you'll need to integrate some custom JavaScript code. Go to your project settings and navigate to the Custom Code tab. In the "Before </body> tag" section, paste the following JavaScript code:

document.addEventListener("DOMContentLoaded", function() {
const counters = document.querySelectorAll('.counter');
counters.forEach(counter => {
counter.innerText = '0';
const target = +counter.getAttribute('data-target');
const updateCounter = () => {
const current = +counter.innerText;
const increment = target / 200;
if (current < target) {
counter.innerText = Math.ceil(current + increment);
setTimeout(updateCounter, 1);
} else {
counter.innerText = target;
}
};
updateCounter();
});
});

Return to your counters in Webflow Designer, select the div containing your number, and set a custom attribute:
Name: data-target
Value: Enter the number you want the counter to animate to, such as 250 for projects completed.

Repeat this process for each counter you wish to animate, adjusting the value in the data-target attribute as needed.

Step 5: Testing Your Counter Animation
Once you've added the HTML structure and JavaScript code, test your animated counters. Click on the "Preview" button in Webflow to see them in action on the live site. If configured correctly, the numbers should animate from 0 to your designated target number once the page loads.

Step 6: Fine-Tuning Your Counters
If the animation speed feels too slow or too fast, modify the speed by adjusting the divisor value in this line of the JavaScript code:

const increment = target / 200;

Lower the divisor value to speed up the animation, or increase it to slow it down.

Step 7: Final Touches
Check your styling to ensure the counters look consistent across different devices. Use responsive design settings in Webflow to adjust the padding, margins, and font sizes, ensuring the counters appear correctly on both mobile and desktop views.

Conclusion
Animated counters in your Webflow project enhance user experience by highlighting important information in a dynamic way. Following these steps will help you create engaging counters, making your site more interactive and visually appealing. Experiment with different styles to find what works best for your project. Happy designing!