The Animate directive in Svelte helps you smoothly change the position of elements when the layout changes. It makes your app look polished and easy on the eyes.
Animate directive in Svelte
<div animate:flip>...</div>
animate:flip on elements that change position to animate their movement.<div animate:flip>{item}</div><ul>{#each items as item (item.id)}
<li animate:flip>{item.name}</li>
{/each}</ul>This example shows a list of fruits. When you click the Shuffle button, the list order changes randomly. The animate:flip directive makes the list items move smoothly to their new positions instead of jumping abruptly.
<script> let items = ['Apple', 'Banana', 'Cherry']; function shuffle() { items = items .map(value => ({ value, sort: Math.random() })) .sort((a, b) => a.sort - b.sort) .map(({ value }) => value); } </script> <button on:click={shuffle}>Shuffle</button> <ul> {#each items as item (item)} <li animate:flip>{item}</li> {/each} </ul>
The animate:flip directive only works on elements that have a stable key in an {#each} block.
It uses the FLIP technique (First, Last, Invert, Play) to animate position changes efficiently.
Works best for layout changes, not for animating colors or opacity.
Animate directive makes elements move smoothly when their position changes.
Use it with animate:flip on elements inside keyed {#each} blocks.
Great for animating list reorderings or layout shifts without extra code.
Practice
animate:flip directive in Svelte?Solution
Step 1: Understand what
Theanimate:flipdoesanimate:flipdirective animates elements smoothly when their position changes, especially in keyed {#each} blocks.Step 2: Compare options with the directive's purpose
Options A, C, and D describe other behaviors like fading, event handling, or styling, which are not the role ofanimate:flip.Final Answer:
To smoothly animate elements when their position changes in the DOM. -> Option CQuick Check:
Animate directive = smooth position change animation [OK]
- Confusing animate:flip with transition or event directives
- Thinking it adds fade or color effects
- Assuming it handles user interactions
Solution
Step 1: Recall the correct directive syntax in Svelte
Svelte uses the syntaxanimate:flipto apply the flip animation as a directive on elements.Step 2: Check each option's syntax
- {item} uses
Final Answer:
<li animate:flip>{item}</li> -> Option AQuick Check:
Directive syntax = animate:flip [OK]
animate:flip correctly. Options A, B, and D use incorrect syntax such as use:animate, hyphenated directive, or object notation which are invalid here.- Using use:animate instead of animate:flip
- Trying to pass flip as an object
- Hyphenating directive names
{`
-
{#each items as item (item)}
- {item} {/each}
Solution
Step 1: Understand the use of animate:flip with keyed each block
The list uses a keyed {#each} block withanimate:flipon eachli. When the array order changes, the directive animates the position changes smoothly.Step 2: Analyze the shuffle function effect
Clicking the button shuffles the items array randomly, causing the list order to change. Theanimate:flipdirective animates the movement of items to their new positions.Final Answer:
The list items will smoothly move to their new positions after shuffling. -> Option AQuick Check:
animate:flip + keyed list = smooth reorder animation [OK]
- Ignoring the keyed block requirement
- Expecting fade instead of position animation
- Thinking the list instantly changes without animation
{`
-
{#each numbers as num}
- {num} {/each}
Solution
Step 1: Check the use of animate:flip in a list
Foranimate:flipto animate position changes in a list, the {#each} block must be keyed to track items uniquely.Step 2: Identify missing key in the {#each} block
The code uses {#each numbers as num} without a key. This prevents Svelte from tracking item positions correctly, so animation won't work as expected.Final Answer:
The {#each} block is missing a key, so animate:flip won't work properly. -> Option DQuick Check:
Animate directive needs keyed each block [OK]
- Forgetting to add a key in {#each}
- Confusing animate with transition directives
- Thinking import of flip is optional or wrong
animate:flip directive to ensure smooth animations when tasks move or disappear?{`
showDone = !showDone}>Toggle Done
-
{#each tasks.filter(t => showDone || !t.done) as task (task.id)}
- {task.text} {/each}
Solution
Step 1: Analyze the use of filtering and keyed each block
The code filters tasks inline and uses a keyed {#each} block withtask.idas key, which is correct for tracking items uniquely.Step 2: Confirm animate:flip usage for reorder and filtering
Usinganimate:flipon thelielements animates position changes smoothly when tasks reorder or disappear due to filtering.Final Answer:
This code correctly uses a keyed {#each} block with animate:flip, animating reorder and filtering smoothly. -> Option BQuick Check:
Keyed filtered list + animate:flip = smooth reorder & filter animation [OK]
- Not keying filtered items uniquely
- Filtering outside {#each} causing stale keys
- Using text as key instead of unique id
Ready to prove it?
Pass the test and this pattern moves unknown
