前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue2使用过渡标签transition使用动画

Vue2使用过渡标签transition使用动画

作者头像
打不着的大喇叭
发布2024-03-11 16:21:41
790
发布2024-03-11 16:21:41
举报
文章被收录于专栏:喇叭的学堂喇叭的学堂

注意:动画必须使用v-if || v-show配合

1、Vue2配Css3实现

  • 我们需要使用 过渡 标签 <transition> :
代码语言:javascript
复制
    <transition name="hello" appear>
      <h1 v-show="isShow">你好啊!</h1>
    </transition>

     :appear="true" [值需要的是布尔值,所以需要用v-bind绑定] 也可以直接写 【appear】
     appear 使用效果是:打开页面立马执行一次过来的动画
  • css3方案一:在样式style标签里面设置动画

【给来和走的样式的名字定义为 v-enter-active | v-leave-active,设置name的值,需要把v 改成它】

代码语言:javascript
复制
<style>
h1 {
  background-color: orange;
}
/* 如果过渡标签加了name属性,下面的v需要改为name的值 
.v-enter-active {
  animation: gbase 1s;
}
.v-leave-active {
  animation: gbase 1s reverse;
}
*/
.hello-enter-active {
  animation: gbase 1s;
}
.hello-leave-active {
  animation: gbase 1s reverse;
}
@keyframes gbase {
  from {
    transform: translateX(-100%);
  }
  to {
    transform: translateX(0px);
  }
}
</style>
  • 注意:vue解析的时候 <transition> 就没有了
  • 源码
代码语言:javascript
复制
<template>
  <div>
    <button @click="isShow = !isShow">显示/隐藏</button>
    <!--//todo 使用动画 [ 标签必须使用v-if||v-show才行 ]
      //todo 1、使用 过渡 标签 <transition> 【里面有一个属性name 标志它的名字】
      //todo 2、在样式style标签里面设置动画
      //todo 3、给来和走的样式的名字定义为 v-enter-active | v-leave-active 【设置name的值,需要把v 改成它】
     -->
    <!-- //? 注意 vue解析的时候 <transition> 就没有了 -->
    <!-- //todo :appear="true" [值需要的是布尔值,所以需要用v-bind绑定] 也可以直接写 【appear】
         //* appear 使用效果是:打开页面立马执行一次过来的动画 -->
    <transition name="hello" appear>
      <h1 v-show="isShow">你好啊!</h1>
    </transition>
  </div>
</template>

<script>
export default {
  name: "Test",
  data() {
    return {
      isShow: true,
    };
  },
};
</script>

<style>
h1 {
  background-color: orange;
}
.hello-enter-active {
  animation: gbase 1s;
}
.hello-leave-active {
  animation: gbase 1s reverse;
}
@keyframes gbase {
  from {
    transform: translateX(-100%);
  }
  to {
    transform: translateX(0px);
  }
}
</style>
  • 其他属性样式

  • <transition>标签 里面只能使用一个 DOM 标签
  • 使用 <transition-group> 可以里面放多个标签使用动画 【但是里面加动画的标签需要加 唯一标识key
代码语言:javascript
复制
    <transition-group name="hello" appear>
      <h1 v-show="isShow" key="1">你好啊!</h1>
      <h1 v-show="!isShow" key="2">我不好啊!</h1>
    </transition-group>
  • css3方案2
代码语言:javascript
复制
<style>
h1 {
  background-color: orange;
  /* transition: 0.5 linear; */
}
todo 进入的起点
.hello-enter {
  transform: translateX(-100%);
}
 todo 进入的终点
.hello-enter-to {
  transform: translateX(0px);
}
 todo 离开的起点
.hello-leave {
  transform: translateX(0px);
}
 todo 离开的终点
.hello-leave-to {
  transform: translateX(-100%);
}

简写 :进入的起点 就是 离开的终点
.hello-enter,
.hello-leave-to {
  transform: translateX(-100%);
}
.hello-enter-active,
.hello-leave-active {
  或者写在需要加动画的标签里面
  transition: 0.5 linear;
}
简写 :进入的终点 就是 离开的起点
.hello-enter-to,
.hello-leave {
  transform: translateX(0px);
}
</style>

2、vue2配合 animate库使用动画

  • npm install animate.css : 安装并使用动画库
  • import "animate.css"; 引入该库
  • 设置 name="animate__animated animate__bounce"
  • https://animate.style/里面找动画,把动画名称赋值给enter-active-class 、leave-active-class这两个属性
代码语言:javascript
复制
    <transition-group
      name="animate__animated animate__bounce"
      appear
      enter-active-class="animate__jackInTheBox"
      leave-active-class="animate__fadeOut"
    >
      <h1 v-show="isShow" key="1">你好啊!</h1>
    </transition-group>
  • 源码
代码语言:javascript
复制
<template>
  <div>
    <button @click="isShow = !isShow">显示/隐藏</button>

    <!-- // todo 2、设置 name="animate__animated animate__bounce" -->
    <!-- // todo 3、去https://animate.style/ 里面找动画 -->
    <transition-group
      name="animate__animated animate__bounce"
      appear
      enter-active-class="animate__jackInTheBox"
      leave-active-class="animate__fadeOut"
    >
      <h1 v-show="isShow" key="1">你好啊!</h1>
    </transition-group>
  </div>
</template>

// npm install animate.css : 安装并使用动画库
<script>
// todo 1、因为是一个样式,可以直接引入文件
import "animate.css";

export default {
  name: "Test",
  data() {
    return {
      isShow: true,
    };
  },
};
</script>

<style>
h1 {
  background-color: orange;
  /* transition: 0.5 linear; */
}
</style>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-03-11,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 注意:动画必须使用v-if || v-show配合
  • 1、Vue2配Css3实现
  • 2、vue2配合 animate库使用动画
相关产品与服务
标签
标签(Tag)是腾讯云推出的云资源管理工具,您可从不同维度对具有相同特征的云资源进行分类、搜索和聚合,从而轻松管理云上资源。 标签是由标签键和标签值共同组成,您可以为云资源创建和绑定标签
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档