flip function creates smooth animations when elements change position in a list. It implements the FLIP (First, Last, Invert, Play) technique to efficiently animate layout changes.
Usage
Theflip animation is used with the animate: directive on elements inside a keyed each block. When the list is reordered, elements smoothly transition to their new positions.
Parameters
You can customize the animation by passing parameters:number
default:"0"
Milliseconds before the animation starts
number | ((len: number) => number)
default:"(d) => Math.sqrt(d) * 120"
Either a number of milliseconds, or a function that receives the distance (in pixels) the element will travel and returns the duration in milliseconds. The default function creates animations that feel natural regardless of distance traveled.
(t: number) => number
default:"cubicOut"
An easing function that controls the animation’s rate of change over time
How FLIP Works
FLIP is a performance technique that creates smooth animations by:- First: Capture the initial position of the element
- Last: Capture the final position after the DOM updates
- Invert: Calculate the difference and apply a transform to make the element appear in its starting position
- Play: Animate from the inverted state back to the natural position
Implementation Details
Theflip function:
- Calculates the translation needed based on the element’s starting and ending positions
- Accounts for scale changes if the element’s size changes
- Respects the element’s
transform-origin - Handles parent transforms and CSS zoom correctly
- Returns an animation configuration with CSS transforms