510 words
3 minutes
Adding Lottie Animations to Your Site

Adding Lottie Animations to Your Site#

Lottie animations are lightweight, scalable animations that can bring your website to life. In this post, I’ll show you how I built a simple vanilla JavaScript Lottie component for Astro.

What are Lottie Animations?#

Lottie is a library by Airbnb that renders After Effects animations in real-time. They’re vector-based, so they scale perfectly and are much smaller than GIF or video files.

Why This Post Uses MDX#

You might notice this file is .mdx instead of .md - here’s why:

What is MDX? MDX is Markdown + JSX (or in our case, Astro components). It lets you import and use interactive components directly inside your markdown content.

Regular Markdown (.md):

# Just text and formatting
No components allowed here!

MDX (.mdx):

import MyComponent from './MyComponent.astro';
# Text with interactive components!
<MyComponent prop="value" />

How it renders in Astro:

  1. Astro reads the .mdx file at build time
  2. Processes the frontmatter (metadata at the top)
  3. Imports any components you specify
  4. Converts markdown to HTML
  5. Renders Astro components as real DOM elements
  6. Outputs static HTML with embedded JavaScript for interactivity

This is how the <Lottie> component below renders as actual animated SVG instead of just plain text!

To use MDX in Astro:

Terminal window
pnpm add @astrojs/mdx

Then add to astro.config.mjs:

import mdx from '@astrojs/mdx';
export default defineConfig({
integrations: [mdx()],
// ...
});

The Component#

I created a reusable Astro component that:

  • Loads Lottie animations from a JSON file
  • Supports autoplay and loop options
  • Includes optional playback controls
  • Uses pure vanilla JavaScript (no framework dependencies)

Usage Example#

Here’s how simple it is to use:

---
import Lottie from '../../components/Lottie.astro';
---
<Lottie
src="/animations/my-animation.json"
width="400px"
height="400px"
autoplay={true}
loop={true}
controls={true}
/>

Live Demo#

Below is a Lottie animation in action. You can use any Lottie JSON file from LottieFiles:

Note: Some Lottie hosting services have CORS restrictions. For best results, download the JSON file and place it in your public/ folder, then use a local path like src="/animations/my-animation.json".

Component Features#

  • Vanilla JavaScript: No React, Vue, or other framework needed
  • CDN Loading: Automatically loads lottie-web from CDN
  • Responsive: Easy to customize width and height
  • Controls: Optional play/pause and stop buttons
  • Multiple Instances: Can have multiple animations on one page

Props#

PropTypeDefaultDescription
srcstringrequiredURL or path to Lottie JSON file
widthstring”300px”Width of the animation container
heightstring”300px”Height of the animation container
autoplaybooleantrueStart playing automatically
loopbooleantrueLoop the animation
controlsbooleanfalseShow play/pause controls

Where to Find Lottie Animations#

Browse thousands of free and premium Lottie animations at LottieFiles.com. Search by category, download the JSON, and use it in your project!

Using Local Lottie Files#

The best practice is to download Lottie JSON files and store them locally to avoid CORS issues:

  1. Download a Lottie JSON from LottieFiles (click the download button)
  2. Place it in your public/animations/ folder
  3. Reference it with a local path:
<Lottie
src="/animations/my-animation.json"
width="300px"
height="300px"
/>

Here’s an example using a local animation:

Finding a Specific Animation#

Want a cheerleader animation? Here’s how to find one:

  1. Go to LottieFiles.com
  2. Search for “cheerleader” or “celebration”
  3. Preview animations by hovering over them
  4. Click on the one you like
  5. Download the JSON file (look for the download button)
  6. Save it to public/animations/cheerleader.json
  7. Use it in your post:
<Lottie
src="/animations/cheerleader.json"
width="400px"
height="400px"
autoplay={true}
loop={true}
controls={true}
/>

Some popular cheerleader-related searches:

  • “cheerleader” - Direct cheerleader animations
  • “celebration” - Confetti, party poppers
  • “dancing” - Dancing characters
  • “sports” - Athletic animations
  • “victory” - Success/winning animations

Next Steps#

You could extend this component with:

  • Speed controls
  • Progress bar
  • Event callbacks (onComplete, onLoop, etc.)
  • Hover to play/pause
  • Intersection Observer for scroll-triggered animations

Happy animating!

Adding Lottie Animations to Your Site
https://fuwari.vercel.app/posts/lottie-animations/
Author
ghostrunners
Published at
2025-10-18
License
CC BY-NC-SA 4.0