CSS Transitions
This recipe shows how to use pocket-vue to apply CSS transitions to elements when they are shown or hidden.
Example Code
html
<style>
.fade-enter-active, .fade-leave-active {
transition: opacity 0.5s ease;
}
.fade-enter-from, .fade-leave-to {
opacity: 0;
}
</style>
<div v-scope="{
show: true,
toggle() {
this.show = !this.show;
}
}">
<div class="animation-container">
<button @click="toggle">Toggle Fade</button>
<div v-if="show" class="fade">
<p>This is a fading element!</p>
</div>
</div>
</div>How it works
- CSS Classes: We define CSS classes for the enter and leave transitions using standard naming conventions (e.g.,
.fade-enter-active,.fade-leave-active). - Reactive State: We use a
showboolean to control the visibility of the element. - Conditional Rendering: We use
v-if="show"to conditionally render the element. - Transition Timing: The transition duration and easing are controlled by CSS properties.
- Simplified UI: This approach is efficient and easy to maintain, providing smooth transitions for common UI patterns.
- Browser Compatibility: CSS transitions are widely supported in modern browsers, providing a consistent experience across different platforms.
