How can I combine the animated JS number counter with scroll animation on Webflow and ensure it only animates when the section is in view?

TL;DR
  • Use Webflow's Interactions to apply a scroll-triggered animation to the section containing your JS number counter.
  • Implement a JS Number Counter using the CountUp.js library by including it in your site's custom code and setting a scroll trigger to activate the counter when the section comes into view.
  • Initialize and run the counter within the scroll interaction using a custom JavaScript script, ensuring it starts once the section is in view and testing to confirm proper functionality.

Combining an animated JS number counter with scroll-triggered animations in Webflow allows you to create dynamic content that engages visitors when they scroll to a specific section.

1. Use Webflow's Interactions

  • Open the Interactions panel in Webflow to access scroll animations.
  • Create a new scroll interaction and apply it to the section where your counter resides.

2. Implement a JS Number Counter

  • Use a library like CountUp.js for the number animation.
  • Include the CountUp.js library in your Webflow project by adding it in the <head> section of your site's custom code (Site Settings > Custom Code).

3. Setup Scroll Trigger

  • Add a scroll trigger interaction in Webflow that targets the same section as your JS number counter.
  • Set the trigger to activate when the section scrolls into view. You can configure this under the “Scroll into view” or “While scrolling in view” options.

4. Initialize the Counter in the Trigger

  • Within the scroll interaction, add a step to run JavaScript that initializes your CountUp.js instance.
  • Ensure your script checks the scroll position and only activates the counter if it's not already done.

5. Custom Script Execution

  • In the custom interaction step, use a script like:

  ```javascript

  var counterDone = false;

  if (!counterDone) {

    // Add your CountUp.js initiation code here

    var countUp = new CountUp('yourElementId', 1000);

    if (!countUp.error) {

      countUp.start();

      counterDone = true;

    } else {

      console.error(countUp.error);

    }

  }

  ```

  • Replace 'yourElementId' with the actual ID of the element displaying the number.

6. Test and Troubleshoot

  • Preview your site in Webflow and scroll to the section to ensure the number counter animates correctly when in view.
  • Verify that the animation doesn’t replay unless the section scrolls out of view and back into view if that’s the behavior you desire.

Summary

To combine a scroll-triggered and JS-based animated number counter on Webflow, utilize Interactions for scroll triggers, integrate CountUp.js for the number animation, and ensure JavaScript executes only when the section appears in view. This approach ensures dynamic engagement while optimizing startup performance.

Rate this answer

Other Webflow Questions