Skip to content

Motion, Transitions and Animations in Svelte

How Svelte ships motion declaratively and without an extra library: transition directives for fading in and out, svelte/motion for smoothly interpolated values and animate:flip for reordering lists.

≈ 10 min readSoftwareFrontendSvelte

A notice appears above the list, and it is simply there – abruptly, without a transition. An entry is deleted, and everything below it jumps up one line at once. In React projects, at this point at the latest, react-transition-group or an animation runtime lands in package.json. In Svelte, it takes three imports from the standard toolkit, and the dependency list stays as it was.

The three modules are designed to work together and between them cover almost everything that comes up in everyday work. If you have already understood why Svelte does without a Virtual DOM and generates code at compile time instead, you can already guess where this is heading: motion is also closely woven into the compiler – it translates the directives into code that, where possible, runs the animation through CSS keyframes instead of JavaScript per frame.

Editorial note (July 2026): The examples are written in Svelte 3 style. svelte/transition, svelte/animate and svelte/motion also exist in Svelte 5; what has mainly changed is the surrounding reactivity model. The old tutorial URLs have been replaced with today’s API pages.

Three tools, three jobs#

Before we write code, it pays to separate things cleanly in your head. The three modules have different responsibilities, and most of the misunderstandings I have come across arise from someone reaching for the wrong tool for the wrong job.

  • transition: (from svelte/transition) describes how a single element enters and leaves the DOM. It is about appearing and disappearing.
  • animate: (from svelte/animate) describes how elements move that stay in the DOM but change their position – that is, when reordering a list.
  • svelte/motion (with tweened and spring) initially has nothing to do with the DOM at all. It is about values that interpolate smoothly from one state to the next – a number, a color, a coordinate.

This three-way split is the common thread for everything that follows. As soon as it is clear whether you are making an element appear, moving it within a list or letting a value glide, the right module almost picks itself.

Transitions: fading in and out#

