Consider this Svelte component that uses the fade transition on a paragraph. What will you see when you click the button to toggle the paragraph?
<script>
import { fade } from 'svelte/transition';
let visible = true;
</script>
<button on:click={() => visible = !visible} aria-pressed={visible} aria-label="Toggle paragraph">Toggle</button>
{#if visible}
<p transition:fade>This paragraph fades in and out.</p>
{/if}Think about what the fade transition does in Svelte.
The fade transition in Svelte smoothly changes the element's opacity from 0 to 1 when it appears and from 1 to 0 when it disappears, creating a fade effect.
You want to make a paragraph fade out over 2 seconds when it is removed. Which code snippet correctly sets the duration for the fade out transition?
<script>
import { fade } from 'svelte/transition';
let show = true;
</script>
<button on:click={() => show = !show} aria-label="Toggle paragraph">Toggle</button>
{#if show}
<p ???>This paragraph fades out slowly.</p>
{/if}Remember how to specify only the out transition with custom parameters.
In Svelte, to customize only the out transition, you use out:fade={{ duration: 2000 }}. The transition:fade applies to both in and out.
Look at this Svelte code. The paragraph should fade out when hidden, but it disappears instantly. Why?
<script>
import { fade } from 'svelte/transition';
let visible = true;
</script>
<button on:click={() => visible = !visible} aria-label="Toggle">Toggle</button>
{#if visible}
<p transition:fade={{ duration: 1000 }} out:fade={{ duration: 2000 }}>Hello!</p>
{/if}Check how Svelte handles multiple transition directives on the same element.
When both transition:fade and out:fade are used on the same element, transition:fade controls both in and out transitions, overriding out:fade. So the out transition uses the duration from transition:fade (1000ms), not the 2000ms specified in out:fade. The out transition still runs but with the shorter duration.
count after this Svelte component runs?This Svelte component increments count each time the paragraph finishes its out transition. What is the value of count after clicking the button twice?
<script>
import { fade } from 'svelte/transition';
let visible = true;
let count = 0;
function handleOutroEnd() {
count += 1;
}
</script>
<button on:click={() => visible = !visible} aria-label="Toggle">Toggle</button>
{#if visible}
<p transition:fade on:outroend={handleOutroEnd}>Count: {count}</p>
{/if}Remember when the outroend event fires in Svelte transitions.
The outroend event fires once when the element finishes its out transition. Clicking the button twice toggles visibility twice: first hides (count increments to 1), then shows (no increment). So final count is 1.
in and out transitions is true?Choose the correct statement about how Svelte handles in and out transitions on elements.
Think about how Svelte triggers transitions when elements enter or leave the DOM.
Svelte allows an element to have separate in and out transitions. The in transition runs when the element appears, and the out transition runs when it disappears. They work independently and can have different effects or durations.