コンテンツにスキップ

useAnimate

カテゴリ
エクスポートサイズ
1.56 KB
最終更新日
先週

リアクティブな Web Animations API.

デモ

VueUse useAnimate

startTime: null
currentTime: null
playbackRate: 1
playState: 'idle'
replaceState: 'active'
pending: false

使用方法

基本的な使い方

useAnimate 関数は、アニメーションとその制御関数を返します。

vue
<script setup>
import { useAnimate } from '@vueuse/core'
import { ref } from 'vue'

const el = ref()
const {
  isSupported,
  animate,

  // actions
  play,
  pause,
  reverse,
  finish,
  cancel,

  // states
  pending,
  playState,
  replaceState,
  startTime,
  currentTime,
  timeline,
  playbackRate,
} = useAnimate(el, { transform: 'rotate(360deg)' }, 1000)
</script>

<template>
  <span ref="el" style="display:inline-block">useAnimate</span>
</template>

カスタムキーフレーム

キーフレームオブジェクトの配列、キーフレームオブジェクト、または ref のいずれかです。詳細は キーフレームフォーマット を参照してください。

ts
const keyframes = { transform: 'rotate(360deg)' }
// Or
const keyframes = [
  { transform: 'rotate(0deg)' },
  { transform: 'rotate(360deg)' },
]
// Or
const keyframes = ref([
  { clipPath: 'circle(20% at 0% 30%)' },
  { clipPath: 'circle(20% at 50% 80%)' },
  { clipPath: 'circle(20% at 100% 30%)' },
])

useAnimate(el, keyframes, 1000)

型宣言

型宣言を表示
typescript
export interface UseAnimateOptions
  extends KeyframeAnimationOptions,
    ConfigurableWindow {
  /**
   * Will automatically run play when `useAnimate` is used
   *
   * @default true
   */
  immediate?: boolean
  /**
   * Whether to commits the end styling state of an animation to the element being animated
   * In general, you should use `fill` option with this.
   *
   * @default false
   */
  commitStyles?: boolean
  /**
   * Whether to persists the animation
   *
   * @default false
   */
  persist?: boolean
  /**
   * Executed after animation initialization
   */
  onReady?: (animate: Animation) => void
  /**
   * Callback when error is caught.
   */
  onError?: (e: unknown) => void
}
export type UseAnimateKeyframes = MaybeRef<
  Keyframe[] | PropertyIndexedKeyframes | null
>
export interface UseAnimateReturn {
  isSupported: Ref<boolean>
  animate: ShallowRef<Animation | undefined>
  play: () => void
  pause: () => void
  reverse: () => void
  finish: () => void
  cancel: () => void
  pending: ComputedRef<boolean>
  playState: ComputedRef<AnimationPlayState>
  replaceState: ComputedRef<AnimationReplaceState>
  startTime: WritableComputedRef<CSSNumberish | number | null>
  currentTime: WritableComputedRef<CSSNumberish | null>
  timeline: WritableComputedRef<AnimationTimeline | null>
  playbackRate: WritableComputedRef<number>
}
/**
 * Reactive Web Animations API
 *
 * @see https://vueuse.dokyumento.jp/useAnimate
 * @param target
 * @param keyframes
 * @param options
 */
export declare function useAnimate(
  target: MaybeComputedElementRef,
  keyframes: UseAnimateKeyframes,
  options?: number | UseAnimateOptions,
): UseAnimateReturn

ソース

ソースデモドキュメント

貢献者

Anthony Fu
Anthony Fu
JianJroh
a1mer
huiliangShen
丶远方
jack zhang
qiang

変更履歴

v12.0.0-beta.1 2024/11/21
0a9ed - feat!: Vue 2 サポートの削除、バンドルの最適化、クリーンアップ (#4349)
v10.10.0 2024/5/27
fba4e - fix: 完了時の commitStyles (#3990)
v10.8.0 2024/2/20
12c09 - fix: immediate オプションを false に設定しても機能しない (#3763)
v10.0.0-beta.4 2023/4/13
bcf5d - feat: 新しい関数 (#2109)

MITライセンスに基づいてリリースされています。