I have a page that has several stages, and I want to add transitions between them.
{#if state === 1}
<div class="red" transition:fade/>
{:else if state === 2}
<div class="blue" transition:fade/>
{:else if state === 3}
<div class="green" transition:fade/>
{/if}
However, when switching from one state to the next, the next one appears at the same time as the previous one is still disappearing, so the two states would appear at the same time for the duration of the transition.
What is the best approach (the approach that requires adding the least amount of code) to make one state’s fade in connect with another state’s fade out?
Here is a code sandbox link: https://codesandbox.io/p/sandbox/priceless-pine-kgrh7w
2
Answers
One way would be to position the elements on top of each other with the help of a wrapper element
REPL
I often use a wrapper to stack items in place:
REPL
This will cause an overlapping fade.
If that is not what you want, you can instead add a delay to to each transition that waits for the previous one to finish.
REPL
(The stack could still be added for safety if animations end up not fully in sync. Or increase the
delay
to a bit more than theduration
.)