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
- 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:{{}} 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:Milliseconds before the animation starts
Duration of the animation in milliseconds
An easing function that transforms the animation timeline
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.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 receivesfrom and to parameters containing DOMRect objects:
from- The element’s bounding rectangle before reorderingto- The element’s bounding rectangle after reordering
left, top, width, height, and more.
Animation Timeline
Thet and u parameters in css and tick functions represent the animation progress:
t- Progress from 0 to 1, with easing appliedu- Equals1 - t, useful for inverting values
Using css vs tick
Prefer the css option when possible:
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.