【Vue3 从入门到实战 进阶式掌握完整知识体系】020-Vue中的动画:使用 transition 标签实现单元素组件的过渡和动画效果

2、使用 transition 标签实现单元素组件的过渡和动画效果

出入场动画

<!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>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="https://unpkg.com/vue@next"></script>
  <!-- 样式 -->
  <style>
    /* 入场动画(过渡) */
    .v-enter-from{
      opacity: 0;
    }
    .v-enter-active {
      transition: opacity 3s ease-out;
    }
    .v-enter-to{
      opacity: 1;
    }
    /* 出场动画(过渡) */
    .v-leave-from{
      opacity: 1;
    }
    .v-leave-active{
      transition: opacity 3s ease-out;
    }
    .v-leave-to{
      opacity: 0;
    }
  </style>
</head>

<body>
  <div id="root"></div>
</body>

<script>
  const app = Vue.createApp({
    data(){
      return{
        show: false
      }
    },
    methods:{
      shift(){
        this.show = !this.show;
      }
    },
    template: `
        <div>
          <transition>
            <div v-if="show">hello world!</div>
          </transition>
          <button @click="shift">切换</button>
        </div>
    `
  });

  const vm = app.mount('#root');
</script>

</html>

运行结果

image-20210613214526364.png

出入场动画

<!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>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="https://unpkg.com/vue@next"></script>
  <!-- 样式 -->
  <style>
    @keyframes shake {
      0%{
        transform: translateX(-100px);
      }
      50%{
        transform: translateX(-50px);
      }
      100%{
        transform: translateX(50px);
      }
    }
    /* 入场动画 */
    .v-enter-active {
      animation: shake 3s;
    }
    /* 出场动画 */
    .v-leave-active{
      animation: shake 3s;
    }
  </style>
</head>

<body>
  <div id="root"></div>
</body>

<script>
  const app = Vue.createApp({
    data(){
      return{
        show: false
      }
    },
    methods:{
      shift(){
        this.show = !this.show;
      }
    },
    template: `
        <div>
          <transition>
            <div v-if="show">hello world!</div>
          </transition>
          <button @click="shift">切换</button>
        </div>
    `
  });

  const vm = app.mount('#root');
</script>

</html>

运行结果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZYhtzHIW-1624976737549)(https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/be9f674f6e2d474dba33d92b24d52b23~tplv-k3u1fbpfcp-watermark.image)]

给transition加个name

<!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>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="https://unpkg.com/vue@next"></script>
  <!-- 样式 -->
  <style>
    @keyframes shake {
      0%{
        transform: translateX(-100px);
      }
      50%{
        transform: translateX(-50px);
      }
      100%{
        transform: translateX(50px);
      }
    }
    /* 这里要以 hello 开头 */
    /* 入场动画 */
    .hello-enter-active {
      animation: shake 3s;
    }
    /* 出场动画 */
    .hello-leave-active{
      animation: shake 3s;
    }
  </style>
</head>

<body>
  <div id="root"></div>
</body>

<script>
  const app = Vue.createApp({
    data(){
      return{
        show: false
      }
    },
    methods:{
      shift(){
        this.show = !this.show;
      }
    },
    // 给 transition 设置 name='hello'
    // 使用 v-show 同样能实现动画效果
    template: `
        <div>
          <transition name='hello'>
            <div v-if="show">hello world!</div>
          </transition>
          <button @click="shift">切换</button>
        </div>
    `
  });

  const vm = app.mount('#root');
</script>

</html>

运行结果

image-20210613221046693.png

在transition标签具体指定出入场动画

<!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>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="https://unpkg.com/vue@next"></script>
  <!-- 样式 -->
  <style>
    @keyframes shake {
      0%{
        transform: translateX(-100px);
      }
      50%{
        transform: translateX(-50px);
      }
      100%{
        transform: translateX(50px);
      }
    }
    /* 这里要以 hello 开头 */
    /* 入场动画 */
    .hello {
      animation: shake 3s;
    }
    /* 出场动画 */
    .bye {
      animation: shake 3s;
    }
  </style>
</head>

<body>
  <div id="root"></div>
</body>

