Performance: Dynamic transitions
Dynamic transitions affect the smoothness and speed of visual changes on the page, impacting user interaction responsiveness and visual stability.
<template> <transition :name="transitionName"> <div v-if="show" class="box">Content</div> </transition> </template> <script setup> import { ref } from 'vue' const show = ref(true) const transitionName = ref('fade') </script> <style> .fade-enter-active, .fade-leave-active { transition: opacity 0.5s ease, transform 0.5s ease; will-change: opacity, transform; } .fade-enter-from, .fade-leave-to { opacity: 0; transform: scale(0.8) translateZ(0); } </style>
<template> <transition :name="transitionName"> <div v-if="show" class="box">Content</div> </transition> </template> <script setup> import { ref } from 'vue' const show = ref(true) const transitionName = ref('fade') </script> <style> .fade-enter-active, .fade-leave-active { transition: all 0.5s ease; } .fade-enter-from, .fade-leave-to { opacity: 0; transform: scale(0.8); } </style>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Transition on 'all' properties | No extra DOM nodes | Multiple reflows per frame | High paint cost due to layout changes | [X] Bad |
| Transition on 'opacity' and 'transform' only | No extra DOM nodes | No reflows | Low paint cost, GPU accelerated | [OK] Good |
:name attribute in Vue's <transition> component control?:name in <transition>:name attribute sets the base CSS class name for the transition states like enter and leave.:name to CSS animation classes{name}-enter-active and {name}-leave-active to trigger CSS animations.:name = CSS class base name [OK]: or v-bind: with a JavaScript expression inside quotes.<transition :name="transitionName"> uses :name="transitionName", correctly binding a variable named transitionName.<template>
<transition :name="animation">
<div v-if="show">Hello</div>
</transition>
</template>
<script setup>
import { ref } from 'vue'
const show = ref(true)
const animation = ref('fade')
</script>animation.value from 'fade' to 'slide' while show is true?v-if changes.animation.value alone does not trigger enter/leave, so no animation runs immediately.show changes -> Option C<transition :name="currentTransition">
<div v-if="visible">Content</div>
</transition>
<script setup>
import { ref } from 'vue'
const visible = ref(true)
const currentTransition = ref('fade')
// Later in code
currentTransition = 'slide'
</script>.value, not direct assignment.currentTransition = 'slide' which replaces the ref instead of updating its value..value, not direct assignment [OK]v-if or similar directives.show triggers the animation with the current transition name.show ensures the element animates with the chosen transition each toggle.show to trigger enter/leave animations -> Option BPass the test and this pattern moves unknown