Home >Web Front-end >Vue.js >Detailed explanation of animation functions in Vue3: application to achieve cool animation effects
With the continuous development of Internet technology, more and more websites and applications need to present cool animation effects to improve user experience. As a modern JavaScript framework, Vue3 provides developers with many excellent toolkits, including animation functions. This article will introduce in detail the application and implementation methods of animation functions in Vue3, as well as how to achieve cool animation effects.
Vue3 provides a powerful animation function library through the Composition API, including:
useTransition
: Transition function useAnimation
: Animation function useTween
: Easing function useSpring
: Spring functionsThese functions allow us to easily implement various complex animation effects in web pages, such as gradients, sliding, rotation and other effects when the state changes.
useTransition
Transition function useTransition
is a transition function in Vue3, used between two states Make transitions, such as from showing to hiding, sliding in from top to bottom out, etc. Its basic usage is as follows:
import { useTransition } from 'vue' const transitions = useTransition(show, { // 定义三个阶段的动画 enter: '', leave: '', appear: '' })
where show
is a Boolean value indicating whether the current state should be presented. The three parameters enter
, leave
and appear
are strings that define the transition animation to be executed in three stages.
Simple example:
<template> <div class="container"> <button @click="toggle">Toggle</button> <transition appear v-for="msg in msgs" :key="msg.id" :css="false" :enter-class="'animate__animated animate__fadeInDown'" :leave-class="'animate__animated animate__fadeOutUp'" > <div class="alert" :class="'alert-' + msg.type"> {{ msg.message }} </div> </transition> </div> </template> <script> import { reactive, toRefs, ref, useTransition } from 'vue'; export default { setup() { const data = reactive({ msgs: [] }) const toggle = () => { data.msgs.unshift({ id: Math.random(), type: 'success', message: '这是一条消息' }) } const transitions = useTransition(data.msgs, { enterActiveClass: 'animate__animated animate__fadeInDown', leaveActiveClass: 'animate__animated animate__fadeOutUp' }) return { ...toRefs(data), transitions, toggle } } } </script>
When we click the "Toggle" button to control the change of the show
value, the prompt box area will be displayed or hidden through the transition function. In this example, we use the animate.css library to achieve animation effects.
useAnimation
Animation functionUnlike the transition function, the animation function can customize various radii, such as rotation, scaling, etc. Various animation effects can be defined using useAnimation
, which accepts a function as a parameter, which contains the following parameters:
initial
: when the animation starts The initial state of from
to
: animation duration
: animation delay time
: easing function
import { useAnimation } from 'vue' const animations = useAnimation(() => ({ top: 0, left: 0, backgroundColor: 'red', width: '100px', height: '100px', translateY: 0, rotate: '0deg' }), { from: { top: '100px', left: '100px', backgroundColor: 'blue', width: '50px', height: '50px', translateY: '200px', rotate: '-90deg' }, to: { top: '200px', left: '200px', backgroundColor: 'black', width: '200px', height: '200px', translateY: '0px', rotate: '360deg' }, duration: 3000, delay: 1000, ease: 'ease' })This example defines an animation function that transitions the
initial state from a small blue square to a large black square, while also animating changing their properties.
setup, we cannot directly obtain its value through the template. We need to manually introduce the specific value to be set in the template. Animations should be used like this:
<template> <div :style="animations"></div> </template> <script> import { useAnimation } from 'vue'; export default { setup() { const animations = useAnimation(() => ({ top: 0, left: 0, backgroundColor: 'red', width: '100px', height: '100px', translateY: 0, rotate: '0deg' }), { from: { top: '100px', left: '100px', backgroundColor: 'blue', width: '50px', height: '50px', translateY: '200px', rotate: '-90deg' }, to: { top: '200px', left: '200px', backgroundColor: 'black', width: '200px', height: '200px', translateY: '0px', rotate: '360deg' }, duration: 3000, delay: 1000, ease: 'ease' }) return { animations } } } </script>Property values that require animation in the template can be passed to
:style to set the final target.
Easing function
useTween function for creating elastic, damping, spring and other easing effects. Basic usage is as follows:
import { useTween } from 'vue' const tween = useTween(0, 100, { duration: 1000, delay: 0, ease: 'easeInQuad', onComplete: () => { console.log('Completed') } })This example will convert the value from 0 to 100 within the specified time, using the
easeInQuad easing function.
useTween:
<template> <div> <div :style="{ transform: 'translateX(' + xValue + 'px)' }">{{ xValue }}</div> <button @click="move">Move</button> </div> </template> <script> import { ref, useTween } from 'vue'; export default { setup() { const xValue = ref(0) const move = () => { useTween(0, 300, { duration: 1000, ease: 'easeOutElastic', onUpdate: (value) => { xValue.value = value } }) } return { xValue, move } } } </script>In this example, we use
useTween to
xValue Move from 0 to 300, using the
easeOutElastic easing function to create a spring effect. The
onUpdate callback function assigns
value (the final value of the spring animation) to
xValue and binds it to a div in the template.
Spring function
useSpring is a function in Vue3 used to implement spring animation. It can be based on Creates an animation given an initial and target state, and applies a spring effect.
import { useSpring } from 'vue' const spring = useSpring({ from: { opacity: 0, transform: 'translateX(-100px)' }, to: { opacity: 1, transform: 'translateX(0px)' }, config: { tension: 120, friction: 14, mass: 5 } })This example will pan the element from the left and translucent to opaque. As with other animation functions, there are many other customization options we can use to control animation effects.
<template> <div :style="spring"> <h1>这是一个标题</h1> <p>这是一段内容</p> </div> </template> <script> import { useSpring } from 'vue'; export default { setup() { const spring = useSpring({ from: { opacity: 0, transform: 'translateX(-100px)' }, to: { opacity: 1, transform: 'translateX(0px)' }, config: { tension: 120, friction: 14, mass: 5 } }) return { spring } } } </script>In the template, we use the
:style attribute to represent the style bound to the animated element. In this example, we apply the spring animation's state to the parent
div to demonstrate how to animate the spring across the entire page.
setup function and then bind their state values to components and templates. Additionally, the configuration options for these functions can be extended as needed to implement a variety of different types of animation effects.
The above is the detailed content of Detailed explanation of animation functions in Vue3: application to achieve cool animation effects. For more information, please follow other related articles on the PHP Chinese website!