<script>
  const app = Vue.createApp({
    data(){
      return{
        show: false
      }
    },
    methods:{
      shift(){
        this.show = !this.show;
      }
    },
    template: `
        <div>
          <transition enter-active-class='hello' leave-active-class='bye'>
            <div v-if="show">hello world!</div>
          </transition>
          <button @click="shift">切换</button>
        </div>
    `
  });

  const vm = app.mount('#root');
</script>

</html>

运行结果

image-20210613222357389.png

使用动画库Animate

官网:https://animate.style/

中文网:http://www.animate.net.cn/

<!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>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="https://unpkg.com/vue@next"></script>
  <!-- 引入Animate样式 cdn地址 -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css">
</head>

<body>
  <div id="root"></div>
</body>

<script>
  const app = Vue.createApp({
    data(){
      return{
        show: false
      }
    },
    methods:{
      shift(){
        this.show = !this.show;
      }
    },
    template: `
        <div>
          <transition 
          enter-active-class='animate__animated animate__bounce' 
          leave-active-class='animate__animated animate__bounce'>
            <div v-if="show">hello world!</div>
          </transition>
          <button @click="shift">切换</button>
        </div>
    `
  });

  const vm = app.mount('#root');
</script>

</html>

运行结果

image-20210613223252594.png

动画 + 过渡

<!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>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="https://unpkg.com/vue@next"></script>
  <!-- 样式 -->
  <style>
    @keyframes shake {
      0%{
        transform: translateX(-100px);
      }
      50%{
        transform: translateX(-50px);
      }
      100%{
        transform: translateX(50px);
      }
    }
    /* 入场动画 */
    .v-enter-active {
      animation: shake 3s;
      transition: color 3s ease-in;
    }
    .v-enter-from{
      color: red
    }
    /* 出场动画 */
    .v-leave-active{
      animation: shake 3s;
      transition: color 3s ease-in;
    }
    .v-leave-from{
      color: seagreen;
    }
  </style>
</head>

<body>
  <div id="root"></div>
</body>

<script>
  const app = Vue.createApp({
    data(){
      return{
        show: false
      }
    },
    methods:{
      shift(){
        this.show = !this.show;
      }
    },
    template: `
        <div>
          <transition>
            <div v-if="show">hello world!</div>
          </transition>
          <button @click="shift">切换</button>
        </div>
    `
  });

  const vm = app.mount('#root');
</script>

</html>

运行结果

image-20210613225921555.png

一个先执行完就是结束另一个

如果过渡执行完了就结束动画,反之亦然

<!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>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="https://unpkg.com/vue@next"></script>
  <!-- 样式 -->
  <style>
    @keyframes shake {
      0%{
        transform: translateX(-100px);
      }
      50%{
        transform: translateX(-50px);
      }
      100%{
        transform: translateX(50px);
      }
    }
    /* 入场动画 */
    .v-enter-active {
      animation: shake 10s;
      transition: color 3s ease-in;
    }
    .v-enter-from{
      color: red
    }
    /* 出场动画 */
    .v-leave-active{
      animation: shake 10s;
      transition: color 3s ease-in;
    }
    .v-leave-from{
      color: seagreen;
    }
  </style>
</head>

<body>
  <div id="root"></div>
</body>

<script>
  const app = Vue.createApp({
    data(){
      return{
        show: false
      }
    },
    methods:{
      shift(){
        this.show = !this.show;
      }
    },
    // type="transition" 表示以过渡效果为准
    // type="animation" 表示以动画效果为准
    template: `
        <div>
          <transition type="transition">
            <div v-if="show">hello world!</div>
          </transition>
          <button @click="shift">切换</button>
        </div>
    `
  });

  const vm = app.mount('#root');
</script>

</html>

运行结果

image-20210613230514709.png

统一设置动画和过渡效果执行时长

执行的节奏还是按钮原本设置的,这里只是设置多长事件结束效果

