Transition modes control how Vue switches between elements during animations. They help make smooth visual changes when content updates.
Transition modes (in-out, out-in) in Vue
<transition mode="in-out|out-in"> ... </transition>The mode attribute accepts two values: in-out and out-in.
in-out means the new element enters first, then the old leaves.
out-in means the old element leaves first, then the new enters.
<transition mode="in-out"> <component :is="view" /> </transition>
<transition mode="out-in"> <component :is="view" /> </transition>
This Vue component toggles between two views labeled 'A' and 'B'. The transition uses mode="out-in" so the old box fades out fully before the new box fades in. This avoids overlap and flicker.
<template>
<div>
<button @click="toggle">Toggle View</button>
<transition name="fade" mode="out-in">
<div :key="view" class="box">
{{ view }}
</div>
</transition>
</div>
</template>
<script setup>
import { ref } from 'vue'
const view = ref('A')
function toggle() {
view.value = view.value === 'A' ? 'B' : 'A'
}
</script>
<style scoped>
.box {
width: 100px;
height: 100px;
display: flex;
align-items: center;
justify-content: center;
background-color: #42b983;
color: white;
font-weight: bold;
border-radius: 0.5rem;
margin-top: 1rem;
}
.fade-enter-active, .fade-leave-active {
transition: opacity 0.5s ease;
}
.fade-enter-from, .fade-leave-to {
opacity: 0;
}
</style>Use mode only on <transition> components wrapping a single element or component.
Without mode, Vue runs enter and leave animations simultaneously.
Choose in-out or out-in depending on which animation order looks better for your UI.
Transition modes control the order of enter and leave animations.
in-out: new element enters first, old leaves after.
out-in: old element leaves first, new enters after.
Practice
in-out transition mode do in Vue transitions?Solution
Step 1: Understand transition modes
Vue transition modes control the order of entering and leaving animations.Step 2: Recall
in-outbehaviorin-outmeans the new element enters first, then the old element leaves.Final Answer:
The new element enters first, then the old element leaves. -> Option DQuick Check:
in-out = new enters first [OK]
- Confusing in-out with out-in order
- Thinking both enter and leave happen simultaneously
- Assuming no animation delay
out-in transition mode in a Vue <transition> component?Solution
Step 1: Identify correct mode attribute
Themodeattribute on<transition>sets the transition mode.Step 2: Match mode value for out-in
The correct value for the mode that makes old element leave first is "out-in".Final Answer:
<transition mode="out-in"> -> Option AQuick Check:
out-in mode syntax = mode="out-in" [OK]
- Using mode="in-out" instead of out-in
- Using invalid mode values like "both" or "enter-leave"
- Omitting quotes around mode value
<transition mode="out-in"> <component :is="view" /> </transition>
If
view changes from "A" to "B", what happens first?Solution
Step 1: Identify transition mode in code
The mode is set to "out-in", meaning the old component leaves first.Step 2: Understand transition order
Whenviewchanges, component A leaves first, then component B enters.Final Answer:
Component A leaves, then component B enters. -> Option AQuick Check:
out-in mode = old leaves first [OK]
- Thinking new enters before old leaves in out-in mode
- Assuming simultaneous enter and leave
- Ignoring the mode attribute effect
<transition mode="in-out"> <div v-if="show">Hello</div> </transition>
But the old element leaves before the new one enters. What is the likely mistake?
Solution
Step 1: Analyze transition mode and behavior
The mode is set to "in-out", which means new enters first, old leaves after.Step 2: Compare expected and actual behavior
The old element leaves before new enters, which matches "out-in" mode, not "in-out".Final Answer:
You used mode="in-out" but expected out-in behavior. -> Option CQuick Check:
in-out mode means new enters first [OK]
- Confusing in-out and out-in modes
- Thinking v-if vs v-show affects mode order
- Assuming transition-group is needed for single elements
Solution
Step 1: Understand desired animation order
You want the old element to fade out completely before the new element fades in.Step 2: Choose correct transition mode
out-inmode makes the old element leave first, then the new enters, matching the requirement.Step 3: Confirm CSS setup
Use CSS classes for fade-out on leave and fade-in on enter to achieve smooth transitions.Final Answer:
Use mode="out-in" with CSS fade-out on leave and fade-in on enter. -> Option BQuick Check:
Old fades out first = out-in mode [OK]
- Using in-out mode which enters new before old leaves
- Assuming simultaneous fades without mode
- Using invalid mode values like "both"
Ready to prove it?
Pass the test and this pattern moves unknown
