【Vue 学习】Vue2 的动画与过渡

Vue2 的动画与过渡

Vue 在插入更新或者移除 DOM 时,提供多种不同方式的应用过渡的效果

1. 原生 CSS3 的动画

<template>
  <h1 class="box leave">Hello,World!!</h1>
</template>


<script>
export default {
  name: "HelloWorld",
}
</script>


<style lang="less" scoped>
.box {
  margin: 0 auto;
  width: 500px;
  height: 50px;
  background-color: pink;
}

.enter {
  animation: hello-box 0.5s linear;
  background-color: black;
}

.leave {
  animation: hello-box 0.5s linear reverse;
  background-color: skyblue;
}

@keyframes hello-box {
  from {
    transform: translateX(-100%);
  }
  to {
    transform: translateX(0);
  }
}
</style>

2. Vue 的动画与过渡

  • 利用 v-isv-show 显示隐藏元素,Vue 会自动添加过渡效果
  • transition 标签最终并没有形成真实 dom
  • 使用 <transition></transition> 包裹,transition 标签有一个 name 属性,可以用来与过渡动画样式绑定
  • 默认第一次显示元素的时候是没有动画的,可以添加 appear 属性,使得第一次显示也有动画

(1)通过 name 绑定过渡样式

<template>
  <div class="main">
    <button @click="isShow = !isShow">显示与隐藏</button>
    <transition name="hello" :appear="true">
      <h1 v-if="isShow">Hello,World!!</h1>
    </transition>
  </div>
</template>


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


<style scoped>
h1 {
  background-color: red;
  text-align: center;
}

.hello-enter {
  transform: translateX(-100%);
}

.hello-enter-active {
  transition: all .5s;
}

.hello-enter-to {
  transform: translateX(0);
}

.hello-leave {
  transform: translateX(0);
}

.hello-leave-active {
  transition: all .5s;
}

.hello-leave-to {
  transform: translateX(-100%);
}
</style>

(2)通过 name 绑定动画样式

<template>
  <div class="main">
    <button @click="isShow = !isShow">显示与隐藏</button>
    <transition name="hello" :appear="true">
      <h1 v-if="isShow">Hello,World!!</h1>
    </transition>
  </div>
</template>


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


<style scoped>
h1 {
  background-color: red;
  text-align: center;
}

.hello-enter-active {
  animation: hello-box .1s linear;
}

.hello-leave-active {
  animation: hello-box .1s linear reverse;
}

@keyframes hello-box {
  from {
    transform: translateX(-100%);
  }
  to {
    transform: translateX(0);
  }
}
</style>

(3)通过 class 绑定过渡样式

<template>
  <div class="main">
    <button @click="isShow = !isShow">显示与隐藏</button>
    <transition
        name="hello"
        :appear="true"
        enter-class="hello-enter"
        enter-active-class="hello-enter-active"
        enter-to-class="hello-enter-to"
        leave-class="hello-leave"
        leave-active-class="hello-leave-active"
        leave-to-class="hello-leave-to"
    >
      <h1 v-if="isShow">Hello,World!!</h1>
    </transition>
  </div>
</template>


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


<style scoped>
h1 {
  background-color: red;
  text-align: center;
}

.hello-enter {
  transform: translateX(-100%);
}

.hello-enter-active {
  transition: all .5s;
}

.hello-enter-to {
  transform: translateX(0);
}

.hello-leave {
  transform: translateX(0);
}

.hello-leave-active {
  transition: all .5s;
}

.hello-leave-to {
  transform: translateX(-100%);
}
</style>

(4)通过 class 绑定动画样式

<template>
  <div class="main">
    <button @click="isShow = !isShow">显示与隐藏</button>
    <transition
        name="hello"
        :appear="true"
        enter-active-class="enter"
        leave-active-class="leave"
    >
      <h1 v-if="isShow">Hello,World!!</h1>
    </transition>
  </div>
</template>


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


<style scoped>
h1 {
  background-color: red;
  text-align: center;
}

.enter {
  animation: hello-box .5s linear;
}

.leave {
  animation: hello-box .5s linear reverse;
}

@keyframes hello-box {
  from {
    transform: translateX(-100%);
  }
  to {
    transform: translateX(0);
  }
}
</style>

3. 多个元素使用过渡

  • <transition></transition> 标签里面只可以有一个元素
  • 使用 <transition-group></transition-group> 标签可以包裹多个元素,但是子元素必须配置一个 key,否则报错

(1)使用多个 transition 标签

<template>
  <div class="main">
    <button @click="isShow = !isShow">显示与隐藏</button>
    <transition name="hello" :appear="true">
      <h1 v-if="isShow">Hello,World!!</h1>
    </transition>
    <transition name="hello" :appear="true">
      <h1 v-if="!isShow">Hello,World!!</h1>
    </transition>
  </div>
</template>


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


<style scoped>
h1 {
  background-color: red;
  text-align: center;
}

.hello-enter {
  transform: translateX(-100%);
}

.hello-enter-active {
  transition: all .5s;
}

.hello-enter-to {
  transform: translateX(0);
}

.hello-leave {
  transform: translateX(0);
}

.hello-leave-active {
  transition: all .5s;
}

.hello-leave-to {
  transform: translateX(-100%);
}
</style>

(2)使用 transition-group 标签

<template>
  <div class="main">
    <button @click="isShow = !isShow">显示与隐藏</button>
    <transition-group name="hello" :appear="true">
      <h1 v-if="isShow" :key="1">Hello,World!!</h1>
      <h1 v-if="!isShow" :key="2">Hello,World!!</h1>
    </transition-group>
  </div>
</template>


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


<style scoped>
h1 {
  background-color: red;
  text-align: center;
}

.hello-enter {
  transform: translateX(-100%);
}

.hello-enter-active {
  transition: all .5s;
}

.hello-enter-to {
  transform: translateX(0);
}

.hello-leave {
  transform: translateX(0);
}

.hello-leave-active {
  transition: all .5s;
}

.hello-leave-to {
  transform: translateX(-100%);
}
</style>

4. 在 Vue 中使用 Animate.css 动画库

第三方动画库

  • 自定义类目使用第三方 CSS 动画库
  • 事件的回调函数中使用第三方 JavaScript 动画库直接操作 DOM

使用第三方动画库,这里演示的是:Animate.css | A cross-browser library of CSS animations.

安装

npm install animate.css --save

引入

// 方法一:在 script 标签里面引入
import 'animate.css';
<!-- 方法二:使用 link 标签引入 -->
<head>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"
  />
</head>

使用

  • 配置 name 属性
  • 使用自定义类目,一般引入第三方动画库,仅配置自定义类名即可
<template>
  <div class="main">
    <button @click="isShow = !isShow">显示与隐藏</button>
    <!-- 
		- 配置 name 属性
		- 自定义类名
	-->
    <transition
        name="animate__animated animate__bounce"
        :appear="true"
        enter-active-class="animate__swing"
        leave-active-class="animate__backInUp"
    >
      <h1 v-if="isShow" style="background-color: red;">Hello,World!!</h1>
    </transition>
  </div>
</template>

<script>
import 'animate.css';
export default {
  name: "MyTest",
  data() {
    return {
      isShow: true,
    }
  }
}
</script>

<style scoped>
</style>
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

写代码的不谷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值