Is there a way to trigger the animation with anime.js only on the currently visible .h2 heading in Webflow?

Published on
September 22, 2023

Yes, it is possible to trigger an animation using anime.js only on the currently visible .h2 heading in Webflow. Here's how you can achieve this:

  1. Add a class to the .h2 heading elements you want to animate. Let's say you add a class called "animate-heading".

  2. In your Webflow project, go to the page where you want to trigger the animation.

  3. Open the custom code section for the page. You can access this by clicking on the gear icon in the Pages panel on the left side of the Webflow Designer and selecting "Custom Code".

  4. In the "Before " section, add the following code snippet:

<script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js"></script><script>  // Detect if the heading element is in the viewport  function isElementVisible(element) {    var rect = element.getBoundingClientRect();    return (      rect.top >= 0 &&      rect.left >= 0 &&      rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&      rect.right <= (window.innerWidth || document.documentElement.clientWidth)    );  }  // Trigger the animation when the heading element is in the viewport  function animateVisibleHeading() {    var headings = document.querySelectorAll('.animate-heading');    for (var i = 0; i < headings.length; i++) {      if (isElementVisible(headings[i])) {        anime({          targets: headings[i],          translateY: 100,          opacity: 1,          duration: 1000,          delay: 200 * i        });      }    }  }  // Call the animateVisibleHeading function on page load and window scroll  document.addEventListener('DOMContentLoaded', animateVisibleHeading);  window.addEventListener('scroll', animateVisibleHeading);</script>
  1. Publish your project and test the animation. When the .h2 heading element becomes visible in the viewport while scrolling, the anime.js animation will be triggered. Adjust the animation properties in the code snippet (e.g., translateY, opacity, duration, delay) to fit your desired effect.

By implementing this code, you can trigger the animation using anime.js only on the currently visible .h2 heading in Webflow.

Additional questions users may ask:

  1. How can I animate a specific element in Webflow using anime.js?
  2. Can I use anime.js to trigger animations based on user interactions in Webflow?
  3. What are some alternatives to anime.js for animating elements in Webflow?