Vue is a modern JavaScript framework that provides an easy way to build interactive interfaces and single-page applications. In Vue, we can easily use CSS animations to achieve smooth transition effects.
In Vue, we use the
<template> <div> <button @click="show = !show">Toggle</button> <transition name="fade"> <div v-if="show">Hello World</div> </transition> </div> </template> <style> .fade-enter-active, .fade-leave-active { transition: opacity 0.5s; } .fade-enter, .fade-leave-to { opacity: 0; } </style>
In this example, the
The CSS animation style is defined in the class name specified by the name attribute of the
Vue will automatically add and remove these class names to trigger CSS transition animations when elements enter and leave.
In addition to the fade in and fade out effect, we can also use CSS transition animation to achieve a variety of complex transition effects, such as movement, rotation, scaling, etc. The following is an example of implementing an element rotation effect:
<template> <div> <button @click="show = !show">Toggle</button> <transition name="rotate"> <div v-if="show" class="box"></div> </transition> </div> </template> <style> .rotate-enter-active, .rotate-leave-active { transition: transform 1s; } .rotate-enter, .rotate-leave-to { transform: rotate(0deg); } .rotate-leave, .rotate-enter-to { transform: rotate(180deg); } .box { width: 100px; height: 100px; background-color: red; } </style>
In this example, we define a transition component named rotate and define CSS transition animations for its enter, leave, and transition states. When an element enters, it rotates from 0 degrees to 180 degrees, and when an element leaves, it rotates from 180 degrees to 0 degrees.
To summarize, CSS transition animation can be easily used in Vue to achieve various transition effects. Through the
The above is the detailed content of How to use CSS transitions to achieve animated transition effects in Vue. For more information, please follow other related articles on the PHP Chinese website!