Performance: CSS transition classes
CSS transition classes affect the smoothness and speed of visual changes, 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, .fade-leave-to { opacity: 0; } </style>
<template> <div :class="{ 'fade-enter-active': show, 'fade-leave-active': !show }">Content</div> </template> <style> .fade-enter-active, .fade-leave-active { transition: all 0.5s ease; } .fade-enter, .fade-leave-to { opacity: 0; height: 0; } </style>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Transition all properties including height | No extra DOM nodes | Multiple reflows per frame | High paint cost due to layout changes | [X] Bad |
| Transition opacity only with Vue <transition> | No extra DOM nodes | No reflows | Low paint cost, uses compositor | [OK] Good |
<transition> component?<transition> in Vue<transition> component wraps elements to apply CSS classes during their entering and leaving phases.<transition>?v-enter class first.v-enter-to is applied after v-enter, v-leave-active and v-leave-to relate to leaving transitions.<transition name="fade"> <div v-if="show">Hello</div> </transition>And these CSS classes:
.fade-enter-active { transition: opacity 0.5s; }
.fade-enter { opacity: 0; }
.fade-enter-to { opacity: 1; }
What will happen when show changes from false to true?show becomes true, Vue applies fade-enter, fade-enter-active, and fade-enter-to classes in sequence.fade-enter sets opacity 0, fade-enter-to sets opacity 1, and fade-enter-active animates opacity over 0.5 seconds, causing a fade-in effect.<transition name="slide"> <div v-if="visible">Content</div> </transition>With CSS:
.slide-enter-active { transition: transform 0.3s ease; }
.slide-enter { transform: translateX(-100%); }
.slide-enter-to { transform: translateX(0); }
.slide-leave { transform: translateX(0); }
.slide-leave-to { transform: translateX(100%); }
If the element does not slide out when visible changes from true to false, what is the likely cause?slide-leave-active to define the transition property for smooth animation.slide-leave-active is missing, the browser does not animate the transform property on leave, causing no slide-out effect..slide-leave-active class with transition property -> Option Cv-leave-active, v-leave, v-leave-to) with opacity changes, the fade-out happens on removal. Omitting enter classes means no fade-in on addition.v-leave-active, v-leave, and v-leave-to classes with opacity changes; omit enter classes -> Option BPass the test and this pattern moves unknown