Custom transitions let you create smooth animations when elements appear or disappear. They make your app feel lively and polished.
Custom transitions in Svelte
import { cubicOut } from 'svelte/easing'; function customTransition(node, { duration = 400 }) { return { duration, css: t => `opacity: ${t}; transform: translateX(${(1 - t) * 100}px);`, easing: cubicOut }; }
The node is the HTML element to animate.
The function returns an object with duration, css for styles, and optional easing.
function fadeAndSlide(node, { duration = 500 }) { return { duration, css: t => `opacity: ${t}; transform: translateX(${(1 - t) * 50}px);` }; }
function scaleUp(node, { duration = 300 }) { return { duration, css: t => `transform: scale(${t}); opacity: ${t};` }; }
function bounce(node, { duration = 600 }) { return { duration, css: t => `transform: translateY(${Math.sin(t * Math.PI) * -20}px); opacity: ${t};` }; }
This Svelte component shows a green box that appears and disappears with a custom fade and slide transition. The button toggles the box. The transition moves the box from right to left while fading it in or out.
Accessibility is included with aria-pressed, aria-live, and keyboard focus.
<script> import { cubicOut } from 'svelte/easing'; import { onMount } from 'svelte'; let visible = false; function customFadeSlide(node, { duration = 400 }) { return { duration, easing: cubicOut, css: t => `opacity: ${t}; transform: translateX(${(1 - t) * 100}px);` }; } onMount(() => { visible = true; }); </script> <button on:click={() => visible = !visible} aria-pressed={visible} aria-label="Toggle box"> Toggle Box </button> {#if visible} <div transition:customFadeSlide={{ duration: 600 }} role="region" aria-live="polite" tabindex="0" style="width: 200px; height: 100px; background-color: #4caf50; color: white; display: flex; align-items: center; justify-content: center; margin-top: 1rem; border-radius: 0.5rem;" > Hello with custom transition! </div> {/if}
Custom transitions let you control exactly how elements animate.
Use css function to write styles that change over time from 0 to 1.
Remember to add accessibility attributes like aria-live and keyboard focus for dynamic content.
Custom transitions create smooth, unique animations for elements entering or leaving.
You write a function that returns styles changing over time.
They help make your app feel friendly and polished.
Practice
custom transition in Svelte?Solution
Step 1: Understand what transitions do in Svelte
Transitions animate elements as they appear or disappear from the DOM.Step 2: Identify the role of custom transitions
Custom transitions let you define your own animation styles for these changes.Final Answer:
To create unique animations when elements enter or leave the page -> Option AQuick Check:
Custom transitions = unique enter/leave animations [OK]
- Confusing transitions with data fetching
- Thinking transitions manage state
- Mixing transitions with event handling
Solution
Step 1: Recall the custom transition function signature
It must acceptnodeand optionallyparams.Step 2: Check the returned object structure
The object should includedurationand acssfunction usingtfor progress.Final Answer:
function fade(node, params) { return { duration: 300, css: t => `opacity: ${t}` }; } -> Option DQuick Check:
Function(node, params) + {duration, css} = C [OK]
- Omitting the node parameter
- Returning delay instead of duration
- Not using a css function with progress t
function slide(node) {
return {
duration: 400,
css: t => `transform: translateX(${(1 - t) * 100}%)`
};
}What happens to the element when it enters the DOM using
transition:slide?Solution
Step 1: Analyze the css function for entry
The transform moves from translateX(100%) at t=0 to translateX(0%) at t=1.Step 2: Understand the visual effect
The element starts off-screen right (100%) and slides left to original place (0%).Final Answer:
It slides in from the right to its original position -> Option CQuick Check:
translateX from 100% to 0% = slide in right to left [OK]
- Confusing enter and leave animations
- Assuming opacity changes instead of transform
- Thinking it slides out instead of in
function grow(node) {
return {
duration: 500,
css: t => `scale(${t})`
};
}What is the error and how to fix it?
Solution
Step 1: Identify the CSS syntax error
The css string lacks the CSS property name; 'scale' alone is invalid.Step 2: Correct the CSS string
It should be 'transform: scale(...)' to apply scaling properly.Final Answer:
Missing 'transform:' prefix in css string; fix by returning `transform: scale(${t})` -> Option AQuick Check:
CSS property needed before scale() [OK]
- Returning only scale() without 'transform:'
- Using wrong duration type
- Removing needed node parameter
Solution
Step 1: Understand the fade and grow requirements
Opacity should go from 0 to 1 (fade in), scale from 0.5 to 1 (grow).Step 2: Check each option's css function
function fadeGrow(node) { return { duration: 600, css: t => `opacity: ${t}; transform: scale(${0.5 + 0.5 * t})` }; } uses opacity = t (0 to 1) and scale = 0.5 + 0.5 * t (0.5 to 1), matching requirements.Final Answer:
function fadeGrow(node) { return { duration: 600, css: t => `opacity: ${t}; transform: scale(${0.5 + 0.5 * t})` }; } -> Option BQuick Check:
Opacity 0->1 and scale 0.5->1 = function fadeGrow(node) { return { duration: 600, css: t => `opacity: ${t}; transform: scale(${0.5 + 0.5 * t})` }; } [OK]
- Reversing opacity or scale ranges
- Using scale starting at 0 instead of 0.5
- Mixing up increasing and decreasing values
Ready to prove it?
Pass the test and this pattern moves unknown
