主题
useAnimate
响应式 Web 动画 API。
🌐 Reactive Web Animations API.
示例
VueUse useAnimate
startTime: null currentTime: null playbackRate: 1 playState: idle replaceState: active pending: false
用法
🌐 Usage
基本用法
🌐 Basic Usage
useAnimate 函数返回动画实例和控制函数。
🌐 The useAnimate function returns the animation instance and control functions.
vue
<script setup lang="ts">
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>自定义关键帧
🌐 Custom Keyframes
可以是关键帧对象的数组、单个关键帧对象,或者一个 ref。更多详情请参阅 关键帧格式。
🌐 Either an array of keyframe objects, or a keyframe object, or a ref. See Keyframe Formats for more details.
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)选项
🌐 Options
第三个参数接受一个持续时间数字或一个选项对象,该对象在 KeyframeAnimationOptions 的基础上具有以下附加属性:
🌐 The third argument accepts a duration number or an options object with the following additional properties on top of KeyframeAnimationOptions:
ts
import { useAnimate } from '@vueuse/core'
useAnimate(el, keyframes, {
duration: 1000,
// Start playing immediately (default: true)
immediate: true,
// Commit the end styling state to the element (default: false)
commitStyles: false,
// Persist the animation (default: false)
persist: false,
// Callback when animation is initialized
onReady(animate) {
console.log('Animation ready', animate)
},
// Callback when an error occurs
onError(e) {
console.error('Animation error', e)
},
})延迟启动
🌐 Delaying Start
将 immediate: false 设置为防止动画自动开始。
🌐 Set immediate: false to prevent the animation from starting automatically.
ts
import { useAnimate } from '@vueuse/core'
const { play } = useAnimate(el, keyframes, {
duration: 1000,
immediate: false,
})
// Start the animation manually
play()