animate
キー付きeachブロックの内容が並べ替えられると、アニメーションがトリガーされます。要素の追加または削除時にはアニメーションは実行されません。eachブロック内の既存のデータ項目のインデックスが変更された場合のみ実行されます。animateディレクティブは、キー付きeachブロックの直接の子である要素に付ける必要があります。
アニメーションは、Svelteのビルトインアニメーション関数またはカスタムアニメーション関数で使用できます。
<!-- When `list` is reordered the animation will run -->
{#each list as item, index (item)}
<li animate:flip>{item}</li>
{/each}
アニメーションパラメータ
アクションやトランジションと同様に、アニメーションにはパラメータを指定できます。
(二重の{{curlies}}
は特別な構文ではありません。これは、式タグ内のオブジェクトリテラルです。)
{#each list as item, index (item)}
<li animate:flip={{ delay: 500 }}>{item}</li>
{/each}
カスタムアニメーション関数
animation = (node: HTMLElement
node: HTMLElement, { from: any
from: type DOMRect: any
DOMRect, to: any
to: type DOMRect: any
DOMRect } , params: any
params: any) => {
delay?: number,
duration?: number,
easing?: (t: number
t: number) => number,
css?: (t: number
t: number, u: number
u: number) => string,
tick?: (t: number
t: number, u: number
u: number) => void
}
アニメーションは、node
、animation
オブジェクト、および任意のパラメータを引数として受け取るカスタム関数を使用できます。animation
パラメータは、それぞれstart
およびend
位置での要素のジオメトリを表すDOMRectを含むfrom
およびto
プロパティを含むオブジェクトです。from
プロパティは、開始位置での要素のDOMRectであり、to
プロパティは、リストの並べ替えとDOMの更新後の最終位置での要素のDOMRectです。
返されたオブジェクトにcss
メソッドが含まれている場合、Svelteは要素で再生されるウェブアニメーションを作成します。
css
に渡されるt
引数は、easing
関数の適用後に0
から1
までの値になります。u
引数は1 - t
と等しくなります。
この関数は、アニメーションが開始される前に、異なるt
およびu
引数で繰り返し呼び出されます。
<script>
import { cubicOut } from 'svelte/easing';
/**
* @param {HTMLElement} node
* @param {{ from: DOMRect; to: DOMRect }} states
* @param {any} params
*/
function whizz(node, { from, to }, params) {
const dx = from.left - to.left;
const dy = from.top - to.top;
const d = Math.sqrt(dx * dx + dy * dy);
return {
delay: 0,
duration: Math.sqrt(d) * 120,
easing: cubicOut,
css: (t, u) => `transform: translate(${u * dx}px, ${u * dy}px) rotate(${t * 360}deg);`
};
}
</script>
{#each list as item, index (item)}
<div animate:whizz>{item}</div>
{/each}
<script lang="ts">
import { cubicOut } from 'svelte/easing';
function whizz(node: HTMLElement, { from, to }: { from: DOMRect; to: DOMRect }, params: any) {
const dx = from.left - to.left;
const dy = from.top - to.top;
const d = Math.sqrt(dx * dx + dy * dy);
return {
delay: 0,
duration: Math.sqrt(d) * 120,
easing: cubicOut,
css: (t, u) => `transform: translate(${u * dx}px, ${u * dy}px) rotate(${t * 360}deg);`
};
}
</script>
{#each list as item, index (item)}
<div animate:whizz>{item}</div>
{/each}
カスタムアニメーション関数は、同じt
およびu
引数でアニメーション中に呼び出されるtick
関数を返すこともできます。
tick
ではなくcss
を使用できる場合は、そうしてください。ウェブアニメーションはメインスレッドから実行できるため、低速なデバイスでのジャンクを防止できます。
<script>
import { cubicOut } from 'svelte/easing';
/**
* @param {HTMLElement} node
* @param {{ from: DOMRect; to: DOMRect }} states
* @param {any} params
*/
function whizz(node, { from, to }, params) {
const dx = from.left - to.left;
const dy = from.top - to.top;
const d = Math.sqrt(dx * dx + dy * dy);
return {
delay: 0,
duration: Math.sqrt(d) * 120,
easing: cubicOut,
tick: (t, u) => Object.assign(node.style, { color: t > 0.5 ? 'Pink' : 'Blue' })
};
}
</script>
{#each list as item, index (item)}
<div animate:whizz>{item}</div>
{/each}
<script lang="ts">
import { cubicOut } from 'svelte/easing';
function whizz(node: HTMLElement, { from, to }: { from: DOMRect; to: DOMRect }, params: any) {
const dx = from.left - to.left;
const dy = from.top - to.top;
const d = Math.sqrt(dx * dx + dy * dy);
return {
delay: 0,
duration: Math.sqrt(d) * 120,
easing: cubicOut,
tick: (t, u) => Object.assign(node.style, { color: t > 0.5 ? 'Pink' : 'Blue' })
};
}
</script>
{#each list as item, index (item)}
<div animate:whizz>{item}</div>
{/each}