Documentation Shento Hendriks
Svelte / SvelteKit
Svelte / SvelteKit
  • Getting Started with SvelteKit
    • Installing SvelteKit
      • Tailwind Typography plugin
    • Base Syntax & Core Features of Svelte
      • Curly Braces & Understanding Core Syntax
      • Reactive Variables
      • Two-way-binding Shortcut
      • Using multiple components
      • Components & Communication via Props
      • Display HTML in Svelte
      • Dynamic classes
      • Using $effect
      • Working with Javascript and Rune reactivity
    • Working with Conditionals & Loops
      • Showing code conditionally in Svelte
      • Looping through arrays (lists)
      • Lists & Keys
    • Closer look at reactivity
      • Updating Arrays & Objects
      • Event Modifiers
    • Component Events
    • Best Practices
    • Adding Google Font in Tailwind and SvelteKit
    • Adding Local Fonts to SvelteKit with Tailwind
    • Prevent flicker in images
    • Enhanced Images
    • Form Submissions in SvelteKit
    • Effective SEO in SvelteKit
    • Automatic Site Maps in Svelte
    • Creating Custom Elements with Svelte, SvelteKit, and Vite
    • Creating a Svelte Library with JavaScript Functions
  • Useful Code Snippets
    • Hover effects
    • Navigation
    • Centering Items
    • Setting Footer Bottom Page
  • Installing Useful Libraries
    • Documenting Javascript with JSDocs (Crash Course)
    • Adding Lottie Animations to Svelte Application
    • SvelteKit Melt-ui
      • First Component - accordion
    • Installing Shadcn/ui Svelte
    • Getting Started with GSAP with SvelteKit
      • What is GSAP?
      • Installing GSAP & Your first animation
      • GSAP in SvelteKit
    • Installing Locomotive Scroll with Svelte 5 and SvelteKit
  • SvelteKit Server Fetching and Deployment
    • Preparing a SvelteKit application for Deployment
    • Utility Functions for URL Handling in SvelteKit
    • Fetching Directus Data from Rest API with SvelteKit
    • Fetching data from api with caching
    • Displaying Fetched Data in a Each Loop
    • Creating slug friendly urls based on query data
    • Finding correct query in api based on url slug
Powered by GitBook
On this page
  • Creating Animations
  • Installing Lottie
  • Adding Lottie
  • An example of a lottie animation that'll play when clicked (hamburger menu animation) and reverse, when clicked again
  1. Installing Useful Libraries

Adding Lottie Animations to Svelte Application

Ever animated something cool in Figma or Adobe After Effects? It's possible to implement those animations in a friendly web format with Lottie.

Creating Animations

You can create Lottie Animations in Figma by installing the LottieFiles Figma Plugin, then export your Flow.

Installing Lottie

npm install lottie-web

Adding Lottie

Lottie should only be initialised, after the DOM is mounted. In Svelte we can use this by using the $effect() (Svelte 5) or the onMount() function (Svelte 4), for the sake of this tutorial, I will create it in Svelte 4, but I will update it later to Svelte 5.

An example of a lottie animation that'll play when clicked (hamburger menu animation) and reverse, when clicked again

<script>
  import lottie from 'lottie-web';
  import { onMount } from 'svelte';
  export let path;
  export let loop = false;
  export let autoplay = false;
  export let speed = 1;

  let animationContainer;
  let animation;
  let isForward = true;

  onMount(() => {
    animation = lottie.loadAnimation({
      container: animationContainer,
      renderer: 'svg',
      loop: loop,
      autoplay: autoplay,
      path: path
    });
    animation.setSpeed(speed); // Set the initial speed
  });

  function toggleAnimation() {
    if (isForward) {
      animation.setDirection(1); // Play forward
    } else {
      animation.setDirection(-1); // Play in reverse
    }
    animation.play();
    isForward = !isForward; // Toggle the direction
  }
</script>

<button bind:this={animationContainer} class="lottie-container" on:click={toggleAnimation}></button>
<script>
	import LottieAnimation from '$lib/components/LottieAnimation.svelte';
</script>

<div class="w-10">
    <LottieAnimation path="src/lib/hamburger-animation.json" speed={2} />
</div>
PreviousDocumenting Javascript with JSDocs (Crash Course)NextSvelteKit Melt-ui

Last updated 9 months ago