Using Transition Parameters (duration, delay) in Svelte
📖 Scenario: You are building a simple Svelte app where a message appears and disappears with a smooth fade effect. You want to control how long the fade lasts and add a delay before it starts, just like waiting for a friend before you both start dancing.
🎯 Goal: Create a Svelte component that shows a message with a fade transition. Use the duration and delay parameters to control how the fade effect behaves.
📋 What You'll Learn
Create a boolean variable visible to control message visibility
Add a button to toggle visible
Use Svelte's fade transition on the message
Set duration to 1000 milliseconds (1 second)
Set delay to 500 milliseconds (0.5 seconds)
💡 Why This Matters
🌍 Real World
Transitions with parameters help make web apps feel smooth and polished, improving user experience by controlling animation timing.
💼 Career
Knowing how to use transitions with parameters is useful for frontend developers building interactive UI components in Svelte.
Progress0 / 4 steps
1
Set up visibility control
Create a boolean variable called visible and set it to true.
Svelte
Hint
Use let visible = true; to create a variable that controls if the message shows.
2
Add a toggle button
Add a <button> element with text Toggle that toggles the visible variable when clicked using on:click.
Svelte
Hint
Use <button on:click={() => visible = !visible}>Toggle</button> to switch visibility.
3
Add fade transition with parameters
Import the fade transition from 'svelte/transition'. Use the fade transition on a <p> element that shows the text "Hello, Svelte!" only when visible is true. Set the transition parameters duration to 1000 and delay to 500.
Svelte
Hint
Use transition:fade={{ duration: 1000, delay: 500 }} inside the <p> tag to control the fade effect.
4
Complete the component
Ensure the component has the import, the visible variable, the toggle button, and the conditional paragraph with the fade transition using the specified parameters.
Svelte
Hint
Check that all parts are included and correctly written.
Practice
(1/5)
1. In Svelte, what does the duration parameter control in a transition?
easy
A. How long to wait before starting the animation
B. The size of the animated element
C. The color of the animated element
D. How long the animation lasts
Solution
Step 1: Understand transition parameters
The duration parameter defines the length of time the animation runs.
Step 2: Differentiate from delay
The delay parameter controls waiting time before animation starts, not duration.
Final Answer:
How long the animation lasts -> Option D
Quick Check:
duration = animation length [OK]
Hint: Duration means how long animation runs, not when it starts [OK]
Common Mistakes:
Confusing duration with delay
Thinking duration changes color or size
Mixing up parameter names
2. Which of the following is the correct syntax to set a 500ms delay in a Svelte transition?
easy
A. transition:fade(delay: 500)
B. transition:fade(delay=500)
C. transition:fade={{ delay: 500 }}
D. transition:fade({ delay 500 })
Solution
Step 1: Recall Svelte transition syntax
Transition parameters are passed as an object inside double curly braces, e.g., transition:fade={{ delay: 500 }}.
Step 2: Identify correct object syntax
transition:fade={{ delay: 500 }} uses correct object syntax with colon and braces; others have invalid syntax.
Final Answer:
transition:fade={{ delay: 500 }} -> Option C
Quick Check:
Use double braces and colon for parameters [OK]
Hint: Use double curly braces and colon for transition params [OK]
Duration and delay must be numbers, not strings. Here, duration is a string '1000'.
Step 2: Validate other options
Delay can be any number >= 0; fade accepts parameters; no parentheses needed.
Final Answer:
Duration should be a number, not a string -> Option B
Quick Check:
Use numbers for duration and delay [OK]
Hint: Use numbers, not strings, for duration and delay [OK]
Common Mistakes:
Putting numbers in quotes
Assuming delay minimum is 500
Adding unnecessary parentheses
5. You want a Svelte element to fade in with a 2-second animation but only start after 1 second delay. Which code correctly achieves this?
hard
A. <div transition:fade={{ duration: 2000, delay: 1000 }}>Content</div>
B. <div transition:fade={{ delay: 2000, duration: 1000 }}>Content</div>
C. <div transition:fade={{ duration: '2000', delay: '1000' }}>Content</div>
D. <div transition:fade={{ duration: 1000 }} delay=2000>Content</div>
Solution
Step 1: Match duration and delay values
Duration should be 2000ms (2 seconds), delay should be 1000ms (1 second).
Step 2: Check syntax correctness
<div transition:fade={{ duration: 2000, delay: 1000 }}>Content</div> uses correct syntax with numbers and proper object format.
Step 3: Identify errors in other options
Using strings like '2000' and '1000' for duration and delay is incorrect (must be numbers); swapping to {{ delay: 2000, duration: 1000 }} is wrong; placing delay=2000 outside the transition directive is invalid.
Final Answer:
<div transition:fade={{ duration: 2000, delay: 1000 }}>Content</div> -> Option A
Quick Check:
Correct values and syntax for delay and duration [OK]
Hint: Duration = animation length; delay = wait time before start [OK]
Common Mistakes:
Swapping delay and duration values
Using strings instead of numbers
Placing delay outside transition directive
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.