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 formattingNo components allowed here!
MDX (.mdx):
import MyComponent from './MyComponent.astro';
# Text with interactive components!<MyComponent prop="value" />
How it renders in Astro:
- Astro reads the
.mdx
file at build time - Processes the frontmatter (metadata at the top)
- Imports any components you specify
- Converts markdown to HTML
- Renders Astro components as real DOM elements
- 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:
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 likesrc="/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
Prop | Type | Default | Description |
---|---|---|---|
src | string | required | URL or path to Lottie JSON file |
width | string | ”300px” | Width of the animation container |
height | string | ”300px” | Height of the animation container |
autoplay | boolean | true | Start playing automatically |
loop | boolean | true | Loop the animation |
controls | boolean | false | Show 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:
- Download a Lottie JSON from LottieFiles (click the download button)
- Place it in your
public/animations/
folder - 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:
- Go to LottieFiles.com
- Search for “cheerleader” or “celebration”
- Preview animations by hovering over them
- Click on the one you like
- Download the JSON file (look for the download button)
- Save it to
public/animations/cheerleader.json
- 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!