0
0
Svelteframework~10 mins

Transition directive (transition:fade) in Svelte - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Transition directive (transition:fade)
Component loads
Element appears
Apply transition:fade
Opacity changes 0 -> 1
Element fully visible
Element removed
Apply transition:fade
Opacity changes 1 -> 0
Element removed from DOM
When an element appears or disappears, the fade transition smoothly changes its opacity from 0 to 1 or 1 to 0.
Execution Sample
Svelte
<script>
  import { fade } from 'svelte/transition';
  let visible = false;
</script>

<button on:click={() => visible = !visible}>Toggle</button>

{#if visible}
  <p transition:fade>Hello!</p>
{/if}
This code toggles a paragraph with a fade effect on show and hide.
Execution Table
StepActionvisibleElement StateOpacityTransition Applied
1Initial loadfalseNo <p>N/ANo
2Click button to showtrue<p> created0 (start fade in)fade in starts
3Fade in progresstrue<p> visible0.5 (mid fade)fade in ongoing
4Fade in completetrue<p> visible1 (fully visible)fade in ends
5Click button to hidefalse<p> visible1 (start fade out)fade out starts
6Fade out progressfalse<p> visible0.5 (mid fade)fade out ongoing
7Fade out completefalse<p> removed0 (invisible)fade out ends
💡 Element is removed after opacity reaches 0 on fade out.
Variable Tracker
VariableStartAfter Step 2After Step 5Final
visiblefalsetruefalsefalse
OpacityN/A0 (fade in start)1 (fade out start)0 (fade out end)
Element StateNo <p><p> created<p> visibleNo <p>
Key Moments - 2 Insights
Why does the element still exist during fade out even though visible is false?
Because the fade transition keeps the element in the DOM while opacity changes from 1 to 0, as shown in execution_table steps 5-7.
What happens if you remove transition:fade from the element?
The element appears or disappears instantly without opacity changes, skipping the fade steps in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the opacity value at step 3?
A0.5
B1
C0
DN/A
💡 Hint
Check the 'Opacity' column in execution_table row for step 3.
At which step does the element get removed from the DOM?
AStep 4
BStep 2
CStep 7
DStep 1
💡 Hint
Look at the 'Element State' column in execution_table for when

is removed.

If visible stayed true, what would happen to the fade transition steps?
AFade in would repeat continuously
BFade out steps would not occur
CElement would be removed immediately
DOpacity would stay at 0
💡 Hint
Refer to variable_tracker and execution_table steps where visible changes to false triggers fade out.
Concept Snapshot
Svelte's transition:fade smoothly changes an element's opacity from 0 to 1 on enter and 1 to 0 on leave.
Use it inside {#if} blocks to animate showing/hiding.
The element stays in the DOM during fade out until fully transparent.
Syntax: <element transition:fade>.
You can customize duration and easing if needed.
Great for gentle appear/disappear effects.
Full Transcript
This visual execution shows how Svelte's transition:fade works step-by-step. When the component loads, the paragraph is hidden. Clicking the button sets visible to true, creating the paragraph with opacity starting at 0. The fade transition gradually increases opacity to 1, making it fully visible. When clicking again, visible becomes false, but the paragraph stays in the DOM while opacity fades from 1 to 0. After fade out completes, the paragraph is removed. Variables tracked include visible, opacity, and element state. Key moments clarify why the element remains during fade out and what happens without the transition. Quiz questions test understanding of opacity values, removal timing, and transition triggers.