前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >【Vue3 从入门到实战 进阶式掌握完整知识体系】020-Vue中的动画:使用 transition 标签实现单元素组件的过渡和动画效果

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

作者头像
訾博ZiBo
发布2025-01-06 14:14:39
发布2025-01-06 14:14:39
14500
代码可运行
举报
运行总次数:0
代码可运行

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

出入场动画
代码语言:javascript
代码运行次数:0
运行
复制
<!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
image-20210613214526364.png
出入场动画
代码语言:javascript
代码运行次数:0
运行
复制
<!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
代码语言:javascript
代码运行次数:0
运行
复制
<!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
image-20210613221046693.png
在transition标签具体指定出入场动画
代码语言:javascript
代码运行次数:0
运行
复制
<!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
image-20210613222357389.png
使用动画库Animate

官网:https://animate.style/ 中文网:http://www.animate.net.cn/

代码语言:javascript
代码运行次数:0
运行
复制
<!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
image-20210613223252594.png
动画 + 过渡
代码语言:javascript
代码运行次数:0
运行
复制
<!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
image-20210613225921555.png
一个先执行完就是结束另一个

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

代码语言:javascript
代码运行次数:0
运行
复制
<!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
image-20210613230514709.png
统一设置动画和过渡效果执行时长

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

代码语言:javascript
代码运行次数:0
运行
复制
<!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
image-20210613230923569.png
精准设置入场和出场动画时长
代码语言:javascript
代码运行次数:0
运行
复制
<!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
image-20210613231216779.png
使用js做动画

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

代码语言:javascript
代码运行次数:0
运行
复制
<!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
image-20210613232739213.png
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-01-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 2、使用 transition 标签实现单元素组件的过渡和动画效果
    • 出入场动画
    • 运行结果
    • 出入场动画
    • 运行结果
    • 给transition加个name
    • 运行结果
    • 在transition标签具体指定出入场动画
    • 运行结果
    • 使用动画库Animate
    • 运行结果
    • 动画 + 过渡
    • 运行结果
    • 一个先执行完就是结束另一个
    • 运行结果
    • 统一设置动画和过渡效果执行时长
    • 运行结果
    • 精准设置入场和出场动画时长
    • 运行结果
    • 使用js做动画
    • 运行结果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档