Vue.js course ยท Module 13: Transitions & Animations
JavaScript Animation Hooks
In this lesson7
CSS classes are the "autopilot" of animations - they work great for simple transitions. But sometimes you need full control over animations - e.g., dynamically computed values, data-driven animations, or integration with JS libraries. In such cases, Vue offers JavaScript animation hooks - like manual rocket steering instead of autopilot.
Available Hooks
<Transition> emits events at each stage of the animation. You can listen to them:
<Transition
:css="false"
@before-enter="onBeforeEnter"
@enter="onEnter"
@after-enter="onAfterEnter"
@enter-cancelled="onEnterCancelled"
@before-leave="onBeforeLeave"
@leave="onLeave"
@after-leave="onAfterLeave"
@leave-cancelled="onLeaveCancelled"
>
<div v-if="show">...</div>
</Transition>Enter Hooks - Execution Order
@before-enter(el)- called before the element is inserted into the DOM. Here you set the initial animation state (e.g.,opacity: 0,scale: 0.5).@enter(el, done)- called on the next frame after the element is inserted. Here you run the actual animation. You must calldone()when the animation finishes!@after-enter(el)- called after the animation finishes (afterdone()). Here you clean up after the animation (remove inline styles, etc.).@enter-cancelled(el)- called when the enter animation is interrupted (e.g., the user quickly toggles the element).
Leave Hooks - Analogous Order
@before-leave(el)- initial state of leave, element is still visible@leave(el, done)- leave animation, you must calldone()!@after-leave(el)- cleanup after leave@leave-cancelled(el)- leave animation interrupted (only withv-show)
The :css="false" Attribute
When using only JS hooks, add :css="false" so Vue doesn't try to apply CSS classes or listen for transitionend/animationend events:
<Transition :css="false" @enter="onEnter" @leave="onLeave">
<div v-if="show">Control panel</div>
</Transition>Without this attribute, Vue will wait for both the done() callback and the CSS transition event - which can cause unpredictable behavior.
The done() Callback - A Key Element
done() is mandatory in the @enter and @leave hooks. It tells Vue that the animation has finished and it can proceed to the next step:
function onBeforeEnter(el) {
// Initial state - element invisible
el.style.opacity = 0
el.style.transform = 'translateY(20px)'
}
function onEnter(el, done) {
// Force reflow so the browser registers the initial state
el.offsetHeight
// Start the animation
el.style.transition = 'all 0.5s ease'
el.style.opacity = 1
el.style.transform = 'translateY(0)'
// Notify Vue when the animation finishes
el.addEventListener('transitionend', done, { once: true })
}
function onAfterEnter(el) {
// Cleanup - remove inline transition
el.style.transition = ''
}Without calling done(), Vue doesn't know when the animation has finished - in the case of @leave, the element will never be removed from the DOM.
Practical Example - Data-Driven Animation
JS hooks allow dynamic values, which CSS classes cannot provide:
function onEnter(el, done) {
// Dynamic animation direction based on data
const direction = el.dataset.direction || 'left'
const offset = direction === 'left' ? '-100px' : '100px'
el.style.opacity = 0
el.style.transform = `translateX(${offset})`
el.offsetHeight // reflow
el.style.transition = 'all 0.4s ease-out'
el.style.opacity = 1
el.style.transform = 'translateX(0)'
el.addEventListener('transitionend', done, { once: true })
}When to Use JS Hooks Instead of CSS?
| Use CSS classes | Use JS hooks |
|---|---|
| Simple fade, slide | Dynamic animation values |
| Fixed durations | Data-dependent durations |
| Typical transitions | Integration with GSAP, Anime.js |
| Most cases | Complex animation sequences |
Code for this lesson: App.vue
<script setup>
import { ref } from 'vue'
const showAlert = ref(false)
function onBeforeEnter(el) {
el.style.opacity = 0
el.style.transform = 'scale(0.5)'
}
function onEnter(el, done) {
// Entry animation without CSS
el.offsetHeight // force reflow
el.style.transition = 'all 0.6s ease'
el.style.opacity = 1
el.style.transform = 'scale(1)'
el.addEventListener('transitionend', done, { once: true })
}
function onLeave(el, done) {
el.style.transition = 'all 0.4s ease'
el.style.opacity = 0
el.style.transform = 'scale(0.5) rotate(10deg)'
el.addEventListener('transitionend', done, { once: true })
}
</script>
<template>
<div class="nova-lab">
<h1>JavaScript Animation Hooks</h1>
<button @click="showAlert = !showAlert" class="btn">
{{ showAlert ? 'Close alert' : 'Show alert' }}
</button>
<Transition
:css="false"
@before-enter="onBeforeEnter"
@enter="onEnter"
@leave="onLeave"
>
<div v-if="showAlert" class="alert">
WARNING: Gravitational anomaly detected in sector 7!
</div>
</Transition>
</div>
</template>
<style scoped>
.nova-lab {
background: #0a0e27;
color: #00ff88;
min-height: 100vh;
padding: 20px;
font-family: monospace;
}
h1 { color: #00ff88; }
.btn {
background: #e74c3c;
color: white;
border: none;
padding: 10px 20px;
border-radius: 8px;
cursor: pointer;
margin: 15px 0;
}
.alert {
background: rgba(231, 76, 60, 0.2);
border: 2px solid #e74c3c;
color: #e74c3c;
padding: 20px;
border-radius: 12px;
font-size: 16px;
}
</style>Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. Which JavaScript hook is called when an element enters the DOM?
2. What should be called at the end of the @enter hook to inform Vue that the animation has finished?
Hands-on tasks in the game
- Vertical ordering
Arrange the JavaScript Transition hooks in the order they are called (enter):
- Code editor
Implement @enter and @leave using el.style and the done() callback.
- Click in order
Arrange the @enter hook code in the correct order:
- Horizontal ordering
Arrange the Transition syntax that disables CSS classes:
- Code editor
Use :css='false' and JS hooks to animate opacity and scale.
- Horizontal ordering
Arrange the @leave hook declaration in the template: