Which of the following best explains why adding transitions to UI elements in Svelte improves user experience?
Think about how smooth changes on screen affect how users feel and understand the app.
Transitions create smooth visual changes that guide users' attention and make the interface feel more natural and responsive.
Consider a Svelte component where an element uses the fade transition when it appears and disappears. What will the user see?
<script> import { fade } from 'svelte/transition'; let visible = false; </script> <button on:click={() => visible = !visible}>Toggle</button> {#if visible} <p transition:fade>This text fades in and out.</p> {/if}
The fade transition changes opacity over time.
The fade transition smoothly changes the element's opacity from 0 to 1 when it appears and from 1 to 0 when it disappears.
Which option shows the correct way to apply a slide transition to a <div> element in Svelte?
import { slide } from 'svelte/transition'; let show = true;
Remember that transitions are applied to elements that appear or disappear inside an {#if} block.
The transition directive transition:slide must be on the element that is conditionally rendered inside the {#if} block.
Given the code below, why does Svelte throw an error when toggling the visible variable?
<script> import { fade } from 'svelte/transition'; let visible = false; </script> <button on:click={() => visible = !visible}>Toggle</button> {#if visible} <p transition:fade>Text</p> {/if} <p transition:fade>Always visible text</p>
Think about when transitions run and what happens if the element is always present.
Transitions only run when elements enter or leave the DOM. Applying transition:fade to an element that never leaves causes an error.
Consider this Svelte code snippet:
import { fade } from 'svelte/transition';
let visible = false;
function toggle() {
visible = !visible;
}
And the template:
<button on:click={toggle}>Toggle</button>
{#if visible}
<div transition:fade>Hello</div>
{/if}If the user clicks the toggle button twice quickly, what will be the opacity of the <div> after the second click's transition finishes?
Think about the state after two toggles starting from false.
Starting from visible = false, two toggles set it to true then false again. The element fades out and is removed, so opacity ends at 0.