Reader small image

You're reading from  Real-World Svelte

Product typeBook
Published inDec 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781804616031
Edition1st Edition
Languages
Right arrow
Author (1)
Tan Li Hau
Tan Li Hau
author image
Tan Li Hau

Tan Li Hau is a frontend developer at Shopee and a core maintainer of Svelte. He has delivered multiple conference talks and workshops on Svelte. Passionate about sharing his knowledge, Li Hau regularly contributes to the community through blog posts, YouTube videos, and books. He aspires to inspire others to explore and learn about Svelte and other modern web technologies.
Read more about Tan Li Hau

Right arrow

Using Transitions

Transitions are essential in creating smooth and engaging user experiences. By defining how elements appear, disappear, or change within a user interface, transitions can turn ordinary interactions into captivating experiences that leave lasting impressions on users.

Over the next three chapters, we will explore the topic of transitions in Svelte, beginning with a comprehensive understanding of how to use transitions in Svelte.

In this chapter, we will start by learning how to add transitions to elements in Svelte. We will explore the different transition directives and learn how to customize the transitions.

After that, we will discuss when and how the transitions are being played. We will look at different scenarios, such as where there’s a mix of elements with and without transitions, or when the elements are within nested logical blocks.

To truly master transitions, it’s important to understand the inner workings of the Svelte transition...

Technical requirements

You can find the code used in this chapter on GitHub: https://github.com/PacktPublishing/Real-World-Svelte/tree/main/Chapter13.

Adding transitions to elements

Svelte provides a simple and powerful way to add transitions to your application elements. The framework offers built-in transition functions that can be easily applied to elements, allowing for smooth animations and seamless user experiences. You can also define your own custom transitions, which we will learn about in the next chapter.

Transitions in Svelte are applied to elements when the elements are mounted or unmounted from the DOM. This ensures that elements appear and disappear gracefully, rather than just abruptly popping in and out of view.

To add a transition to an element in Svelte, you can use the transition: directive with the desired transition function. Here’s an example of adding a transition to an element:

<script>
  import { fade } from 'svelte/transition';
</script>
<div transition:fade>some text here</div>

In the preceding code snippet, we imported fade from svelte/transition...

When are the transitions played?

The transitions in Svelte are played when elements are added or removed from the DOM.

in: transitions are executed when an element is added to the DOM. This usually occurs when a component is initialized or when a condition that controls the element’s rendering becomes true.

For example, in an {#if} block, when the if condition turns from falsy to truthy, the elements inside the {#if} block are added to the DOM. All the in: transitions applied to these elements will be played simultaneously as soon as the elements are inserted into the DOM:

{#if condition}
  <div in:fade>some content</div>
  <div transition:blur>more content</div>
{/if}

In the preceding code snippet, as condition turns to true, both <div> elements will be inserted into the DOM. As soon as both <div> elements are inserted, both the fade and blur transitions will start playing simultaneously. Whether both the fade...

How Svelte transition works under the hood

Before we delve into the inner workings of Svelte transitions, let us first briefly discuss the general methods for creating animations on the web. Understanding these fundamental concepts provides a solid foundation for grasping how Svelte transitions work.

In general, you can create animations using either CSS or JavaScript.

Creating animations with CSS

To create animations using CSS, you can use the CSS animation property along with the @keyframes rules. The @keyframes rule is used to define a sequence of styles, specifying the CSS styles at each keyframe (from 0% to 100%) during the animation.

See this, for example:

@keyframes example {
  0% {
    opacity: 1;
    transform: scale(1);
  }
  100% {
    opacity: 0;
    transform: scale(1.75);
  }
}

In the preceding code snippet, we defined an animation keyframe...

Summary

In this chapter, we learned how to add a transition onto an element. We explored the transition:, in:, and out: directives, and how to customize them.

Following that, we looked at when and how the transitions are played. We discussed how the transitions are played when we have a mix of elements with and without transitions, and also how the transitions are played when used inside elements within nested logical blocks.

Last but not least, we dug deeper into how the transition animations are played by Svelte.

With this knowledge, you can now confidently apply transitions into elements when working with Svelte. This will allow you to enhance the interactivity and visual appeal of your applications, thereby providing a more engaging user experience.

In the next chapter, we will look beyond the built-in transitions and will explore the creation of custom transitions.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Real-World Svelte
Published in: Dec 2023Publisher: PacktISBN-13: 9781804616031
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Tan Li Hau

Tan Li Hau is a frontend developer at Shopee and a core maintainer of Svelte. He has delivered multiple conference talks and workshops on Svelte. Passionate about sharing his knowledge, Li Hau regularly contributes to the community through blog posts, YouTube videos, and books. He aspires to inspire others to explore and learn about Svelte and other modern web technologies.
Read more about Tan Li Hau