一、动画的原理

transition.png

  1. <transtion name="fade">
  2. <div v-show="show">hello world</div>
  3. </transtion>
  4. <button @click="toggle">切换</button>

当一个标签被transition包裹的时候,vue会自动给我们构建一个动画流程。

Enter的动画表示显示的动画 Tip:重点关注fade-enter的值 Leave的动画表示消失的动画 Tip:重点关注fade-leave-to的值

fade-enter-active,fade-leave-active监听动画的过程

  1. export default {
  2. name: 'home',
  3. data(){
  4. return {
  5. show:true
  6. }
  7. },
  8. methods:{
  9. toggle(){
  10. this.show = !this.show;
  11. }
  12. }
  13. }
  1. <style scoped>
  2. .fade-enter,.fade-leave-to{
  3. opacity: 0;
  4. }
  5. .fade-enter-active,.fade-leave-active{
  6. transition:opacity 2s;
  7. }
  8. </style>

二、动画详解

三、vue-cli和animate.css

3-1 安装animate.css

https://animate.style/

  1. yarn add animate.css@3.7.2

3-2 配置

  1. import animate from 'animate.css'
  2. Vue.use(animate)

3-3 使用

  1. <template>
  2. <div class="home">
  3. 首页
  4. <transition name="fade"
  5. enter-active-class="animated bounce"
  6. leave-active-class="animated shake">
  7. <div v-show="show">hello world</div>
  8. </transition>
  9. <button @click="toggle">切换</button>
  10. </div>
  11. </template>
  12. <script>
  13. export default {
  14. name: 'home',
  15. data(){
  16. return {
  17. show:true
  18. }
  19. },
  20. methods:{
  21. toggle(){
  22. this.show = !this.show;
  23. }
  24. }
  25. }
  26. </script>