vue 过渡 动画

动画
<style>
  @keyframes leftToRight {
    50% {
      transform: translate3d(200px,200px,0)
    }
  }
  .animation{
    animation: leftToRight 2s;
  }
</style>

过渡

<style>
 .transition{
    transition:background-color 3s ease
   }
   .blue{
     background-color: blue;
   }
   .green{
     background-color: green;
   }
 </style>
 
<div id="root">
     <div :class="animate">hello</div>
     <button @click="change">切换</button>
 </div>
  
 const app = new Vue({
   el: "#root",
   data() {
     return {
       animate:{
         transition: true,
         blue:true,
         green:false
       }
     }
   },
   methods: {
     change(){
       this.animate.blue = !this.animate.blue;
       this.animate.green = !this.animate.green;
     }
   }
 })

动画案例

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://unpkg.com/vue@next"></script>
  <style>
    @keyframes shakeEnter {
      0%{
        transform:translateX(-100px)
      }
    }
    @keyframes shakeLeave {
      100%{
        transform:translateX(100px)
      }
    }
    .v-enter-active{
      animation: shakeEnter 2s ease-out;
      transition: opacity 2s ease-out;
    }

    .v-leave-active{
      animation: shakeLeave 2s ease-out;
      transition: opacity 2s ease-out;
    }
    .v-enter-from,.v-leave-to{
      opacity: 0;
    }
    body {
      margin: 200px;
    }
  </style>
</head>

<body>
  <div id="root">
    <transition>
      <div v-show="show">hello</div>
    </transition>
    <button @click="change">切换</button>
  </div>
  <script>
    const app = Vue.createApp({
      data() {
        return {
          show: false
        }
      },
      methods: {
        change() {
          this.show = !this.show;
        }
      }
    })
    const vm = app.mount("#root");
  </script>
</body>

</html>

v-enter-from 和v-leave-from 都是触发过渡的一瞬间生效,然后下一帧就失效,过渡期间会给dom添加enter-active、enter-to或者leave-active、leave-to这个时候transition:all 1s ease-in; 就会起作用

不用css做动画,而用js
案例:

<body>
  <div id="root">
    <transition :css="false"
    @before-enter="handleBeforeEnter"
    @enter="handleEnter"
    @after-enter="handleEnterEnd"
    >
      <div v-show="show">hello</div>
    </transition>
    <button @click="change">切换</button>
  </div>
  <script>

    const app = Vue.createApp({
      data() {
        return {
          show: false
        }
      },
      methods: {
        change() {
          this.show = !this.show;
        },
        handleBeforeEnter(el){
          el.style.color = 'red'
        },
        handleEnter(el,done){
          const timer = setInterval(()=>{
            const color = el.style.color;
            el.style.color = color === 'red' ? 'green' : 'red';
          },1000)

          setTimeout(()=>{
            clearInterval(timer);
            done();  //动画结束
          },3000)
        },
        handleEnterEnd(){
          alert("动画结束")
        }
      }
    })
    const vm = app.mount("#root");
  </script>
</body>
</html>

当有两个dom同时做动画,要求先出去再进来时,可以写mode
< transition mode=“out-in” appear> //appear是初次展示也要动画

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值