The Method of Adding Transition Effect in Vue

  • 2021-08-05 08:10:22
  • OfStack

As for the transition effect of vue, the official overview given by vue is as follows.

Vue provides many different application transition effects when inserting, updating or removing DOM.

Includes the following tools:

1. Automatically apply class in CSS transition and animation

2. You can use the third-party CSS animation library together, such as Animate. css

3. Use JavaScript to directly manipulate DOM in the transition hook function

4. You can use the third-party JavaScript animation library together, such as Velocity. js

There are many examples given in it, but when the examples are given to "multiple element transition", there is no case code. Here, this part of the case in vue official document is supplemented by one part.

Here's my code


<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <script src="../js/vue.js" charset="utf-8"></script>
 <title> Transition effect - Transition of multiple elements </title>
 <style>
  /* Fade in and fade out button , This makes the button work poorly */
  /* In   " on "   Buttons and   " off "   In the transition of buttons, both buttons are redrawn. 1 When leaving the transition, another 1 Began to enter the transition. This is  <transition>  Default behavior of  -  Entry and exit occur at the same time. */
  .fade-enter-active,.fade-leave-active{
   transition: all .5s
  }
  .fade-enter,.fade-leave-active{
   opacity: 0
  }
  /* Fade in and fade out + Absolute positioning */
  #app2{
   position: relative;
   height: 100px;
   width: 100px;
   background:green;
  }
  #app2 button{
   position: absolute;
   left:50%;
   top: 50%;
  }
  .fade-abosolute-enter,.fade-abosolute-leave-active{
   opacity: 0
  }
  .fade-abosolute-enter-active,.fade-abosolute-leave-active{
   transition: all 1s
  }
  /* Fade in and fade out + Absolute positioning + Sliding effect */
  /* This 3 Species effect is combined in 1 From now on, the effect is relatively good */
  #app3{
   width: 200px;
   height: 100px;
   background: rgb(26, 46, 224);
   position: relative;
  }
  #app3 button{
   position: absolute;
   left: 50%;
   top: 50%;
  }
  .fade-slide-enter-active,.fade-slide-leave-active{
   transition: all 1s
  }
  .fade-slide-enter{
   opacity: 0;
   transform: translateX(30px);
  }
  .fade-slide-leave-active{
   opacity: 0;
   transform: translateX(-30px);
  }
  #app4{
   position: relative;
   width: 200px;
   height: 100px;
   background: rgb(8, 201, 80);
  }
  #app4 button{
   position: absolute;
   left: 50%;
   top: 50%;
  }
  .fade-slide-mode-enter-active,.fade-slide-mode-leave-active{
   transition: all .5s
  }
  .fade-slide-mode-enter{
   opacity: 0;
   transform: translateX(30px);
  }
  .fade-slide-mode-leave-active{
   opacity: 0;
   transform: translateX(-30px);
  }
 </style>
</head>
<body>
  <!--   Fade in and fade out button , This makes the button work poorly  -->
 <div id="app1">
  <!--  This fade-out effect, which is very bad when the transition mode is not added, is added mode="out-in" After that, the effect is better. out-in It refers to transitioning existing elements first and transitioning new elements later  -->
  <transition name="fade" mode="out-in">
    <button v-if="isEditing" key="save" @click="isEditing = !isEditing">
     save
    </button>
    <button v-else key="edit" @click="isEditing = !isEditing">
     Edit
    </button>
  </transition>
 </div>
 <!--  Fade in and fade out + Absolute positioning  -->
 <div id="app2">
  <transition name="fade-abosolute">
   <button v-if="on" key="on" @click="on = !on">
    on
   </button>
   <button v-else key="off" @click="on = !on">
    off
   </button>
  </transition>
 </div>
 <!--  Fade in and fade out + Absolute positioning + Slip  -->
 <div id="app3">
  <transition name="fade-slide">
   <button v-if="on" key="on" @click="on = !on">
    on
   </button>
   <button v-else key="off" @click="on = !on">
    off
   </button>
  </transition>
 </div>
 <!-- in-out,out-in The transition mode, the entry and exit transition that takes effect at the same time, cannot meet all the requirements, so vue Provides a transition mode  -->
 <div id="app4">
  <transition name="fade-slide-mode" mode="in-out">
   <button v-if="on" key="on" @click="on = !on">
    on
   </button>
   <button v-else key="off" @click="on = !on">
    off
   </button>
  </transition>
 </div>
</body>
<script type="text/javascript">
 var vm1 = new Vue({
  el:'#app1',
  data () {
   return{
    isEditing:true,
   }
  }
 })
 var vm2 = new Vue({
  el:'#app2',
  data () {
   return {
    on:true
   }
  }
 })
 var vm3 = new Vue({
  el:'#app3',
  data () {
   return {
    on:true
   }
  }
 })
 var vm4 = new Vue({
  el:'#app4',
  data () {
   return {
    on:true
   }
  }
 })
</script>
</html>

Related articles: