Home > Web Front-end > Vue.js > body text

Detailed explanation of animation functions in Vue3: application to achieve cool animation effects

WBOY
Release: 2023-06-18 23:34:13
Original
1915 people have browsed it

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.

  1. Introduction

Vue3 provides a powerful animation function library through the Composition API, including:

  • useTransition : Transition function
  • useAnimation : Animation function
  • useTween : Easing function
  • useSpring: Spring functions

These 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.

  1. 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: ''
})
Copy after login

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>
Copy after login

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.

  1. useAnimation Animation function

Unlike 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
  • ##duration: animation duration
  • delay: animation delay time
  • ease: easing function
A simple example:

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'
})
Copy after login

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.

It is worth noting that since the animation is set in

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>
Copy after login

Property values ​​that require animation in the template can be passed to

:style to set the final target.

  1. useTween Easing function
The easing function can not only have animation effects, but also make the animation more natural. Vue3 provides the

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')
  }
})
Copy after login

This example will convert the value from 0 to 100 within the specified time, using the

easeInQuad easing function.

Here is a simple example showing

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>
Copy after login

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.

  1. useSpring 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
  }
})
Copy after login

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>
Copy after login
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.

    Summary
Vue3 provides a set of excellent animation functions that can help developers quickly and easily understand complex animation effects. With these functions, we can implement various cool animation effects to further improve the user experience of web applications. To use these functions, we just need to call them in the

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!