1

当反应变量发生变化时触发动画的最佳方式是什么?我想做这样的事情: 

<script>
    import { fade } from 'svelte/transition'
    let count = 0;
    const handleClick = () => count +=1
</script>

<button on:click={handleClick} transition:slide>
    Click me
</button>
<p> You cliked <strong transition:fade>{count}</strong> times</p>

这不起作用,因为<strong>节点没有从 DOM 中删除(我猜)。那么,当数字发生变化时,让数字淡入淡出的最佳方法是什么?

4

1 回答 1

4

您现在可以(v3.28)使用该{#key <key}指令:

<script>
    import { fade } from 'svelte/transition'
    let count = 0;
    const handleClick = () => count +=1
</script>

<button on:click={handleClick}>
    Click me
</button>
<p> You cliked 
    {#key count}
      <strong in:fade>{count}</strong> 
    {/key}
    times</p>

旧答案

尝试这个 :

<script>
    import { fade } from 'svelte/transition'
    let count = 0;
    const handleClick = () => count +=1
</script>

<button on:click={handleClick}>
    Click me
</button>
<p> You cliked 
    {#each [count] as c (c)}
    <strong in:fade>{c}</strong> 
    {/each}
    times</p>

REPL

于 2020-06-28T19:14:16.423 回答