Forward & Reverse Hover Animations with dotLottie

Hover magic ✨ Cards come alive with dotLottie where it goes forward when you hover, reverse when you leave. Hope this is useful!

Step 1 - Add your canvases

Each card is just a <canvas> with a unique id.

<canvas id="electronics" width="220" height="220"></canvas>
<canvas id="grocery" width="220" height="220"></canvas>
<canvas id="music" width="220" height="220"></canvas>
<canvas id="clothes" width="220" height="220"></canvas>

Step 2 - Import dotLottie Web

Use an ES module import (works great if placed inside a <script type="module">).

<script type="module">
  import { DotLottie } from "https://esm.sh/@lottiefiles/dotlottie-web@0.50.0";
  // JS from steps 3–4 goes here
</script>

Step 3 - Wire up a helper

Create a tiny function that mounts a dotLottie instance on a canvas and attaches the hover logic.

  • mouseenter: set mode to forward, speed 0.8, play

  • mouseleave: set mode to reverse, speed 2, play

function setupCardAnimation(id, url) {
  const canvas = document.getElementById(id);
  if (!canvas) return;

  let dotLottie = null, onEnter = null, onLeave = null;

  function load() {
    if (dotLottie) { dotLottie.destroy(); dotLottie = null; }
    if (onEnter) canvas.removeEventListener("mouseenter", onEnter);
    if (onLeave) canvas.removeEventListener("mouseleave", onLeave);

    setTimeout(() => {
      dotLottie = new DotLottie({ canvas, src: url, loop: false, autoplay: false });
      dotLottie.addEventListener("load", () => {
        onEnter = () => { dotLottie.setMode("forward"); dotLottie.setSpeed(0.8); dotLottie.play(); };
        onLeave = () => { dotLottie.setMode("reverse"); dotLottie.setSpeed(2);   dotLottie.play(); };
        canvas.addEventListener("mouseenter", onEnter);
        canvas.addEventListener("mouseleave", onLeave);
      });
    }, 100);
  }
  load();
}

Step 4 - Point each canvas to an animation

Pass your canvas id and the .lottie URL.

setupCardAnimation("electronics","https://lottie.host/b514b814-9d44-4bf7-a931-0007de7f37f9/pAxq3RSO2d.lottie");
setupCardAnimation("grocery","https://lottie.host/a8b19e71-1e2f-4ec9-843e-6b63e85b3eb6/Qs8Kt3kEfi.lottie");
setupCardAnimation("music","https://lottie.host/cdca5368-6750-4a7d-b017-eabc4b4cb84b/MOormyvOkx.lottie");
setupCardAnimation("clothes","https://lottie.host/35bd9df4-006d-4121-9f3c-faaa01f7bb1e/dQ5lUfYDgH.lottie");

Step 5 - (Nice to have) Basic styling

Make it feel “card-y” and give a soft hover background.

canvas { display:block; width:220px; height:220px; cursor:pointer; }
.card:hover { background:#f6f6f6; }