跳至內容

useAnimate

類別
匯出大小
1.64 kB
上次變更
5 天前

響應式 Web Animations API

示範

VueUse useAnimate

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

用法

基本用法

useAnimate 函式將會回傳 animate 及其控制函式。

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

const el = useTemplateRef('el')
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: ComputedRef<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.dev.org.tw/useAnimate
 * @param target
 * @param keyframes
 * @param options
 */
export declare function useAnimate(
  target: MaybeComputedElementRef,
  keyframes: UseAnimateKeyframes,
  options?: number | UseAnimateOptions,
): UseAnimateReturn

原始碼

原始碼示範文件

貢獻者

Anthony Fu
Anthony Fu
IlyaL
James Garbutt
liyan
bab
Fernando Fernández
Alex Liu
Robin
JianJroh
a1mer
huiliangShen
丶远方
jack zhang
qiang

變更日誌

v12.8.0 於 2025/3/5
7432f - feat(types): 棄用 MaybeRefMaybeRefOrGetter,改用 Vue 原生型別 (#4636)
e8665 - fix: 修正更新關鍵影格的條件 (#4619)
v12.6.0 於 2025/2/14
1a934 - fix: 當元素消失時清除動畫 (#4579)
v12.5.0 於 2025/1/22
eddbf - feat: 更多被動事件處理器 (#4484)
v12.3.0 於 2025/1/2
59f75 - feat(toValue): 棄用來自 @vueuse/sharedtoValue,改用 Vue 原生型別
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 授權條款發布。