📖 Scenario: You are building a simple Svelte app that shows a list of fruits. When you add or remove fruits, you want the items to smoothly crossfade instead of just appearing or disappearing abruptly.
🎯 Goal: Create a Svelte component that displays a list of fruits with a crossfade animation when items are added or removed.
📋 What You'll Learn
Create an array called fruits with the exact values: ['Apple', 'Banana', 'Cherry'].
Create a variable called duration set to 400 to configure animation speed.
Use Svelte's crossfade function to create send and receive functions with the duration config.
Apply the transition:send={send} and transition:receive={receive} directives to the list items for crossfade effect.
💡 Why This Matters
🌍 Real World
Crossfade animations make UI changes feel smooth and natural, improving user experience in apps that update lists or collections dynamically.
💼 Career
Understanding Svelte's built-in transitions and how to apply them is useful for frontend developers building interactive, polished web applications.
Progress0 / 4 steps
1
Create the initial fruits array
Create an array called fruits with the exact values ['Apple', 'Banana', 'Cherry'].
Svelte
Hint
Use let fruits = ['Apple', 'Banana', 'Cherry']; to create the array.
2
Add a duration variable for animation speed
Create a variable called duration and set it to 400.
Svelte
Hint
Use let duration = 400; to set the animation speed.
3
Import and configure crossfade functions
Import crossfade from 'svelte/transition' and create send and receive functions by calling crossfade with an object that has duration set to the duration variable.
Svelte
Hint
Use import { crossfade } from 'svelte/transition'; and then const [send, receive] = crossfade({ duration });.
4
Apply crossfade transitions to list items
Create a <ul> element that loops over fruits using {#each fruits as fruit (fruit)}. Inside the loop, create <li> elements with the exact text {fruit}. Add transition:send={send} and transition:receive={receive} directives to each <li>.
Svelte
Hint
Use {#each fruits as fruit (fruit)} to loop and add transition:send={send} and transition:receive={receive} to each <li>.
Practice
(1/5)
1. What is the main purpose of using crossfade in Svelte list items?
easy
A. To fetch data asynchronously for list items
B. To validate user input in list forms
C. To smoothly animate items when they are added, removed, or reordered in a list
D. To sort list items alphabetically
Solution
Step 1: Understand crossfade usage context
Crossfade is used to animate transitions between list states, such as adding or removing items.
Step 2: Identify the animation effect
It creates smooth movement or fading effects for list items during changes.
Final Answer:
To smoothly animate items when they are added, removed, or reordered in a list -> Option C
Quick Check:
Crossfade = smooth list animations [OK]
Hint: Crossfade = smooth item movement in lists [OK]
Common Mistakes:
Confusing crossfade with data fetching
Thinking crossfade sorts items
Using crossfade for input validation
2. Which of the following is the correct way to import the crossfade functions in a Svelte component?
easy
A. import { crossfade } from 'svelte/animate';
B. import { send, receive } from 'svelte/transition';
C. import { fade } from 'svelte/motion';
D. import { crossfade } from 'svelte/transition';
Solution
Step 1: Recall correct import source
The crossfade function comes from svelte/transition.
Step 2: Match correct import syntax
import { crossfade } from 'svelte/transition'; correctly imports crossfade from svelte/transition.
Final Answer:
import { crossfade } from 'svelte/transition'; -> Option D