0
0
Vueframework~10 mins

Enter and leave transitions in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Enter and leave transitions
Element State: Not in DOM
Trigger Enter Transition
Add Element to DOM
Apply Enter Classes
Transition Animation Runs
Remove Enter Classes
Element Fully Visible
Trigger Leave Transition
Apply Leave Classes
Transition Animation Runs
Remove Element from DOM
This flow shows how Vue adds an element with enter transition and removes it with leave transition, applying CSS classes to animate.
Execution Sample
Vue
<template>
  <transition name="fade">
    <p v-if="show">Hello!</p>
  </transition>
</template>

<script setup>
import { ref } from 'vue'
const show = ref(false)
setTimeout(() => show.value = true, 1000)
setTimeout(() => show.value = false, 4000)
</script>
This Vue code shows a paragraph that fades in after 1 second and fades out after 4 seconds using enter and leave transitions.
Execution Table
StepTrigger/EventElement StateClasses AppliedDOM ChangeDescription
1InitialNot in DOMNoneNo elementParagraph is not rendered initially
2show.value = true (after 1s)Add elementfade-enter-from fade-enter-activeElement addedElement inserted with enter classes to start fade-in
3Transition animation runningIn DOMfade-enter-to fade-enter-activeElement presentFade-in animation runs
4Animation endsIn DOMNoneElement presentEnter classes removed, element fully visible
5show.value = false (after 4s)Start leavefade-leave-from fade-leave-activeElement presentLeave class added to start fade-out
6Transition animation runningIn DOMfade-leave-to fade-leave-activeElement presentFade-out animation runs
7Animation endsRemove elementNoneElement removedElement removed from DOM after leave transition
💡 Element removed from DOM after leave transition finishes
Variable Tracker
VariableStartAfter Step 2After Step 5Final
show.valuefalsetruefalsefalse
Key Moments - 2 Insights
Why does the element stay in the DOM during the leave transition even though show.value is false?
Vue keeps the element in the DOM during the leave transition to allow the animation to run. See execution_table rows 5-7 where leave classes are applied before removal.
What is the difference between fade-enter-from and fade-enter-active classes?
fade-enter-from sets the start state of the animation, while fade-enter-active controls the animation duration and easing. They work together during the enter transition (rows 2-3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what classes are applied when the element first appears in the DOM?
Afade-leave-active
Bfade-leave-from
Cfade-enter-from
DNo classes
💡 Hint
Check Step 2 in the execution_table where the element is added with enter classes.
At which step does the element get removed from the DOM?
AStep 7
BStep 5
CStep 4
DStep 3
💡 Hint
Look for the step where DOM Change says 'Element removed' in the execution_table.
If show.value never changes to false, which steps would NOT happen?
ASteps 2 and 3
BSteps 5, 6, and 7
CStep 1 only
DAll steps happen anyway
💡 Hint
Refer to variable_tracker and execution_table steps related to leave transition.
Concept Snapshot
Vue enter and leave transitions animate elements when they appear or disappear.
Use <transition> with CSS classes: fade-enter-from, fade-enter-active for entering,
fade-leave-from, fade-leave-active for leaving.
Vue keeps elements in DOM during leave to run animation before removal.
Transitions trigger on v-if or v-show changes controlling element presence.
Full Transcript
This visual execution shows how Vue handles enter and leave transitions. Initially, the element is not in the DOM. When the reactive variable 'show' changes to true, Vue adds the element and applies 'fade-enter-from' and 'fade-enter-active' classes to start the fade-in animation. After the animation finishes, the classes are removed and the element is fully visible. When 'show' changes to false, Vue applies 'fade-leave-from' and 'fade-leave-active' classes to start the fade-out animation but keeps the element in the DOM. After the animation ends, Vue removes the element from the DOM. This process ensures smooth animations for elements appearing and disappearing.