Svelte:响应式数据更改的过渡
Svelte: transition on reactive data change
当反应变量发生变化时触发动画的最佳方式是什么?
我想做这样的事情:
<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 中删除(我猜)。那么让数字在变化时淡入淡出的最佳方法是什么?
您现在可以 (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>
当反应变量发生变化时触发动画的最佳方式是什么? 我想做这样的事情:
<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 中删除(我猜)。那么让数字在变化时淡入淡出的最佳方法是什么?
您现在可以 (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>