Can you initiate a Lottie animation when hovering over an element that is not a Lottie element in Webflow?
Published on
September 22, 2023
Yes, it is possible to initiate a Lottie animation when hovering over an element that is not a Lottie element in Webflow. This can be achieved by using custom code and interactions. Here's a step-by-step guide:
- Prepare your Lottie animation:
- Create or obtain the Lottie animation file that you want to use.
- Save the file in JSON format.
- Add a Lottie container:
- Drag and drop a div block from the Elements panel to the desired location on your page.
- Give the div block a class name, for example, "lottie-container".
- Upload the Lottie animation:
- Go to the Assets panel in the Webflow Designer.
- Click the "Upload" button and select your Lottie animation JSON file.
- Once uploaded, click on the Lottie animation file in the Assets panel to get the asset URL.
- Insert the Lottie animation:
- Double-click the div block to open the settings for that element.
- In the settings panel, click the "+ Add Field" button.
- Choose "Lottie" from the list of field types.
- Paste the asset URL of the Lottie animation into the URL field.
- Adjust the width and height of the Lottie container as needed.
- Create a hover interaction:
- Select the element that you want to trigger the animation on hover.
- In the Interactions panel, click the "+ New Interaction" button.
- Choose the "Mouse Hover" trigger.
- Add a new action by clicking the "+ Add New Action" button.
- Set the action to "Run JavaScript".
- Add the custom code:
- In the JavaScript Action window, click on the "Open Code Editor" button.
- Copy and paste the following code into the code editor:
const lottieContainer = document.querySelector('.lottie-container');const lottieAnimation = bodymovin.loadAnimation({ container: lottieContainer, renderer: 'svg', loop: false, autoplay: false, path: 'URL_TO_YOUR_LOTTIE_JSON_FILE',});lottieContainer.addEventListener('mouseenter', () => { lottieAnimation.play();});lottieContainer.addEventListener('mouseleave', () => { lottieAnimation.stop();});
Make sure to replace 'URL_TO_YOUR_LOTTIE_JSON_FILE'
with the correct URL of your Lottie animation JSON file.
- Test the hover animation:
- Preview your page to test the hover animation.
- When you hover over the element, the Lottie animation should play, and when you move the cursor away, the animation should stop.
By following these steps, you can initiate a Lottie animation when hovering over an element that is not a Lottie element in Webflow.
Additional SEO questions:
- Can Lottie animations improve SEO on my Webflow site?
- How can I optimize Lottie animations for SEO in Webflow?
- What are the best practices for using Lottie animations in terms of SEO?