Performance: Transition component basics
This affects the smoothness and speed of visual changes on the page, impacting user interaction responsiveness and visual stability.
<template> <Transition name="fade"> <div v-if="show">Content</div> </Transition> </template> <style> .fade-enter-active, .fade-leave-active { transition: opacity 0.5s ease; } .fade-enter-from, .fade-leave-to { opacity: 0; } .fade-enter-to, .fade-leave-from { opacity: 1; } </style>
<template> <div v-if="show" class="fade">Content</div> </template> <style> .fade { transition: opacity 0.5s ease; opacity: 1; } .fade-leave-active { opacity: 0; } </style>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct class toggle without Transition | Multiple add/remove nodes | Multiple reflows per toggle | High paint cost due to layout thrashing | [X] Bad |
| Vue Transition component with opacity animation | Minimal DOM changes | Single reflow per animation | Low paint cost with GPU compositing | [OK] Good |
<Transition> component in Vue?<Transition> component with a CSS animation named "fade"?name attribute to set the CSS class prefix for animations.<Transition name="fade"><div v-if="show">Hello</div></Transition> uses name="fade", which is correct. Others use invalid attributes like animation, class, or effect.show changes from true to false?
<template>
<Transition name="slide">
<div v-if="show">Slide Content</div>
</Transition>
</template>
<script setup>
import { ref } from 'vue'
const show = ref(true)
</script>v-if controls the presence of the div. When show becomes false, the div is removed.<Transition name="fade"> <div v-show="visible">Content</div> </Transition>What is the problem?
v-if because it controls element presence, triggering enter/leave animations.v-show inside Transition does not trigger the full animation lifecycle.<Transition>. Which approach correctly applies the Transition component to animate each item individually when the list changes?
A) Wrap the entire list with one <Transition> component.
B) Wrap each list item inside its own <Transition> component.
C) Use <TransitionGroup> instead of <Transition> for lists.
D) Use <Transition> only on the parent container without keys on items.<TransitionGroup> is designed to animate lists, handling keys and individual item transitions automatically.Pass the test and this pattern moves unknown