Svelte · Junior · Question 39 of 76

How do you animate an element appearing and disappearing?

<script>
  import { fade } from "svelte/transition";
  let visible = $state(true);
</script>

{#if visible}
  <p transition:fade>Hello</p>
{/if}

Explanation

in: and out: set separate intro and outro transitions. Built-ins include fade, fly, slide, scale, blur, draw.

Further reading

Problem with a question

Svelte · Junior · question 39

Test yourself

Take the Svelte · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz