Where can I find Javascript code to make a GIF start animating on hover in Webflow?

TL;DR
  • Replace the GIF with a placeholder image, upload the GIF to Webflow, and add custom JavaScript in Project Settings to swap image sources on hover.
  • Test the effect by publishing changes and verifying that the GIF animates when hovered over, reverting to static when not.

To create a GIF animation that starts on hover in Webflow, you will need to use custom JavaScript code to manipulate the GIF's playback. Webflow does not natively support GIF hover effects, so custom coding is essential.

1. Use a Placeholder Image

  • Replace the GIF in your design with a static placeholder image that represents the first frame of your GIF.
  • Ensure the placeholder image matches the size of your actual GIF for seamless transitions.

2. Prepare the GIF Asset

  • Upload the GIF to your Webflow assets.
  • Note the URL of the GIF, as you'll need this to switch the placeholder image on hover.

3. Add Custom JavaScript

  • Go to Project Settings in Webflow.
  • Navigate to the Custom Code section, specifically under the Head or Body tag (as needed).
  • Add the following JavaScript, inside a <script> tag, that swaps the image source on hover:

  ```javascript

  document.addEventListener('DOMContentLoaded', function() {

      const gifElement = document.querySelector('.gif-hover'); // Ensure the selector matches your element

      const staticImageSrc = gifElement.getAttribute('src');

      const animatedGifSrc = 'https://your-gif-url.com/your-animation.gif';

      gifElement.addEventListener('mouseenter', function() {

          gifElement.src = animatedGifSrc;

      });

      gifElement.addEventListener('mouseleave', function() {

          gifElement.src = staticImageSrc;

      });

  });

  ```

4. Implement the Changes

  • Publish your changes to see the effect live on your website.
  • Test the hover effect by moving your pointer over the placeholder image. The GIF should animate on hover and revert to the static image when the pointer leaves.

Summary

To make a GIF animate on hover in Webflow, use a static image as a placeholder, employ JavaScript for hover effect, and switch the image source to the GIF URL on mouse events.

Rate this answer

Other Webflow Questions