Skip to main content
The animate: directive triggers animations when elements are reordered within a keyed each block. Unlike transitions, animations are triggered by position changes, not element addition or removal.

Basic Usage

Animations must be used on immediate children of a keyed each block:

When Animations Trigger

Animations run when:
  • An element’s position changes within a keyed each block
  • The list is reordered or sorted
  • Items are rearranged in the data array
Animations do not run when:
  • Elements are added to the list (use in: transitions)
  • Elements are removed from the list (use out: transitions)
  • The each block is not keyed

With Parameters

Pass parameters to customize the animation behavior:
The double curly braces {{}} represent an object literal inside an expression tag.

Built-in Animation

Svelte provides one built-in animation function:
  • flip - Smoothly animates position changes using the FLIP technique

Custom Animation Functions

Create custom animations by providing a function that returns an animation configuration:

Animation Function Signature

Animation Configuration

Your animation function should return an object with these properties:
delay
number
default:"0"
Milliseconds before the animation starts
duration
number
default:"300"
Duration of the animation in milliseconds
easing
(t: number) => number
default:"linear"
An easing function that transforms the animation timeline
css
(t: number, u: number) => string
A function that returns CSS to apply at each frame. t is a value from 0-1 after easing is applied, u equals 1 - t. If provided, Svelte creates a web animation that runs off the main thread for better performance.
tick
(t: number, u: number) => void
A function called on each frame with t and u values. Use this when you need to imperatively update the DOM, but prefer css when possible as it performs better.

Position Information

The animation function receives from and to parameters containing DOMRect objects:
  • from - The element’s bounding rectangle before reordering
  • to - The element’s bounding rectangle after reordering
These provide properties like left, top, width, height, and more.

Animation Timeline

The t and u parameters in css and tick functions represent the animation progress:
  • t - Progress from 0 to 1, with easing applied
  • u - Equals 1 - t, useful for inverting values
The function is called repeatedly before and during the animation with different values.

Using css vs tick

Prefer the css option when possible:
Use tick only when CSS animations aren’t sufficient:
Web animations created with css can run off the main thread, preventing jank on slower devices. Use tick only when necessary.

Complete Example

Here’s a sortable todo list with animations:

TypeScript