Skip to content

useAnimate

类别
导出体积
1.64 kB
上次更改
4 months ago

响应式的 Web Animations API

示例

VueUse useAnimate

startTime: 3571.9979999999996
currentTime: 3150.0020000000004
playbackRate: 1
playState: running
replaceState: active
pending: false

用法

基本用法

useAnimate 函数将返回动画及其控制函数。

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)' }
// 或者
const keyframes = [
  { transform: 'rotate(0deg)' },
  { transform: 'rotate(360deg)' },
]
// 或者
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 {
  /**
   * 当使用 `useAnimate` 时,是否自动运行播放
   *
   * @default true
   */
  immediate?: boolean
  /**
   * 是否将动画的结束样式状态提交给被动画的元素
   * 通常情况下,你应该使用 `fill` 选项
   *
   * @default false
   */
  commitStyles?: boolean
  /**
   * 是否持续动画
   *
   * @default false
   */
  persist?: boolean
  /**
   * 在动画初始化后执行
   */
  onReady?: (animate: Animation) => void
  /**
   * 捕获到错误时的回调
   */
  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>
}
/**
 * 响应式 Web Animations API
 *
 * @see https://vueuse.org/useAnimate
 * @param target
 * @param keyframes
 * @param options
 */
export declare function useAnimate(
  target: MaybeComputedElementRef,
  keyframes: UseAnimateKeyframes,
  options?: number | UseAnimateOptions,
): UseAnimateReturn

源码

源码演示文档

贡献者

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

更新日志

v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
e8665 - fix: Correct condition for updating keyframes (#4619)
v12.6.0 on
1a934 - fix: Clear animate when element is gone (#4579)
v12.5.0 on
eddbf - feat: more passive event handlers (#4484)
v12.3.0 on
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.10.0 on
fba4e - fix: commitStyles when finish (#3990)
v10.8.0 on
12c09 - fix: immediate option set false not working (#3763)
v10.0.0-beta.4 on
bcf5d - feat: new function (#2109)