Skip to content

useAnimate

Category
Export Size
1.75 kB
Last Changed
3 months ago

Reactive Web Animations API.

Demo

VueUse useAnimate

startTime: 2212.686
currentTime: 1316.614
playbackRate: 1
playState: running
replaceState: active
pending: false

Usage

Basic Usage

The useAnimate function will return the animate and its control function.

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

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)

Type Declarations

Show Type Declarations
ts
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/useAnimate * @param target * @param keyframes * @param options */ export declare function
useAnimate
(
target
:
MaybeComputedElementRef
,
keyframes
:
UseAnimateKeyframes
,
options
?: number | UseAnimateOptions,
): UseAnimateReturn

Source

SourceDemoDocs

Contributors

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

Changelog

v13.7.0 on
90c7f - fix: respect immediate: false with conditionally rendered elements (#4947)
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)

Released under the MIT License.

FREE WEEKEND
Unlimited access to ALL Vue School courses
8-9 November 2025
Reserve Your Spot
8-9 November 2025