冇事来学系--Vue2.0中Vue封装的过度与动画

简介: Vue封装的过度与动画

Vue封装的过度与动画


作用:在插入、更新或移除DOM元素时,在合适的时候给元素添加 含有过渡或者动画 的样式类名(操作元素时,Vue会自动帮我们添加指定的样式

写法:

写法:

  1. 准备好样式:
  1. 元素进入的样式 (类选择器):
  1. v-enter:进入的起点
  2. v-enter-active:进入过程中
  3. v-enter-to:进入的终点
  1. 元素离开的样式:
  1. v-leave:离开的起点
  2. v-leave-active:离开的过程中
  3. v-leave-to:离开的终点
  1. 使用包裹要过度的元素,并配置name属性
  2. 使用包裹要过度的元素,并配置name属性

要让哪个元素有过渡效果,就用transition包裹

注意:配置name属性之后,写样式时要用name的属性值来替换掉v,如 name="hello",则样式的选择器要写hello-enter、hello-enter-active等等

<transition name="hello">
  <h1 v-show="isShow">你好阿!</h1>
</transition>

注意:如果有多个元素要过度,则需要使用包裹这些元素,且每个元素都要指定key值

<transition-group name="hello">
  <h1 v-show="isShow" key="1">你好阿!</h1>
  <h1 v-show="isShow" key="2">前端!</h1>
</transition-group>

Vue中使用动画

  1. 先定义好一个动画(@keyframes)
  2. 准备样式好,在样式中使用动画
  1. v-enter-active:指定进入过程中的动画
  2. v-leave-active:指定离开过程中的动画
  1. 使用包裹使用动画的元素,并设置name属性
  2. 还可以在中设置appear属性,让元素一进入页面的时候就展示动画
<template>
  <div>
  <transition name="hello" :appear="true">  <!-- 可以简写为只写appear -->
  <h1 v-show="isShow">你好阿!</h1>
  </transition>
  <button @click="isShow=!isShow">显示/隐藏</button>
  </div>
</template>
<script>
  export default {
    name: 'Test',
    data(){
      return {
        isShow: 'true'
      }
    }
  }
</script>
<style>
  h1 {
    background-color: pink;
  }
  .hello-enter-active {
    animation: move 0.5s linear;
  }
  .hello-leave-active {
    animation: move 0.5s linear reverse;    /*进入过程和离开过程动画一样,方向相反*/
  }
  /*声明动画*/
  @keyframes move {
    from {
      transform: translateX(-100%);
    }
    to {
      transform: translateX(0px);
    }
  }
</style>


Vue中使用过渡


<template>
  <div>
  <transition name="hello" :appear="true">  <!-- 可以简写为只写appear -->
  <h1 v-show="isShow">你好阿!</h1>
  </transition>
  <button @click="isShow=!isShow">显示/隐藏</button>
  </div>
</template>
<script>
  export default {
    name: 'Test',
    data(){
      return {
        isShow: 'true'
      }
    }
  }
</script>
<style>
  h1 {
    background-color: red;
  }
  /* 进入的起点、离开的终点 */
  .hello-enter-from, .hello-leave-to {
    transform: translateX(-100%)
  }
  .hello-enter-active, .hello-leave-active {   /* 把过渡设置到过程中 */
    transition: all 0.5s linear 
  }
  /* 进入的终点、离开的起点 */
  .hello-enter-to, .hello-leave-from {
    transform: translateX(0)
  }
</style>


目录
相关文章
|
23小时前
|
JavaScript 开发工具 git
Vue 入门系列:.env 环境变量
Vue 入门系列:.env 环境变量
7 1
|
1天前
|
缓存 监控 JavaScript
探讨优化Vue应用性能和加载速度的策略
【5月更文挑战第17天】本文探讨了优化Vue应用性能和加载速度的策略:1) 精简代码和组件拆分以减少冗余;2) 使用计算属性和侦听器、懒加载、预加载和预获取优化路由;3) 数据懒加载和防抖节流处理高频事件;4) 图片压缩和选择合适格式,使用CDN加速资源加载;5) 利用浏览器缓存和组件缓存提高效率;6) 使用Vue Devtools和性能分析工具监控及调试。通过这些方法,可提升用户在复杂应用中的体验。
8 0
|
1天前
|
JavaScript
vue知识点
vue知识点
10 0
|
1天前
|
JavaScript 前端开发
vue(1),小白看完都会了
vue(1),小白看完都会了
|
1天前
|
JavaScript 前端开发 定位技术
Vue使用地图以及实现轨迹回放 附完整代码
Vue使用地图以及实现轨迹回放 附完整代码
Vue使用地图以及实现轨迹回放 附完整代码
|
前端开发 JavaScript
初识 Vue(24)---(Vue 中同时使用过渡和动画)
Vue 中同时使用过渡和动画 在上篇博客 《Vue 中使用 animate.css 库》基础上开始这篇博客 在上篇博客中,完成了 引入 animate.
1215 0
|
前端开发 内存技术
Vue_同时使用过渡和动画
在上一节我们用animate动画库,在刷新页面时没有动画 如何解决第一次就显示动画内容呢? 在transform 上加上appear 和appear-active-class <transition name='fade' appear enter-active-class='animate.
1534 0
|
1天前
|
JavaScript
Vue中避免滥用this去读取data中数据
Vue中避免滥用this去读取data中数据
|
1天前
|
JavaScript
vue中使用pinia及持久化
vue中使用pinia及持久化
5 0
|
1天前
|
JavaScript 前端开发 UED
Vue class和style绑定:动态美化你的组件
Vue class和style绑定:动态美化你的组件