Performance: Why transitions enhance UX
Transitions affect the smoothness of visual changes and user interaction responsiveness on a page.
<template><button @click="show = !show">Toggle</button><transition name="fade"><div v-if="show" class="fade">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.3s ease; } .fade-enter-from, .fade-leave-to { opacity: 0; }</style>
<template><button @click="show = !show">Toggle</button><div v-if="show" style="transition: all 1s;">Content</div></template><script setup>import { ref } from 'vue'; const show = ref(false);</script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Transition on 'all' properties | Normal DOM ops | Multiple reflows triggered | High paint cost | [X] Bad |
| Transition on 'opacity' only | Normal DOM ops | No reflows | Low paint cost (composite only) | [OK] Good |
<transition> component wraps a single element to animate it.<div> is a valid block element and commonly used inside <transition>.show changes from false to true?
<template>
<transition name="fade">
<div v-if="show">Hello</div>
</transition>
</template>v-if with <transition><transition> component animates elements entering or leaving the DOM controlled by v-if.div fades in smoothly using CSS classes. -> Option C<template>
<transition>
<div v-if="show">Content</div>
<span>Extra</span>
</transition>
</template><transition> wrapping rules<transition> component must wrap exactly one element or component.<transition><div> and a <span>, which is invalid.<transition> component can only wrap one element, but here it wraps two. -> Option A<transition> component.<transition> allows smooth, controlled animations improving user experience.Pass the test and this pattern moves unknown