Performance: Transition component for animations
This affects the rendering performance during element entry and exit animations, impacting visual stability and interaction responsiveness.
<template> <Transition name="fade"> <div v-if="show">Content</div> </Transition> </template> <script setup> import { ref } from 'vue' const show = ref(false) </script> <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" :style="{ transition: 'all 0.5s', opacity: show ? 1 : 0 }">Content</div> </template> <script setup> import { ref } from 'vue' const show = ref(false) </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct style toggle with v-if | Insert/remove DOM nodes abruptly | 2 per toggle | 2 paints per toggle | [X] Bad |
| Vue Transition component with CSS classes | Batched DOM updates with class toggling | 1 per animation | 1 paint per animation | [OK] Good |
<Transition> component in Vue?<Transition> component with a CSS class name 'fade'?name attribute sets the base CSS class for transition states.name="fade", which is correct. Others use invalid attributes like class, animation, or effect.show changes from false to true?
<Transition name="slide"> <div v-if="show">Hello</div> </Transition>
show becomes true, the div is inserted and Transition applies CSS classes based on the 'slide' name.slide-enter-from and slide-enter-active are added to animate the div's entrance.<Transition>:
<Transition name="fade"> <div v-show="visible">Content</div> </Transition>
v-if because it controls element insertion/removal, triggering enter/leave animations.v-show only toggles visibility via CSS, so Transition cannot detect element entering or leaving, preventing animations.<Transition> with these CSS classes:
.fade-enter-active { transition: opacity 0.5s; }
.fade-enter-from { opacity: 0; }
.fade-enter-to { opacity: 1; }
.fade-leave-active { transition: opacity 0.5s; }
.fade-leave-from { opacity: 1; }
.fade-leave-to { opacity: 0; }
Which Vue template correctly applies this animation to a paragraph that toggles with showText?name="fade".Hello World
usesname="fade" and v-if, which is correct.
Hello World
uses v-show, which won't animate properly.Hello World
uses an invalid attribute 'animation'.Hello World
shows the paragraph whenshowText is false, which is opposite of expected.Pass the test and this pattern moves unknown