最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to animate card fade in and fade out inplace in svelte - Stack Overflow

programmeradmin2浏览0评论

How would I create the following animation in svelte?

what I am trying to do: I have a two div elements. When I click on a button, firs div element fades out while the second div element fades in. Both the div elements are at the same position. So it looks like when one is fading out, the other card is fading in to take its place. Example:

What I have already tried: Demo here: /u09gz5ytcs9rl7i

As you can see in the demo, the other card fades in but it is below the first card, and as the first card disappears it snaps in to its place.

How would I create the following animation in svelte?

what I am trying to do: I have a two div elements. When I click on a button, firs div element fades out while the second div element fades in. Both the div elements are at the same position. So it looks like when one is fading out, the other card is fading in to take its place. Example: https://playcode.io/2226396

What I have already tried: Demo here: https://www.sveltelab.dev/u09gz5ytcs9rl7i

As you can see in the demo, the other card fades in but it is below the first card, and as the first card disappears it snaps in to its place.

Share Improve this question asked Jan 17 at 16:47 montemonte 1,8932 gold badges17 silver badges39 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

This happens because the first element is still in place when the second element is rendered. The first element will only leave the DOM after its animation ends, and the second is rendered before its animation starts.

What you could do is make both elements occupy the same space in the div, while making sure there is a delay between the animations:

<CardContent>

    {#if btn}
        <div transition:fade={{ duration: 1000, delay: btn ? 1000 : 0 }} class="column-start-1">
            <Card>
                <CardContent>
                    First Card
                </CardContent>
            </Card>
        </div>
    {:else}
        <div transition:fade={{ duration: 1000, delay: btn ? 0 : 1000 }} class="column-start-1">
            <Card>
                <CardContent>
                    Second Card
                </CardContent>
            </Card>
        </div>
    {/if}
    
</CardContent>
发布评论

评论列表(0)

  1. 暂无评论