Vue3 is the latest version of Vue, and it has also been greatly improved and updated in terms of animation. The animation function in Vue3 is implemented by using the Composition API and Transition components, allowing developers to more easily achieve various cool animation effects.
1. Composition API
Composition API is a new feature introduced in Vue3, which can optimize the readability, maintainability and reusability of components. In the Composition API, we can use ref and reactive to create responsive data, use watchEffect and computed to monitor changes in variables and calculated properties, and use onMounted and onUnmounted to execute life cycle hook functions, etc.
In terms of animation, the Composition API provides two functions: useAnimation and useTransition.
The useAnimation function allows us to create an animation function inside the component to handle some complex animation logic. Its basic syntax is as follows:
import { useAnimation } from 'vue'; export default { setup() { const {animate, stop} = useAnimation(); return { animate, stop } } }
Using the function returned by useAnimation, we can create a series of animation keyframes and then execute these animations through the animate function. For example, we can create a scaling animation like this:
const { animate, stop } = useAnimation(); const scale = animate({ 0: { transform: 'scale(1)', }, 0.5: { transform: 'scale(1.2)', }, 1: { transform: 'scale(1)', }, }, { duration: 1000, easing: 'ease-in-out', iterations: Infinity, direction: 'alternate', });
In this example, we use the animate function to create a scaling animation. By specifying the transform attribute of the keyframe, the element can be enlarged and reduced. In the second parameter of the animate function, we can set the duration, easing function, number of iterations, and playback direction of the animation. In this way, we can implement the zoom animation effect in our Vue component.
The useTransition function allows us to perform some transition animation operations when an element enters or leaves. Its basic syntax is as follows:
import { useTransition } from 'vue'; export default { setup() { const { enter, leave } = useTransition(); return { enter, leave } } }
Using the enter and leave functions, we can set different animation transition effects for the entry and exit behaviors of elements. For example, in the following example, we set a fade in and fade out for an element. Effect:
<template> <div :class="{'opacity-0': !show}" v-enter="fadeIn" v-leave="fadeOut"></div> </template> <script> import { useTransition } from 'vue'; export default { setup() { const { enter, leave } = useTransition(); const fadeIn = enter((el, done) => { el.style.opacity = '0'; setTimeout(() => { el.style.opacity = '1'; done(); }, 1000) }); const fadeOut = leave((el, done) => { el.style.opacity = '1'; setTimeout(() => { el.style.opacity = '0'; done(); }, 1000) }); return { fadeIn, fadeOut, show: true } } } </script>
In this example, we execute the fadeIn and fadeOut functions respectively when the element enters and leaves, and executes the fade-in and fade-out transition effects by passing different callback functions. In the callback function, we can use the setTimeout function to delay the execution of the animation, and use the done parameter to identify whether the animation is completed, thereby achieving the transition effect of the animation.
2. Transition component
In addition to the animation functions provided by the Composition API, Vue3 also provides a Transition component to help us achieve some basic transition animation effects, such as fade in and fade out, left Slide, slide up, rotate, etc. Its basic syntax is as follows:
<transition name="fade"> <div v-if="show">HELLO VUE3</div> </transition>
In the Transition component, we can specify different name values to use different transition animation effects, such as fade, slide-left, slide-up, rotate, etc. In Vue3, many transition effects are provided by default, and they can be achieved by setting the name value. For example, we can implement a simple fade-in and fade-out animation effect through the following example:
<template> <div> <button @click="show = !show">Show/Hide</button> <hr> <transition name="fade"> <p v-if="show">Hello Vue3</p> </transition> </div> </template> <script> export default { data() { return { show: true } } } </script> <style> .fade-enter-active, .fade-leave-active { transition: opacity 0.5s ease-in-out; } .fade-enter, .fade-leave-to { opacity: 0; } </style>
In this example, we achieve a fade-in and fade-out effect by setting name to fade, and dynamically change the show The value to control the display and hiding of elements. In the animation effect, we can use the transition attribute of CSS3 to set the animation effect of the transition, set the animation duration and easing function through .fade-enter-active and .fade-leave-active, and set the animation duration and easing function through .fade-enter and .fade -leave-to sets the state when the animation starts and ends.
Summary:
In Vue3, animation functions and Transition components are two common ways to achieve animation effects. The former is more flexible and free and can handle more complex animation logic. The latter is simpler and easier to use, suitable for some basic transition animation effects. No matter which method is used, you can use CSS3 properties and functions to achieve various cool animation effects and add lively colors to our applications.
The above is the detailed content of Animation function in Vue3: achieve cool animation effects. For more information, please follow other related articles on the PHP Chinese website!