0
0
Svelteframework~10 mins

Custom transitions in Svelte - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom transitions
Component mounts
Element enters DOM
Apply custom transition function
Transition runs over time
Element fully visible
Trigger element removal
Apply custom transition function in reverse
Transition runs over time
Element removed from DOM
When an element appears or disappears, a custom transition function runs to animate it smoothly before it fully shows or hides.
Execution Sample
Svelte
import { cubicOut } from 'svelte/easing';

function fade(node, { duration }) {
  return {
    duration,
    css: t => `opacity: ${t}`
  };
}

<div transition:fade={{ duration: 500 }}>Hello</div>
This code applies a custom fade transition that changes opacity from 0 to 1 over 500ms when the element appears.
Execution Table
StepTransition PhaseTime Progress (t)CSS AppliedElement State
1Enter start0opacity: 0Element starts invisible
2Enter mid0.5opacity: 0.5Element partially visible
3Enter end1opacity: 1Element fully visible
4Exit start1opacity: 1Element fully visible before exit
5Exit mid0.5opacity: 0.5Element partially fading out
6Exit end0opacity: 0Element invisible, about to be removed
💡 Transition completes when t reaches 1 on enter and 0 on exit, then element is added or removed.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 6
t00.510.50
opacity00.510.50
Key Moments - 3 Insights
Why does the opacity start at 0 when the element enters?
Because at Step 1 in the execution_table, t=0 means the transition just started, so opacity is 0 to make the element invisible initially.
What happens if the duration is longer?
The transition steps take more time, but the opacity still moves smoothly from 0 to 1 (enter) or 1 to 0 (exit) as shown in the execution_table steps.
Why is the element still visible at exit start (Step 4)?
At exit start, t=1 means the element is fully visible before the fade-out begins, so opacity is 1 before it starts fading.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the opacity at Step 2 during the enter phase?
A0
B0.5
C1
D0.75
💡 Hint
Check the 'CSS Applied' column at Step 2 in the execution_table.
At which step does the element become fully visible during entering?
AStep 5
BStep 1
CStep 3
DStep 6
💡 Hint
Look for opacity: 1 in the 'CSS Applied' column during enter phase.
If the transition duration is doubled, how does the execution_table change?
AThe steps remain the same but take longer in real time.
BThe time progress (t) values change to 0, 0.25, 0.5, etc.
CThe opacity values change to different numbers.
DThe element skips the fade and appears instantly.
💡 Hint
Duration affects timing, not the opacity values shown in the execution_table.
Concept Snapshot
Custom transitions in Svelte animate elements when they enter or leave.
Define a function returning duration and CSS styles based on progress t (0 to 1).
Use transition:yourFunction={{ options }} on elements.
The transition runs smoothly over time, changing styles like opacity.
When done, element is fully shown or removed.
Full Transcript
Custom transitions in Svelte let you animate elements as they appear or disappear. When the component mounts, the element enters the DOM and the custom transition function runs. This function returns an object with duration and a CSS function that changes styles based on a progress value t from 0 to 1. For example, opacity can go from 0 to 1 to fade in. The execution table shows steps where t changes and opacity updates accordingly. When the element leaves, the transition runs in reverse from opacity 1 to 0 before the element is removed. Variables like t and opacity update step by step during the transition. Beginners often wonder why opacity starts at 0 or why the element is visible at exit start; these are explained by the transition progress values. The duration controls how long the transition takes but not the style values themselves. This visual trace helps understand how custom transitions animate elements smoothly in Svelte.