Detailed explanation of Vue transition effects and animation transition usage examples

Detailed explanation of Vue transition effects and animation transition usage examples

Let’s look at an example first

insert image description here

The code is as follows

<template>
  <div align="center" style="margin-top: 100px;">
    <button @click="show= !show" >Test</button>
    <transition>
      <div v-if="show">
        <p>This is a piece of text</p>
      </div>
    </transition>
  </div>
</template>
<script>
export default {
  name: 'transitionTest',
  data () {
    return {
      show: true
    }
  }
}
</script>
<style scoped>
    .v-enter-active, .v-leave-active {
    transition: all .5s ;
    }
    .v-enter {
    transform: translateY(50px);
    opacity: 0;
    }
    .v-leave-active {
    transform: translateY(50px);
    opacity: .5;
    }
</style>

Wrap a div component and click the button to achieve the animation effect. It is usually used with v-if, v-show, dynamic components, and component root nodes.

transition hook function

Transition provides six hook functions, allowing us to write corresponding animation effects at different times. The following are the execution times of these six hook functions

1.v-enter: Enter the transition start state. Takes effect before the element is inserted and is removed in the next frame after the element is inserted.

2.v-enter-active: The state when the transition takes effect. This class can be used to define entry transition timing, delays and curve functions.

3.v-enter-to: Enter the end state of the transition. Takes effect one frame after the element is inserted (at the same time v-enter is removed), and is removed after the transition/animation is complete.

4.v-leave: Leave the starting state of the transition. Takes effect immediately when a leaving transition is triggered, and is removed on the next frame.

5.v-leave-active: The state when the leaving transition takes effect. This class can be used to define the duration, delay and curve function of the leaving transition.

6.v-leave-to: Leave the end state of the transition. Takes effect the next frame after the leaving transition is triggered (at the same time v-leave is removed), and is removed after the transition/animation is complete.

Custom transition class name

There are multiple places on the page that need transition effects. If you use the default 6 hook functions provided by Vue, the transition effects of all places to be transitioned will be the same. If we need to define different animation effects in different situations, we need to define the class name specific to each transition effect. The solution is to add a name attribute to the transition tag and write your own class name prefix in the name attribute. For example, when using class names, it would be like this: .my-trans-leave, .my-trans-enter-to. like:

    <transition name="my-trans" mode="out-in">
       <router-view v-if="!$route.meta.keepAlive"></router-view>
    </transition>

style, my-trans is the prefix of ".my-trans-enter-active"

<style>
  .my-trans-enter-active,
  .my-trans-leave-active {
    transition: all .2s;
    opacity: 1;
  }
  .my-trans-enter {
    transition: all .2s;
    opacity: 0;
  }
  .my-trans-leave-to {
    transition: all .2s;
    opacity: 0;
  }  
</style>

Use of transition-group

When performing transition rendering on a list, you must wrap it with the transition-group element. Click on the content in the list to remove it according to the following animation. The example is as follows

insert image description here

<template>
  <div class="main_css">
    <transition-group name="slide">
      <li v-for="(item,i) in list" :key="item.id" @click="del(i)">
        {{ item.id }} --- {{ item.name }}
      </li>
    </transition-group>
  </div>
</template>
<script>
export default {
  name: 'transitionGroupTest',
  data () {
    return {
      list: [{
        id: 1,
        name: 'Braised Fish'
      },
      {
        id: 2,
        name: 'Fried potatoes'
      },
      {
        id: 3,
        name: 'Roasted Eggplant'
      }
      ]
    }
  },
  methods: {
    del (i) {
      this.list.splice(i, 1)
    }
  }
}
</script>
<style scoped>
  .main_css {
    margin-left: 50px;
    margin-top: 50px;
  }
  .slide-enter-active {
    transition: all .5s linear;
  }
  .slide-leave-active {
    transition: all .1s linear;
  }
  .slide-enter {
    transform: translateX(-100%);
    opacity: 0;
  }
  .slide-leave-to {
    transform: translateX(110%);
    opacity: 0;
  }
</style>

summary

Transition and animation are completed using the transition tag, and 6 hook functions are provided. The global animation is in app.vue, and the transition is wrapped in the router-view component, such as: ; Add the name attribute to the transition tag to define the transition class name to achieve local changes.

The above is a detailed explanation of the usage examples of Vue transition and animation transition. For more information about Vue transition and animation transition, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Use transition and transition-group in vue components to implement transition animation
  • Examples of transition methods for multiple elements, components, and lists in Vue
  • The whole process of transition single node transition and transition-group list transition in Vue

<<:  Tutorial on installing Elasticsearch 7.6.2 in Docker

>>:  Solve the scroll-view line break problem of WeChat applet

Recommend

Docker Basic Tutorial: Detailed Explanation of Dockerfile Syntax

Preface Dockerfile is a script interpreted by the...

A brief introduction to mysql mycat middleware

1. What is mycat A completely open source large d...

Implementation of socket options in Linux network programming

Socket option function Function: Methods used to ...

React nested component construction order

Table of contents In the React official website, ...

W3C Tutorial (6): W3C CSS Activities

A style sheet describes how a document should be ...

Detailed explanation of Vue's simple store

The simplest application of store in Vue is globa...

Three.js sample code for implementing dewdrop animation effect

Preface Hello everyone, this is the CSS wizard - ...

How to use Linux commands in IDEA

Compared with Windows system, Linux system provid...

CSS to achieve horizontal lines on both sides of the middle text

1. The vertical-align property achieves the follo...

Sample code for displaying a scroll bar after the HTML page is zoomed out

Here is a record of how to make a scroll bar appe...

Design Tips: We think you will like it

<br />Looking at this title, you may find it...

Button is stretched on both sides in IE

When you write buttons (input, button), you will f...

Methods and steps for deploying go projects based on Docker images

Dependence on knowledge Go cross-compilation basi...