Vue.js course ยท Module 13: Transitions & Animations

Integration with Animation Libraries

4 min read ยท
In this lesson5

NOVA LAB uses cutting-edge technology - holograms, 3D interfaces, dynamic control panels. Similarly, you can enrich your Vue animations by integrating them with popular CSS and JavaScript libraries. Vue's <Transition> was designed with such integration in mind - the custom class names system and JS hooks make it straightforward.

Custom Class Names

Instead of the automatic classes name-enter-active, name-leave-to, etc., you can specify your own CSS classes. This is crucial when integrating with libraries such as Animate.css:

<Transition
  enter-active-class="animate__animated animate__bounceIn"
  leave-active-class="animate__animated animate__bounceOut"
>
  <div v-if="show">Station status panel</div>
</Transition>

Vue will apply the provided CSS classes directly to the element instead of its automatic ones. You have 6 custom class attributes at your disposal - one for each animation phase:

  • enter-from-class - replaces name-enter-from
  • enter-active-class - replaces name-enter-active
  • enter-to-class - replaces name-enter-to
  • leave-from-class - replaces name-leave-from
  • leave-active-class - replaces name-leave-active
  • leave-to-class - replaces name-leave-to

You can mix custom classes with named transitions - the specified attributes will override only selected phases, while the rest will use the name prefix.

CSS @keyframes

In addition to CSS transition (changing from value A to B), you can use CSS animation with @keyframes - this gives you control over multi-step animations:

.bounce-enter-active {
  animation: bounce-in 0.5s;
}
.bounce-leave-active {
  animation: bounce-in 0.5s reverse;
}

@keyframes bounce-in {
  0% { transform: scale(0); opacity: 0; }
  50% { transform: scale(1.15); }
  70% { transform: scale(0.95); }
  100% { transform: scale(1); opacity: 1; }
}

The bounce-in animation consists of 4 steps - the element grows, slightly shrinks (a "spring" effect), and stabilizes. On leave (reverse), the animation plays backwards.

Difference Between transition and animation

CSS transitionCSS animation
Change from A to BMultiple steps (keyframes)
Requires state change (-from, -to)Defines a full sequence
SimplerMore flexible
Single transitionCan repeat

In Vue, you can combine both approaches. When an element has both transition and animation, use the type attribute to tell Vue which one to wait for:

<Transition name="mixed" type="animation">
  <!-- Vue will wait for the animation to finish, not the transition -->
</Transition>

Integration with GSAP

GSAP (GreenSock Animation Platform) is the most powerful animation library in the JavaScript ecosystem. Integration with Vue is done through JS hooks:

import gsap from 'gsap'

function onBeforeEnter(el) {
  el.style.opacity = 0
  el.style.transform = 'scale(0.5) translateY(30px)'
}

function onEnter(el, done) {
  gsap.to(el, {
    opacity: 1,
    scale: 1,
    y: 0,
    duration: 0.6,
    ease: 'elastic.out(1, 0.5)',
    onComplete: done
  })
}

function onLeave(el, done) {
  gsap.to(el, {
    opacity: 0,
    scale: 0.5,
    y: -30,
    duration: 0.4,
    ease: 'power2.in',
    onComplete: done
  })
}
<Transition
  :css="false"
  @before-enter="onBeforeEnter"
  @enter="onEnter"
  @leave="onLeave"
>
  <div v-if="showPanel">Reactor Control Panel</div>
</Transition>

Important: When using GSAP (or any JS library), add :css="false" to Transition so Vue doesn't try to apply CSS classes simultaneously.

Integration with Animate.css

Animate.css is a popular library of ready-made CSS animations. After adding it to your project:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" />

Using it with Vue is trivial thanks to custom class names:

<Transition
  enter-active-class="animate__animated animate__fadeInUp"
  leave-active-class="animate__animated animate__fadeOutDown"
>
  <div v-if="visible" class="notification">
    New sensor reading available
  </div>
</Transition>

You can control the duration through CSS custom properties:

.animate__animated {
  --animate-duration: 0.4s;
}

Choosing the Right Approach

SituationApproach
Simple fade/slideCSS transition with named classes
Multi-step animationsCSS @keyframes
Ready-made effectsAnimate.css + custom classes
Dynamic/complexGSAP + JS hooks
Physics simulationsGSAP with ease functions
Code for this lesson: App.vue
<script setup>
import { ref } from 'vue'

const showBox = ref(false)
</script>

<template>
  <div class="nova-lab">
    <h1>Integration with Animate.css</h1>

    <button @click="showBox = !showBox" class="btn">
      Toggle
    </button>

    <!-- Custom class names with Animate.css -->
    <Transition
      enter-active-class="animate__animated animate__bounceIn"
      leave-active-class="animate__animated animate__bounceOut"
    >
      <div v-if="showBox" class="cargo-box">
        Cargo Module Alpha-7
        <p>Status: Loaded</p>
      </div>
    </Transition>

    <h2 style="margin-top: 30px;">CSS @keyframes</h2>
    <button @click="showBox = !showBox" class="btn">Toggle Keyframes</button>

    <Transition name="bounce">
      <div v-if="showBox" class="cargo-box alt">
        Science Module Beta-3
      </div>
    </Transition>
  </div>
</template>

<style scoped>
.nova-lab {
  background: #0a0e27;
  color: #00ff88;
  min-height: 100vh;
  padding: 20px;
  font-family: monospace;
}
h1, h2 { color: #00ff88; }
.btn {
  background: #00b4d8;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 8px;
  cursor: pointer;
  margin: 10px 0;
}
.cargo-box {
  background: rgba(0, 180, 216, 0.15);
  border: 2px solid #00b4d8;
  padding: 20px;
  border-radius: 12px;
  margin-top: 10px;
}
.cargo-box.alt {
  border-color: #9b59b6;
  background: rgba(155, 89, 182, 0.15);
}

/* @keyframes animation */
.bounce-enter-active {
  animation: bounce-in 0.5s;
}
.bounce-leave-active {
  animation: bounce-in 0.5s reverse;
}
@keyframes bounce-in {
  0% { transform: scale(0); }
  50% { transform: scale(1.15); }
  100% { transform: scale(1); }
}
</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. 1. What is the best way to integrate GSAP with <Transition> in Vue?

  2. 2. Which attributes allow you to override the default Transition classes?

Hands-on tasks in the game

  • Vertical ordering

    Arrange the steps for integrating GSAP with Vue Transition:

  • Code editor

    Use the enter-active-class and leave-active-class attributes with Animate.css classes.

  • Horizontal ordering

    Arrange the custom class attribute for enter-active-class:

  • Code editor

    Use gsap.from() in the @enter hook and gsap.to() in @leave.

  1. QuestionGSAP in Vue
  2. Put in orderGSAP Integration Steps
  3. Coding taskPractice: Animate.css with Vue
  4. Put in orderCustom Class Names
  5. QuestionCustom Transition Classes
  6. Coding taskPractice: GSAP in Transition

Useful articles