In Svelte, you attach a transition to an element as a directive. It is triggered as soon as the element enters or leaves the DOM through control flow – an {#if}, an {#each}. That is the crucial point: I do not call an animation; the state change does. I only describe what it looks like.

<script>
  import { fade, fly } from 'svelte/transition';
  let visible = true;
</script>

<label>
  <input type="checkbox" bind:checked={visible} />
  toggle
</label>

{#if visible}
  <p transition:fade={{ duration: 300 }}>
    fades in and out
  </p>

  <div transition:fly={{ y: 20, duration: 300 }}>
    flies in from below, flies back down on exit
  </div>
{/if}

transition: is bidirectional – the same motion runs forward when fading in and backward when fading out. That is often exactly right. But sometimes you want to be asymmetric: fly in, but fade out. For that, there are the separate directives in: and out:.

<script>
  import { fly, fade } from 'svelte/transition';
  let visible = true;
</script>

{#if visible}
  <div in:fly={{ y: 20, duration: 300 }} out:fade={{ duration: 200 }}>
    enters flying, leaves fading
  </div>
{/if}

The built-in selection is manageable and well coordinated: fade, blur, fly, slide, scale, draw and crossfade. For everyday use, the first five are almost always enough. fly takes x and y as numbers in pixels, slide opens and closes vertically, scale scales from a starting value.

One detail only became clear to me when I looked closely: an out transition takes time, and the element stays in the DOM for that duration. Svelte removes it only when the motion has finished. So if you think an element is already gone while it is still fading out, you will be surprised by briefly duplicated entries. That is not a bug but the precondition for being able to see the disappearance at all.

What the compiler makes of it internally#

The reason these transitions run without performance worries lies in how they are generated. At its core, a transition function returns a css function that produces a CSS string for a progress value t (from 0 to 1). From this function, Svelte builds real CSS keyframes. The animation thus runs through the browser’s CSS engine and does not block the main thread with JavaScript per frame.

flowchart LR
  A["State change<br/>{#if} / {#each}"] --> B["Svelte compiler"]
  B --> C["transition: / animate:<br/>directive"]
  C --> D{"css function<br/>available?"}
  D -->|yes| E["CSS keyframes<br/>off main thread"]
  D -->|no| F["tick per frame<br/>JS on main thread"]
  E --> G["DOM update"]
  F --> G

Only when a motion cannot be expressed purely in CSS – for example because you want to change text content per frame – does the second path kick in, via a tick function that actually runs in JavaScript per frame. But the default case is the performant CSS path, and you get it for free without writing keyframes yourself.

animate:flip for lists#

Let’s get to the second tool. transition: takes care of elements that come and go. But what about elements that stay and just swap places? When I sort or shuffle a list, the entries jump hard to their new position on their own. This is exactly where animate:flip comes in.

The name FLIP stands for “First, Last, Invert, Play” – a well-known technique in which the old and new positions of an element are compared and the difference is animated away. Svelte does this calculation for us. Only one condition matters: animate: works exclusively in a keyed {#each} block, that is, an {#each} with a stable key. Without this key, Svelte cannot know which element was where before.

The following example brings both tools together – transition:fly for fading new or removed entries in and out, and animate:flip for smoothly reordering the ones that stay:

<script>
  import { fly } from 'svelte/transition';
  import { flip } from 'svelte/animate';

  let items = [
    { id: 1, label: 'apples' },
    { id: 2, label: 'oranges' },
    { id: 3, label: 'bananas' },
    { id: 4, label: 'pears' }
  ];

  function shuffle() {
    items = items
      .map((item) => ({ item, sort: Math.random() }))
      .sort((a, b) => a.sort - b.sort)
      .map(({ item }) => item);
  }

  function remove(id) {
    items = items.filter((item) => item.id !== id);
  }
</script>

<button on:click={shuffle}>shuffle</button>

<ul>
  {#each items as item (item.id)}
    <li
      transition:fly={{ y: 20, duration: 300 }}
      animate:flip={{ duration: 300 }}
    >
      {item.label}
      <button on:click={() => remove(item.id)}>x</button>
    </li>
  {/each}
</ul>

The key (item.id) is not decorative here; it carries the whole mechanism. It lets Svelte recognize the same DOM <li> as the same element across the reordering and animate the difference in position. The animated element must also be a direct child of the {#each} block. If you put a wrapper in between instead and attach animate:flip to the wrong element, you simply see no motion – a classic I keep stumbling over in reviews.

By the way, combining transition and animate in the same list is intentional and well coordinated: new entries fly in, removed ones fly out and stay in the DOM briefly to do so, and all the others slide smoothly into their new places. Three motions, one declarative block.

svelte/motion: values that glide#

The third tool leaves the DOM aside at first. tweened and spring are stores – writable stores, as I described them in the article about state outside the component with Svelte stores, with one special feature: when you set their value, they do not jump to the target immediately but interpolate toward it. In the markup, you read them like any store with the $ prefix.

tweened interpolates over a fixed duration and an easing function – good for anything that should follow a clear timeline, such as a progress bar.

<script>
  import { tweened } from 'svelte/motion';
  import { cubicOut } from 'svelte/easing';

  const progress = tweened(0, {
    duration: 400,
    easing: cubicOut
  });
</script>

<progress value={$progress}></progress>

<button on:click={() => progress.set(0)}>0%</button>
<button on:click={() => progress.set(0.5)}>50%</button>
<button on:click={() => progress.set(1)}>100%</button>

spring, on the other hand, does not interpolate over a duration but via physics – stiffness and damping. The result feels more alive because it bounces slightly instead of arriving at an exact point in time. For things that should feel “natural,” such as an element that follows the mouse or reacts to input, spring is usually the nicer choice.

<script>
  import { spring } from 'svelte/motion';

  const size = spring(1, {
    stiffness: 0.1,
    damping: 0.4
  });
</script>

<div class="box" style="transform: scale({$size})">
  {$size.toFixed(2)}
</div>

<button on:click={() => size.set(0.5)}>small</button>
<button on:click={() => size.set(1)}>normal</button>
<button on:click={() => size.set(2)}>large</button>

<style>
  .box {
    width: 80px;
    height: 80px;
    background: #ff3e00;
    display: grid;
    place-items: center;
    color: white;
  }
</style>

Two details only become apparent when you work with them. First, .set() and .update() return a promise that only resolves when the interpolation has finished – so if you want to wait for a value to have “arrived,” you can await it. Second, Svelte automatically interpolates numbers, arrays and objects of numbers; for other value types, such as colors in a particular notation, you give tweened your own interpolation function via the interpolate option.

The stiffness and damping parameters can even be adjusted at runtime. You can bind them to sliders and experience live how the spring’s behavior changes. That is the fastest way to get a feel for what the two numbers actually do – faster, in any case, than guessing values in code and reloading.

Writing your own transitions#

Perhaps the nicest proof that no black box is at work here: a transition is nothing more than a function. It gets the DOM node and its parameters and returns an object that contains, among other things, a css function. This function receives the progress t from 0 to 1 as well as the counter value u, which equals 1 - t, and returns a CSS string.

function spin(node, { duration = 400 }) {
  return {
    duration,
    css: (t, u) => `
      transform: rotate(${u * 360}deg) scale(${t});
      opacity: ${t};
    `
  };
}

You can use this custom transition exactly like the built-in ones:

<script>
  import { spin } from './spin.js';
  let visible = true;
</script>

{#if visible}
  <div in:spin={{ duration: 600 }}>
    spins into view
  </div>
{/if}

Once you have written this, the rest demystifies itself. fade, fly and scale are exactly such functions – they just live in the library. There is no second, hidden mechanism. This consistency is exactly what I appreciate so much about Svelte’s motion model: from the ready-made one-liner to your own function, it is one continuous concept, not a break.

To put it in context, here are the three tools compared directly once more, so the mapping sticks for good:

  • transition: / in: / out: – for the appearing and disappearing of individual elements, triggered by {#if} or {#each}.
  • animate:flip – for elements that stay in the DOM and only swap positions in a keyed {#each}.
  • tweened / spring from svelte/motion – for interpolated values that you read as a store with $ and bind into the markup.

Conclusion#

In Svelte, motion is not an extra decision you retrofit later with a library; it is part of the language. That changes how you approach the topic. Instead of asking “Which package do I use?”, you ask “Is something appearing here, is something moving, or is a value gliding?” – and the answer leads directly to transition:, animate:flip or svelte/motion.

The common denominator is the declarative approach: I describe what a motion looks like, and the compiler makes sure it is triggered when the state changes and that it runs performantly via CSS keyframes where possible. Because custom transitions are the same simple functions as the built-in ones, the whole thing can be learned right down to the bottom. For everyday project work, this means: fewer dependencies, less boilerplate and more time for the question of which motion really makes a UI easier to understand – instead of the question of which framework makes it possible at all.

Further reading#

Revised and translated from German with the help of AI.