Vue.js course Β· Module 13: Transitions & Animations
Advanced Animation Patterns
In this lesson5
In NOVA LAB's advanced systems, we need sophisticated effects - cascading dashboard element loading, animation on first page display, precise duration control, and respect for users' motion preferences. Learn the patterns that give interfaces a professional feel.
Appear - Animation on Mount
By default, <Transition> only animates the element when toggling v-if/v-show. The appear attribute causes the animation to also run on the first render (mount) of the component:
<Transition appear name="fade">
<div>This element animates on page load</div>
</Transition>This is the ideal solution for elements visible right after loading - the station logo, dashboard header, status panel. Without appear, these elements would simply "be there" without any enter animation.
You can use special CSS classes for the appear animation (independent from the normal enter classes):
<Transition
appear
appear-from-class="appear-start"
appear-active-class="appear-running"
appear-to-class="appear-end"
>
<div>Custom appear animation</div>
</Transition>Or with libraries:
<Transition
appear
appear-active-class="animate__animated animate__fadeInDown"
>
<header>NOVA LAB Control Center</header>
</Transition>Staggered List Animation
One of the most impressive patterns - cascading appearance of list elements, where each successive element appears with an increasing delay. It looks like data displaying on a NOVA LAB holographic dashboard, element by element:
<TransitionGroup
appear
:css="false"
@before-enter="onBeforeEnter"
@enter="onEnter"
tag="div"
>
<div v-for="(item, index) in items" :key="item.id" :data-index="index">
{{ item.name }}
</div>
</TransitionGroup>The key is the :data-index attribute - it passes the element index to the JS hook, which calculates the delay:
function onBeforeEnter(el) {
el.style.opacity = 0
el.style.transform = 'translateY(20px)'
}
function onEnter(el, done) {
// Each successive element has a 150ms greater delay
const delay = el.dataset.index * 150
setTimeout(() => {
el.style.transition = 'all 0.5s ease'
el.style.opacity = 1
el.style.transform = 'translateY(0)'
el.addEventListener('transitionend', done, { once: true })
}, delay)
}The effect: element 0 appears immediately, element 1 after 150ms, element 2 after 300ms, etc. For 10 elements, the entire animation takes 1.5s + 0.5s (animation duration itself) = ~2s.
Tip: Limit the delay to a max of ~1s so the user doesn't wait too long for the last elements:
const delay = Math.min(el.dataset.index * 100, 1000)Explicit Duration
When an element has both CSS transition and animation, or when nested elements have different animation durations, Vue may not correctly detect the end point. The :duration attribute lets you explicitly specify the duration in milliseconds:
<!-- Same duration for enter and leave -->
<Transition :duration="500">...</Transition>
<!-- Different durations for enter and leave -->
<Transition :duration="{ enter: 300, leave: 500 }">...</Transition>This is particularly useful when:
- Animating nested elements with different durations
- Combining CSS transition and animation on the same element
- Using external libraries whose duration isn't automatically detected
Accessibility - prefers-reduced-motion
Respect users' motion settings. People with vestibular disorders or epilepsy may have the prefers-reduced-motion preference enabled. Your CSS should honor this:
/* Normal animations */
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.3s ease, transform 0.3s ease;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
transform: translateY(10px);
}
/* Disable animations for users who don't want them */
@media (prefers-reduced-motion: reduce) {
.fade-enter-active,
.fade-leave-active {
transition: none;
}
.fade-enter-from,
.fade-leave-to {
opacity: 1;
transform: none;
}
}You can also handle this in JavaScript:
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches
function onEnter(el, done) {
if (prefersReducedMotion) {
done() // Skip the animation
return
}
// Normal animation...
}Performance Best Practices
Only animate
transformandopacity- these properties are GPU-accelerated and don't cause reflow/repaint. Avoid animatingwidth,height,top,left,margin,padding.Use
will-changefor heavy animations - it informs the browser about an upcoming animation, allowing for optimization:
.heavy-animation-enter-active {
will-change: transform, opacity;
transition: all 0.5s ease;
}Prefer CSS over JS - CSS animations are optimized by the browser and run on a separate thread. Use JS hooks only when you need dynamic values.
Short durations - UI animations should last 200-500ms. Longer animations slow down interaction and frustrate users.
Test on mobile devices - animations that run smoothly on desktop may lag on older phones.
Code for this lesson: App.vue
<script setup>
import { ref } from 'vue'
// Appear - animation on mount
const items = ref([
{ id: 1, name: 'Life Support', delay: 0 },
{ id: 2, name: 'Navigation', delay: 100 },
{ id: 3, name: 'Communications', delay: 200 },
{ id: 4, name: 'Power Grid', delay: 300 },
{ id: 5, name: 'Science Lab', delay: 400 }
])
function onBeforeEnter(el) {
el.style.opacity = 0
el.style.transform = 'translateY(20px)'
}
function onEnter(el, done) {
const delay = el.dataset.index * 150
setTimeout(() => {
el.style.transition = 'all 0.5s ease'
el.style.opacity = 1
el.style.transform = 'translateY(0)'
el.addEventListener('transitionend', done, { once: true })
}, delay)
}
</script>
<template>
<div class="nova-lab">
<h1>Staggered List + Appear</h1>
<TransitionGroup
appear
:css="false"
@before-enter="onBeforeEnter"
@enter="onEnter"
tag="div"
class="systems"
>
<div
v-for="(item, index) in items"
:key="item.id"
:data-index="index"
class="system-card"
>
<span class="dot">β</span>
{{ item.name }}
</div>
</TransitionGroup>
</div>
</template>
<style scoped>
.nova-lab {
background: #0a0e27;
color: #00ff88;
min-height: 100vh;
padding: 20px;
font-family: monospace;
}
h1 { color: #00ff88; }
.systems { margin-top: 20px; }
.system-card {
display: flex;
align-items: center;
gap: 12px;
background: rgba(0, 180, 216, 0.1);
border: 1px solid #00b4d8;
padding: 14px 20px;
margin: 8px 0;
border-radius: 8px;
font-size: 14px;
}
.dot { color: #00ff88; }
</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. What does the appear attribute do in <Transition>?
2. How do you set an explicit animation duration in <Transition>?
These are 2 of 4 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Vertical ordering
Arrange the steps for implementing a staggered (delayed) list animation:
- Code editor
Use data-index and gsap/setTimeout for staggered animation in TransitionGroup.
- Horizontal ordering
Arrange the appear attribute with a custom class:
- Click in order
Arrange the code for passing an index to a list element:
- Vertical ordering
Arrange the animation techniques from simplest to most advanced:
- Horizontal ordering
Arrange the duration syntax with different enter/leave times:
- Click in order
Arrange the JavaScript hooks in the order they are called (leave animation):
- Click in order
Arrange the CSS transition shorthand declaration:
- Vertical ordering
Arrange the 6 CSS classes added by <Transition> in chronological order:
Tasks after this lesson
- QuestionThe appear Prop
- Put in orderStaggered List Animation
- Coding taskPractice: Staggered Animation
- Put in orderAppear Classes
- Click in orderData Attribute in Stagger
- Put in orderCSS vs JS Animation Types
- QuestionThe duration Prop
- Put in orderDuration with an Object
- Click in orderLeave Hooks
- QuestionVue Animation Best Practices
- Questionprefers-reduced-motion
- Click in orderCSS Transition Properties
- Put in orderThe 6 CSS Transition Classes