Home / Learn / How do I add and control a Lottie icon on my site?

How do I add and control a Lottie icon on my site?

Lottie is a JSON format exported from After Effects and played by a small player in the page, so the designer makes the motion and the developer does not rebuild it in code. This article teaches loading and control (play, pause, on hover) with the official lottie-web player, and shows the newer dotLottie player as an alternative.

Steps

  1. Get the animation file: export it from After Effects with the Bodymovin plugin, or download a ready one (JSON or .lottie).
  2. Get lottie.js from the build/player folder of the official repo, or its official CDN listing, host it yourself, and include it with a script tag.
  3. Call lottie.loadAnimation with: container (the element), renderer ("svg"), loop, autoplay, and either animationData (the object) or path (the JSON file path). Set autoplay: false so you decide when it plays.
  4. The call returns an object you control: play(), pause(), setSpeed() and goToAndStop(frame, true). Wire it to mouseenter, mouseleave and click.
  5. For accessibility, pass rendererSettings.title as the SVG title, and under prefers-reduced-motion skip autoplay and show only the first frame.
  6. For size: a bodymovin JSON is smaller than a GIF, and lottie.js should be gzipped for production. If you choose dotLottie, the .lottie format is a compressed file that bundles several animations and their assets.

Sources

The code

HTML

<!doctype html>
<html lang="ar" dir="rtl">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
  body { margin: 0; min-height: 100vh; display: grid; place-content: center; justify-items: center; gap: .8rem; background: #0f172a; color: #fff; font-family: system-ui, "Segoe UI", Tahoma, sans-serif; }
  #icon { width: 140px; height: 140px; cursor: pointer; }
  button { font: inherit; padding: .4rem 1rem; }
</style>
<div id="icon" role="img" aria-label="أيقونة متحركة"></div>
<button id="btn">تشغيل / إيقاف</button>
<!-- Get lottie.js from the official repo (build/player folder) or its CDN, and host it yourself -->
<script src="lottie.min.js"></script>
<script>
  // a tiny hand-written animation (pulsing circle), so this file needs no other download.
  // In real work, export your own JSON from After Effects with Bodymovin, or use a path: 'data.json' instead.
  const k = (t, s) => ({ t, s, i: { x: [0.5, 0.5, 0.5], y: [1, 1, 1] }, o: { x: [0.5, 0.5, 0.5], y: [0, 0, 0] } });
  const animationData = { v: '5.7.0', fr: 30, ip: 0, op: 60, w: 100, h: 100, nm: 'pulse', ddd: 0, assets: [], layers: [{
    ddd: 0, ind: 1, ty: 4, nm: 'dot', sr: 1, ao: 0, ip: 0, op: 60, st: 0, bm: 0,
    ks: { o: { a: 0, k: 100 }, r: { a: 0, k: 0 }, p: { a: 0, k: [50, 50, 0] }, a: { a: 0, k: [0, 0, 0] },
          s: { a: 1, k: [k(0, [100, 100, 100]), k(30, [150, 150, 100]), { t: 60, s: [100, 100, 100] }] } },
    shapes: [{ ty: 'gr', it: [
      { ty: 'el', p: { a: 0, k: [0, 0] }, s: { a: 0, k: [40, 40] }, d: 1, nm: 'circle' },
      { ty: 'fl', c: { a: 0, k: [1, 0.42, 0.3, 1] }, o: { a: 0, k: 100 }, r: 1, nm: 'fill' },
      { ty: 'tr', p: { a: 0, k: [0, 0] }, a: { a: 0, k: [0, 0] }, s: { a: 0, k: [100, 100] }, r: { a: 0, k: 0 }, o: { a: 0, k: 100 }, sk: { a: 0, k: 0 }, sa: { a: 0, k: 0 } }
    ], nm: 'group' }]
  }] };

  const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches;
  const anim = lottie.loadAnimation({
    container: document.getElementById('icon'),
    renderer: 'svg',
    loop: true,
    autoplay: false,                    // we decide when it plays
    animationData,
    rendererSettings: { title: 'أيقونة متحركة' }   // accessible title for the SVG
  });
  let playing = false;
  function toggle() { playing ? anim.pause() : anim.play(); playing = !playing; }
  document.getElementById('btn').onclick = () => { if (!reduce) toggle(); };
  document.getElementById('icon').addEventListener('mouseenter', () => { if (!reduce && !playing) toggle(); });
  document.getElementById('icon').addEventListener('mouseleave', () => { if (playing) toggle(); });
  anim.addEventListener('DOMLoaded', () => anim.goToAndStop(0, true));   // show the first frame while idle (and always, for reduced motion)
  window.__lottie = anim;             // test hook, safe to delete
</script>
</html>

Alternative: the newer dotLottie player (also a standalone file, tested in Chrome with internet access):

<!doctype html>
<html lang="ar" dir="rtl">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
  body { margin: 0; min-height: 100vh; display: grid; place-content: center; justify-items: center; gap: .8rem; background: #0f172a; color: #fff; font-family: system-ui, "Segoe UI", Tahoma, sans-serif; }
  canvas { width: 160px; height: 160px; cursor: pointer; }
</style>
<canvas id="c" role="img" aria-label="أيقونة متحركة"></canvas>
<script type="module">
  import { DotLottie } from 'https://cdn.jsdelivr.net/npm/@lottiefiles/dotlottie-web/+esm';
  const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches;
  const player = new DotLottie({
    canvas: document.getElementById('c'),
    src: 'animation.json',            // a .lottie or .json file you host yourself
    loop: true,
    autoplay: false,
  });
  player.addEventListener('load', () => player.setFrame(0));          // idle on the first frame
  const c = document.getElementById('c');
  c.addEventListener('mouseenter', () => { if (!reduce) player.play(); });
  c.addEventListener('mouseleave', () => player.pause());
  window.__dl = player;
</script>
</html>

Note: the demo above runs lottie-web from a lottie.min.js file hosted on this site, with no external link. The alternative dotLottie code loads its player from a CDN (jsdelivr), so we do not run it inside the page and show it as code only.

can't find the motion you need? Request it here

Related articles: