In and out transitions in Svelte are animations that run when a component or element enters (in) or leaves (out) the DOM. They help make UI changes smooth and visually clear.
Click to reveal answer
beginner
How do you apply an in transition to an element in Svelte?
Use the in: directive followed by the transition function name, like in:fade. For example: <div in:fade>Hello</div>.
Click to reveal answer
beginner
Name two built-in transitions provided by Svelte.
Two built-in transitions are fade (fades element in/out) and slide (slides element in/out from a direction).
Click to reveal answer
beginner
What is the difference between in: and out: directives in Svelte?
in: runs the transition when an element appears in the DOM. out: runs the transition when the element is removed from the DOM.
Click to reveal answer
intermediate
How can you customize the duration of a Svelte transition?
Pass an options object to the transition function, like in:fade={{ duration: 500 }} to make the fade last 500 milliseconds.
Click to reveal answer
Which directive applies a transition when an element is removed from the DOM in Svelte?
Aout:
Bin:
Ctransition:
Danimate:
✗ Incorrect
The out: directive runs the transition when the element leaves the DOM.
What does the built-in fade transition do in Svelte?
ASlides the element in and out
BRotates the element
CChanges the element's opacity gradually
DScales the element up and down
✗ Incorrect
The fade transition changes the element's opacity smoothly to appear or disappear.
How do you specify a 1-second duration for a fade transition in Svelte?
Atransition:fade duration=1000
Bin:fade(1000)
Cfade:duration=1000
Din:fade={{ duration: 1000 }}
✗ Incorrect
You pass an options object with duration like in:fade={{ duration: 1000 }}.
Which of these is NOT a built-in Svelte transition?
Abounce
Bfly
Cslide
Dfade
✗ Incorrect
bounce is not a built-in transition in Svelte.
What happens if you use both in: and out: on the same element?
AOnly the in transition runs
BBoth transitions run on enter and leave respectively
CIt causes an error
DOnly the out transition runs
✗ Incorrect
Both transitions run: in: when the element appears, out: when it disappears.
Explain how to add a fade-in and fade-out effect to a Svelte element using built-in transitions.
Think about how to tell Svelte what to do when the element appears and disappears.
You got /3 concepts.
Describe the difference between in and out transitions in Svelte and when each runs.
Consider the element's lifecycle in the page.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using in: and out: transitions in Svelte?
easy
A. To animate elements when they enter or leave the page
B. To change the color of text dynamically
C. To handle form input validation
D. To fetch data from an API
Solution
Step 1: Understand transition directives
The in: and out: directives in Svelte are used to add animations when elements appear or disappear.
Step 2: Identify the purpose of these transitions
These transitions help create smooth animations for elements entering or leaving the page, improving user experience.
Final Answer:
To animate elements when they enter or leave the page -> Option A
Quick Check:
Transitions animate element entry/exit = C [OK]
Hint: Remember: in/out transitions animate element appearance/disappearance [OK]
Common Mistakes:
Confusing transitions with styling like color changes
Thinking transitions handle data fetching
Mixing transitions with form validation
2. Which of the following is the correct syntax to apply a fade-in transition to a <div> in Svelte?
easy
A.
B.
C.
D.
Solution
Step 1: Recall Svelte transition syntax
Svelte uses in: and out: directives followed by the transition name, e.g., in:fade.
Step 2: Match the correct syntax
<div in:fade> uses in:fade correctly on the <div> element.
Final Answer:
<div in:fade> -> Option B
Quick Check:
Correct directive syntax = A [OK]
Hint: Use in: before transition name, like in:fade [OK]
A. The <div> appears and disappears instantly without animation
B. An error occurs because fade is not imported correctly
C. The button stops working after first click
D. The <div> smoothly fades in and out on toggle
Solution
Step 1: Understand the code behavior
The visible variable toggles between true and false on button click, controlling the <div> display.
Step 2: Analyze transitions applied
The in:fade and out:fade directives animate the <div> fading in when it appears and fading out when it disappears.
Final Answer:
The <div> smoothly fades in and out on toggle -> Option D
Quick Check:
Fade transitions animate toggle = B [OK]
Hint: Toggle with in:fade and out:fade means smooth fade animations [OK]
Common Mistakes:
Expecting instant show/hide without animation
Assuming button stops working
Thinking import is missing or wrong
4. You wrote this Svelte code:
<script>
import { slide } from 'svelte/transition';
let show = false;
</script>
<button on:click={() => show = !show}>Show</button>
{#if show}
<div in:slide out:slide>Sliding content</div>
{/if}
But the slide transition does not run when the <div> disappears. What is the likely cause?
medium
A. The <div> is removed instantly before the out transition runs
B. The slide transition requires a duration parameter
C. The out:slide directive is missing or misspelled
D. The show variable is not toggled correctly
Solution
Step 1: Check transition directives
The code includes both in:slide and out:slide, so directives are present and spelled correctly.
Step 2: Understand Svelte transition timing
If the element is removed instantly (e.g., by a parent condition or key), the out transition may not run because the element is gone before animation starts.
Final Answer:
The <div> is removed instantly before the out transition runs -> Option A
Quick Check:
Instant removal blocks out transition = A [OK]
Hint: Out transitions need element present; instant removal skips animation [OK]
Common Mistakes:
Assuming missing directive causes issue
Thinking duration parameter is always required
Believing toggle variable is incorrect
5. You want to create a Svelte component that slides in a message when it appears and fades out smoothly when it disappears. Which code snippet correctly applies these transitions?
hard
A. <div in:slide={{ x: -200 }} out:fade>Message</div>
B. <div in:fade out:slide={{ x: 200 }}>Message</div>
C. <div in:slide out:fade={{ duration: 500 }}>Message</div>
D. <div in:slide={{ y: -200 }} out:fade>Message</div>
Solution
Step 1: Understand transition requirements
The message should slide in when it appears and fade out smoothly with a custom duration.
Step 2: Analyze options for correct syntax and parameters
<div in:slide out:fade={{ duration: 500 }}>Message</div> uses in:slide (default slide transition) and out:fade with a duration parameter, which is valid syntax.
Step 3: Check other options for errors
<div in:slide={{ x: -200 }} out:fade>Message</div> uses x: -200 which is invalid for slide (slide uses default direction, no x param). <div in:fade out:slide={{ x: 200 }}>Message</div> reverses transitions. <div in:slide={{ y: -200 }} out:fade>Message</div> uses invalid parameter y: -200.
Final Answer:
<div in:slide out:fade={{ duration: 500 }}>Message</div> -> Option C
Quick Check:
Slide in + fade out with duration = D [OK]
Hint: Use slide for in, fade with params for out to combine effects [OK]
Common Mistakes:
Using invalid parameters for slide transition
Mixing up in and out transitions
Forgetting to add parameters for smooth fade out
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.