Menu

1 / 2

How to Use Lottie Animations in Svelte and SvelteKit

DEV Community: webdev·Fazal Shah·5 months ago
#9zuPmftN
#dev#lottie#animation#svelte#article
Reading 0:00
15s threshold

Svelte's onMount lifecycle hook is perfect for integrating Lottie — it guarantees DOM availability before the animation loads. Here's how to set it up.

Get a Lottie File

Download a free animation from IconKing — no login needed, just preview and download .json or .lottie files. Place it in your static/animations/ folder (SvelteKit) or public/animations/ folder.

Install lottie-web

npm install lottie-web

Enter fullscreen mode

Exit fullscreen mode

Basic Svelte Component

Create src/lib/LottiePlayer.svelte :

<script> import { onMount , onDestroy } from " svelte " ; import lottie from " lottie-web " ;

export let src = " /animations/default.json " ; export let loop = true ; export let autoplay = true ; export let width = " 200px " ; export let height = " 200px " ;

let container ; let animation ;

onMount (() => { animation = lottie . loadAnimation ({ container , renderer : " svg " , loop , autoplay , path : src , }); });

onDestroy (() => animation ?.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.