import { ref, onMounted } from 'vue'; export default { setup() { const isVisible = ref(false); onMounted(() => { isVisible.value = true; }); return { isVisible } } }
ref
Formula isVisible
variable and set it to true
in the component's onMounted
life cycle function. By modifying the value of isVisible
, we can dynamically control the display and hiding of elements. <transition>
component to wrap the elements that need to be animated and specify different stages by adding class names. animation effects. In Vue3, in addition to continuing to use the <transition>
component, the <transition-group>
and <teleport>
components are also introduced, so that The implementation of animation effects is more flexible and efficient. <transition>
component: <template> <transition name="fade"> <p v-if="isVisible">Hello, Vue3!</p> </transition> </template> <style> .fade-enter-active, .fade-leave-active { transition: opacity 0.5s; } .fade-enter, .fade-leave-to { opacity: 0; } </style>
<transition>
The component wraps a <p>
element and specifies the name of the animation effect as "fade". In the CSS style, we define the style of the entry and exit stages of the animation, and trigger the animation effect by adding the class name. <Transition>
component, we can easily integrate GSAP and use its animation effect function. <template> <transition name="rotate" enter-active-class="rotate-enter-active" enter-from-class="rotate-enter-from" > <div v-if="isVisible" class="box"></div> </transition> </template> <style> .box { width: 100px; height: 100px; background-color: red; } .rotate-enter-active { animation: rotateEnter 1s; } @keyframes rotateEnter { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } </style>
@keyframes
to achieve the rotation effect. Apply CSS animation to the animation effect by adding the enter-active-class
and enter-from-class
attributes to the <transition>
component.
<p>Summary: <transition-group> ;
and <teleport>
components expand the application scenarios of animation effects; built-in support for GSAP provides more powerful animation library integration.
<p>The above is an introduction and code example of Vue3’s more powerful animation effect support than Vue2. The new animation function makes us more convenient and flexible when building beautiful user interfaces. Together with other enhancements brought by Vue3, we can develop excellent Vue applications more efficiently. The above is the detailed content of The difference between Vue3 and Vue2: more powerful animation effect support. For more information, please follow other related articles on the PHP Chinese website!