跳到内容

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.org.cn/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/shared 中的 toValue,转而使用 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 许可证下发布。