How can I trigger typed.js based on scroll position or when an element becomes visible?

Published on
September 22, 2023

To trigger Typed.js based on scroll position or when an element becomes visible, you can use JavaScript in conjunction with Webflow's Interactions. Here's how you can achieve this:

  1. Add the Typed.js library:
  • Go to the Typed.js website (https://mattboldt.com/demos/typed-js/).
  • Click on the "Download" button to get the latest version.
  • Extract the downloaded package and locate the typed.min.js file.
  • Upload the typed.min.js file to your Webflow project's Assets Manager.
  1. Create a new JavaScript file:
  • In your Webflow project, go to the Designer and navigate to the "Pages" tab.
  • Find the page where you want to trigger Typed.js and click on the gear icon to open the settings.
  • Scroll down to the "Custom Code" section and click on the "Add File" button.
  • Upload a new JavaScript file (e.g., typed-trigger.js) and click on the "Add" button.
  1. Write the JavaScript code:
  • Open the typed-trigger.js file and start by adding the Typed.js initialization code.
    ```javascript
    const options = {
    strings: ['Hello', 'World'],
    typeSpeed: 50,
    loop: true
    };
    const typed = new Typed('.typed-element', options);
    ```
    In this example, the text will be typed out as "Hello" and "World" with a typing speed of 50 milliseconds per character. The loop option allows continuous typing.

  • Next, we'll create a function to check if the element is in the viewport.
    ```javascript
    function isInViewport(element) {
    const rect = element.getBoundingClientRect();
    return (
    rect.top >= 0 &&
    rect.left >= 0 &&
    rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
    rect.right <= (window.innerWidth || document.documentElement.clientWidth)
    );
    }
    ```

  • Lastly, we'll check if the element is visible on scroll and trigger the Typed.js animation accordingly.
    ```javascript
    const element = document.querySelector('.trigger-element');

    document.addEventListener('scroll', function() {
    if (isInViewport(element)) {
    typed.start();
    } else {
    typed.stop();
    }
    });
    ```
    In this code snippet, we select the element that will trigger Typed.js (assign the appropriate class name to .trigger-element). The scroll event listener checks if the element is in the viewport using the isInViewport() function and decides whether to start or stop the Typed.js animation.

  1. Implement the trigger element in Webflow:
  • In the Designer, add a new element (e.g., a section or a text element) to the page.
  • Assign the appropriate class name (e.g., .trigger-element) to the element that will trigger the Typed.js animation.
  • Add the class .typed-element to the element where you want the text to be typed.
  1. Publish your project:
  • Make sure to publish your project for the changes to take effect on the live site.
  • To test if everything is working correctly, open your published website and scroll to the trigger element. The Typed.js animation should start when it becomes visible.

By following these steps, you can trigger Typed.js based on scroll position or when an element becomes visible on your Webflow site.

Additional Questions:

  1. How do I integrate the Typed.js library into my Webflow project?
  2. Can I customize the typing speed and animation of Typed.js?
  3. Are there any alternative libraries to Typed.js that can achieve similar effects in Webflow?