<!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>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="https://unpkg.com/vue@next"></script>
  <!-- 样式 -->
  <style>
    @keyframes shake {
      0%{
        transform: translateX(-100px);
      }
      50%{
        transform: translateX(-50px);
      }
      100%{
        transform: translateX(50px);
      }
    }
    /* 入场动画 */
    .v-enter-active {
      animation: shake 10s;
      transition: color 3s ease-in;
    }
    .v-enter-from{
      color: red
    }
    /* 出场动画 */
    .v-leave-active{
      animation: shake 10s;
      transition: color 3s ease-in;
    }
    .v-leave-from{
      color: seagreen;
    }
  </style>
</head>

<body>
  <div id="root"></div>
</body>

<script>
  const app = Vue.createApp({
    data(){
      return{
        show: false
      }
    },
    methods:{
      shift(){
        this.show = !this.show;
      }
    },
    // 1000毫秒后理解结束效果
    template: `
        <div>
          <transition :duration="1000">
            <div v-if="show">hello world!</div>
          </transition>
          <button @click="shift">切换</button>
        </div>
    `
  });

  const vm = app.mount('#root');
</script>

</html>

运行结果

image-20210613230923569.png

精准设置入场和出场动画时长

<!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>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="https://unpkg.com/vue@next"></script>
  <!-- 样式 -->
  <style>
    @keyframes shake {
      0%{
        transform: translateX(-100px);
      }
      50%{
        transform: translateX(-50px);
      }
      100%{
        transform: translateX(50px);
      }
    }
    /* 入场动画 */
    .v-enter-active {
      animation: shake 3s;
      transition: color 3s ease-in;
    }
    .v-enter-from{
      color: red
    }
    /* 出场动画 */
    .v-leave-active{
      animation: shake 3s;
      transition: color 3s ease-in;
    }
    .v-leave-from{
      color: seagreen;
    }
  </style>
</head>

<body>
  <div id="root"></div>
</body>

<script>
  const app = Vue.createApp({
    data(){
      return{
        show: false
      }
    },
    methods:{
      shift(){
        this.show = !this.show;
      }
    },
    // 1000毫秒后理解结束效果
    // 精准控制
    template: `
        <div>
          <transition :duration="{enter: 1000, leave: 3000}">
            <div v-if="show">hello world!</div>
          </transition>
          <button @click="shift">切换</button>
        </div>
    `
  });

  const vm = app.mount('#root');
</script>

</html>

运行结果

image-20210613231216779.png

使用js做动画

代码演示了入场动画,出场动画也类似

<!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>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="https://unpkg.com/vue@next"></script>
  <!-- 样式 -->
  <style>
    @keyframes shake {
      0%{
        transform: translateX(-100px);
      }
      50%{
        transform: translateX(-50px);
      }
      100%{
        transform: translateX(50px);
      }
    }
    /* 入场动画 */
    .v-enter-active {
      animation: shake 3s;
      transition: color 3s ease-in;
    }
    .v-enter-from{
      color: red
    }
    /* 出场动画 */
    .v-leave-active{
      animation: shake 3s;
      transition: color 3s ease-in;
    }
    .v-leave-from{
      color: seagreen;
    }
  </style>
</head>

<body>
  <div id="root"></div>
</body>

<script>
  const app = Vue.createApp({
    data(){
      return{
        show: false
      }
    },
    methods:{
      shift(){
        this.show = !this.show;
      },
      // 动画执行前
      handleBeforeEnter(el){
        el.style.color = 'red';
      },
      // 动画执行中
      handleEnterActive(el, done){
        // 每隔 1s 执行
        const animation = setInterval(() => {
          if(el.style.color === 'red'){
            el.style.color = 'blue'
          }else{
            el.style.color = 'red'
          }
        }, 1000);
        // 5s 后执行:清除定时器
        setTimeout(() => {
          clearInterval(animation);
          // 执行 done() 表示动画执行结束了
          done();
        }, 5000);
      },
      // 动画执行后
      handleAfterEnter(el){
        console.log("动画执行结束了!");
      }
    },
    template: `
        <div>
          <transition
          :css="false"
          @before-enter="handleBeforeEnter"
          @enter="handleEnterActive"
          @after-enter="handleAfterEnter"
          >
            <div v-if="show">hello world!</div>
          </transition>
          <button @click="shift">切换</button>
        </div>
    `
  });

  const vm = app.mount('#root');
</script>

</html>

运行结果

image-20210613232739213.png

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值