Svelte 40 🧡 Transitions Concept — transition:fade, transition:fly, transition:slide
Transitions are the standard Svelte way to add visual polish to your UI as elements enter and leave the DOM. Instead of manually manipulating classes or writing complex keyframe CSS, Svelte allows you to declare exactly how an element should behave when it appears or disappears. Because these transitions are compiled into CSS animations, they remain performant and offloaded from the main JavaScript thread, ensuring your UI stays responsive during movement.
This chapter introduces the core transition: directive and its directional counterparts, in: and out:. We will examine the three fundamental transitions—fade, fly, and slide—and how to apply parameters to customize their duration, easing, and direction. By the end of this chapter, you will understand how to orchestrate graceful UI updates without the overhead of manual animation logic.
Key point: Svelte transitions provide a declarative API that automates the generation of CSS animations for elements as they are added or removed from the DOM; a bidirectional transition can be interrupted, while in and out allow for asymmetric entry and exit.
Why transitions exist
The layout-shift problem.
When elements suddenly appear or disappear, the layout jumps, causing “layout shift” which is poor for user experience. Transitions provide a visual cue, softening the impact of DOM updates.
Declarative DOM management.
Traditional animations often require adding and removing CSS classes via JavaScript, which requires manual tracking of the animation completion. Transitions handle the cleanup automatically.
Performance optimization.
Svelte transitions are compiled to CSS keyframes whenever possible. This avoids the “jank” associated with JavaScript-driven animation libraries, keeping the browser’s paint pipeline efficient.
Contextual UX feedback.
Transitions help users perceive the hierarchy of your interface. An element “sliding” down from a menu feels like a child of the header, while a “fade” suggests a neutral layer.
Asymmetric logic.
Sometimes you want an element to enter quickly (in:) but leave slowly (out:). The Svelte transition system makes this distinction explicit, allowing for fine-grained control over how the UI evolves.
a. Bidirectional transitions vs. in and out
The transition: directive creates a bidirectional animation: if an element is currently in the middle of an “in” transition and is removed, Svelte smoothly reverses the animation from its current state. If you want separate logic, you use in: and out:.
{#if visible}
<!-- transition: fade in and out -->
<div transition:fade>Hello</div>
<!-- in: fly, out: fade -->
<div in:fly={{ y: 20 }} out:fade>Asymmetric</div>
{/if}
Using transition: is best for elements where the enter and exit behavior should be symmetric. Using in: and out: is essential when you need elements to arrive from one specific state (like a slide-in) but leave via a different mechanic (like a subtle fade) to reduce visual clutter.
b. The core transitions: fade, fly, and slide
fade is the simplest transition, gradually changing the opacity of an element. fly adds movement, allowing an element to enter from an offset position (x or y) while fading. slide handles height transitions, making an element “grow” or “shrink” vertically as it appears or disappears, which is perfect for menus and accordions.
<script>
import { fade, fly, slide } from 'svelte/transition';
</script>
<div transition:fade>I just change opacity</div>
<div transition:fly={{ y: 200 }}>I fly in from below</div>
<div transition:slide>I slide to show/hide content</div>
These transitions are built-in and optimized. They require no external CSS files; the Svelte compiler generates the necessary keyframes automatically at build time, ensuring your bundle remains small and efficient.
c. Customizing with parameters
Every built-in transition accepts an object of parameters: duration, delay, easing, and specific properties like x, y, or axis. This allows you to tailor the transition feel to fit the specific speed and responsiveness of your UI.
<div transition:fly={{
duration: 1000,
delay: 250,
y: 50,
easing: (t) => t * t
}}>
Configurable fly animation
</div>
Providing custom parameters is the primary way to maintain a consistent visual language across your application. You can even create transition factories—functions that return these objects—to share configuration throughout your design system.
Complete Example Session
// ============================================
// PART 1: FADE TRANSITION
// ============================================
{#if show}
<div transition:fade={{ duration: 300 }}>Fading...</div>
{/if}
// ============================================
// PART 2: FLY TRANSITION
// ============================================
<div transition:fly={{ y: 50, duration: 500 }}>Flying...</div>
// ============================================
// PART 3: SLIDE TRANSITION
// ============================================
<div transition:slide={{ axis: 'y' }}>Sliding...</div>
// ============================================
// PART 4: ASYMMETRIC IN/OUT
// ============================================
<div in:fly={{ x: 100 }} out:fade={{ duration: 1000 }}>
In and Out
</div>
This session shows the three core transitions and the distinction between bidirectional transitions and split in/out behavior.
Quick Reference
Directives
| Directive | Direction | Usage |
|---|---|---|
transition: | Bidirectional | Syncs enter/exit |
in: | Entry only | Element appearing |
out: | Exit only | Element disappearing |
Core Transitions
| Transition | Property | Best For |
|---|---|---|
fade | Opacity | Neutral appearance |
fly | Transform/Opacity | Directional movement |
slide | Height/Margin | Lists, menus, text |
Best Practices
✅ Do This:
// Use transition for simple toggles
<div transition:fade>...</div> // ✅
// Prefer in/out for complex UX
<div in:fly={{ y: 10 }} out:fade>...</div> // ✅
// Extract transition settings into constants
const transitionOpts = { duration: 300 }; // ✅
// Combine with animate for list reordering
<div transition:fade animate:flip>...</div> // ✅
❌ Don’t Do This:
// Forget to include the 'if' block logic
<div transition:fade /> // Will not animate // ❌
// Use extremely long durations
<div transition:fade={{ duration: 5000 }} /> // ❌
// Over-animate every single element
// Can lead to "animation soup" // ❌
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| No animation | No {#if} block | Wrap in {#if} |
| CSS conflicts | Manual styles on element | Use style: directive |
| Layout jump | slide conflicts with height | Add overflow: hidden |
| In/Out mismatch | Conflicting transition params | Keep durations consistent |
Real-World Examples
1. Notification Toast
<div transition:fly={{ y: 20 }}>Toast</div>
2. Accordion Body
<div transition:slide>Body content</div>
3. Modal Overlay
<div transition:fade={{ duration: 200 }} />
4. Dropdown Menu
<ul in:fly={{ y: -10 }} out:fade>...</ul>
5. Search Results
<div transition:fade>Result item</div>
6. Loading Spinner
<div transition:fade>Loading...</div>
7. Sidebar Slide
<nav transition:slide={{ axis: 'x' }}>... </nav>
8. Image Gallery Lightbox
<div transition:fade>Overlay</div>
9. Error Message
<p transition:fly={{ x: -20 }}>Error!</p>
10. Expanding Section
<div transition:slide>Extra details</div>
Visual
The Transition Directive Logic
┌──────────────────────────────────────────────┐
│ {#if visible} ──► In Transition ──► DOM │
│ │
│ {/if} ──► Out Transition ─► Remove │
└──────────────────────────────────────────────┘
In vs. Out vs. Transition
┌──────────────────────────────────────────────┐
│ transition: ──► [Enter] <───► [Exit] │
│ │
│ in: ──────────► [Enter] │
│ out: [Exit] │
└──────────────────────────────────────────────┘
Slide Mechanics
┌──────────────────────────────────────────────┐
│ Height: 0px ──► 50px ──► 100px │
│ (Triggered by `slide`) │
│ │
│ CSS: display: block; overflow: hidden; │
└──────────────────────────────────────────────┘
Summary
| Item | Value |
|---|---|
| Syntax | transition:, in:, out: |
| Core Types | fade, fly, slide |
| Timing | Duration, Delay, Easing |
| Bidirectional | transition (can interrupt) |
| Performance | Compiled CSS |
Key takeaways:
- Transitions are declarative. You define the intent (fade, fly, slide), and Svelte handles the DOM manipulation and CSS generation.
transitionvsin/out. Usetransitionfor symmetric entry/exit; usein/outwhen you want different logic for appearing and disappearing.- Keep it light. Animations should be quick (200ms–400ms) to ensure the UI feels responsive.
- Use
slidefor content blocks. It’s the perfect transition for revealing hidden text or menu options by animating the element’s height. - Parameters are key. Don’t settle for defaults; customize your easing and duration to match the pacing of your specific interface.
Remember: Svelte transitions allow you to design professional, fluid user interfaces without the burden of manual DOM animation libraries. By focusing on declarative intent—fading in, flying in, or sliding in—you keep your component code clean while providing your users with a smooth, polished navigation experience.
Stop using slow, ad-bloated tool sites! 🤮
🔎 Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)
⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free
🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!