首页 > 其他分享 >38-Vue脚手架-过渡与动画

38-Vue脚手架-过渡与动画

时间:2023-11-08 16:57:57浏览次数:40  
标签:38 vue 元素 leave Vue 过渡 enter 脚手架 hello

Vue封装的过渡与动画

在插入、更新或移除 DOM 元素时,在合适的时候给元素添加样式类名

 

transition 讲解

transition 是 vue 内置的一个组件,我们可以直接使用。

<transition> 元素作为单个元素 / 组件的过渡效果。<transition>  只会把过渡效果应用到其包裹的内容上,而不会额外渲染 DOM 元素,也不会出现在可被检查的组件层级中


注意:<transition>  只能用来包裹单个元素,如果包裹了多个元素则用 <transition-group>   transition 组件上定义的属性: 1)name:string类型,用于自动生成 CSS 过渡类名。例如:name: 'hello' 将自动拓展为 .hello-enter.hello-enter-active 等。如果没有定义 name ,默认的类名前缀为 "v" ,例如 .v-enter 2)appear:boolean类型,表示是否在初始渲染时使用过渡。默认为 false。 3)type:string类型,指定过渡事件类型,侦听过渡何时结束。有效值为 "transition""animation"默认 Vue.js 将自动检测出持续时间长的为过渡事件类型。 4)mode:string类型,控制离开/进入过渡的时间序列。有效的模式有 "out-in""in-out"默认同时进行。 5)duration:number类型或者对象类型:{ enter: number, leave: number } ,用来指定过渡的持续时间。默认情况下,Vue 会等待过渡所在根元素的第一个 transitionend 或 animationend 事件。
实现过渡的原理,就是通过在某一时刻给 transition 包裹的的元素上动态添加和删除 class 类名的方式来实现(在合适的时候给元素添加样式类名)。 元素进入的样式(Enter):
  • v-enter进入的起点,在元素被插入之前生效,在元素被插入之后的下一帧移除。
  • v-enter-active进入的过程,在整个进入过渡的阶段中应用,在元素被插入之前生效,在过渡 / 动画完成之后移除。 这个类可以被用来定义进入过渡的过程时间,延迟和曲线函数。
  • v-enter-to进入的终点,在元素被插入之后下一帧生效 (与此同时 v-enter 被移除),在过渡 / 动画完成之后移除。

元素离开的样式(Leave):

  • v-leave离开的起点,在离开过渡被触发时立刻生效,下一帧被移除。
  • v-leave-active离开的过程,在整个离开过渡的阶段中应用,在离开过渡被触发时立刻生效,在过渡 / 动画完成之后移除。 这个类可以被用来定义离开过渡的过程时间,延迟和曲线函数。
  • v-leave-to离开的终点,在离开过渡被触发之后下一帧生效 (与此同时 v-leave 被删除),在过渡 / 动画完成之后移除。
 

1. 单元素 / 单组件的过渡

Vue 提供了 transition 的封装组件,在下列情形中,可以给任何元素和组件添加进入 / 离开过渡:

  • 条件渲染 (使用 v-if )
  • 条件展示 (使用 v-show )
  • 动态组件
  • 组件根节点

简单案例:

src/components/Test.vue

<template>
  <div>
    <button @click="isShow = !isShow">显示/隐藏</button>
    <!--<Transition> 会在一个元素或组件进入和离开 DOM 时应用动画-->
    <transition>
      <h1 class="demo1" v-show="isShow">你好啊!我是Test</h1>
    </transition>

    <!--定义name为"hello",则后续格式应该用.hello-enter-active-->
    <!--:appear="true" 代表 appear属性 为true, 默认显示进入动画-->
    <transition name="hello" :appear="true">
      <h1 class="demo2" v-show="isShow">你好啊!我是Test</h1>
    </transition>
  </div>
</template>

<script>
  export default{
    // eslint-disable-next-line vue/multi-word-component-names
    name:"Test",
    data(){
      return{
        isShow:true
      }
    }
  }
</script>

<style scoped>
  .demo1{
    background-color: orange;
  }
  .demo2{
    background-color: skyblue;
  }

  .v-enter-active{
    animation: malingshu 0.5s linear;
  }

  .v-leave-active{
    animation: malingshu 0.5s linear reverse;
  }

  .hello-enter-active{
    animation: malingshu 0.5s linear;
  }

  .hello-leave-active{
    animation: malingshu 0.5s linear reverse;
  }

  /* 动画 */
  @keyframes malingshu {
    from{
      transform: translateX(-100%);
    }
    to{
      transform: translateX(0px);
    }
  }

</style>

src/components/Test2.vue

<template>
  <div>
    <button @click="isShow = !isShow">显示/隐藏</button>
    <!--<Transition> 会在一个元素或组件进入和离开 DOM 时应用动画-->
    <transition name="hello" :appear="true">
      <h1 class="demo2" v-show="isShow">你好啊!我是Test2</h1>
    </transition>
  </div>
</template>

<script>
  export default{
    // eslint-disable-next-line vue/multi-word-component-names
    name:"Test2",
    data(){
      return{
        isShow:true
      }
    }
  }
</script>

<style scoped>
  .demo2{
    background-color: skyblue;
    //transition: 0.5s linear;
  }

  /* 进入的起点、离开的终点 */
  .hello-enter,.hello-leave-to{
    transform: translateX(-100%);
  }

  /* 进入的过程、离开的过程 */
  .hello-enter-active,.hello-leave-active{
    transition: 0.5s linear;
  }

  /* 进入的终点、离开的起点 */
  .hello-enter-to,.hello-leave,{
    transform: translateX(0);
  }

</style>

src/App.vue

<template>
  <div>
    <Test></Test>
    <hr/>
    <Test2></Test2>
  </div>
</template>

<script>
import Test from "@/components/Test.vue";
import Test2 from "@/components/Test2.vue";

export default {
  name: "App",
  components: {
    Test,
    Test2
  },
};
</script>

<style></style>

src/main.js

import Vue from "vue"
import App from "./App.vue"

// 阻止 vue 在启动时生成生产提示
Vue.config.productionTip = false

new Vue({
    el:"#app",
    render:h => h(App),
})

2. 多元素 / 多组件的过渡

在src/components/Test2.vue,使用<transition-group>标签,完成多个元素的过渡,且每个元素都要指定key值

<template>
  <div>
    <button @click="isShow = !isShow">显示/隐藏</button>
    <!--多元素过渡,使用transition-group-->
    <transition-group name="hello" :appear="true">
      <!--里面的两个元素,互斥,一个显示则另一个不显示-->
      <h1 class="demo2" v-show="!isShow" key="1">你好啊!我是Test2</h1>
      <h1 class="demo2" v-show="isShow" key="2">你好啊!我是Test2</h1>
    </transition-group>
  </div>
</template>

<script>
  export default{
    // eslint-disable-next-line vue/multi-word-component-names
    name:"Test2",
    data(){
      return{
        isShow:true
      }
    }
  }
</script>

<style scoped>
  .demo2{
    background-color: skyblue;
    //transition: 0.5s linear;
  }

  /* 进入的起点、离开的终点 */
  .hello-enter,.hello-leave-to{
    transform: translateX(-100%);
  }

  /* 进入的过程、离开的过程 */
  .hello-enter-active,.hello-leave-active{
    transition: 0.5s linear;
  }

  /* 进入的终点、离开的起点 */
  .hello-enter-to,.hello-leave,{
    transform: translateX(0);
  }


</style>

 

3. 第三方动画库 Animate.css

官方网址Animate.css | A cross-browser library of CSS animations.

安装 animate.css库

npm install animate.css --save

引入 animate.css库

import 'animate.css';

使用方法:

<transition :appear="true"
    name="animate__animated animate__bounce"
    enter-active-class="animate__swing"
    leave-active-class="animate__backOutDown"
>
    <h1 class="demo2"v-show="isShow"key="1">你好啊!我是Test3</h1>
</transition>

 在src/components/Test3.vue,完整代码:

<template>
  <div>
    <button @click="isShow = !isShow">显示/隐藏</button>
    <!--多元素过渡,使用transition-group-->
    <transition
        :appear="true"
        name="animate__animated animate__bounce"
        enter-active-class="animate__swing"
        leave-active-class="animate__backOutDown"
    >
      <h1 class="demo2" v-show="isShow" key="1">你好啊!我是Test3</h1>
    </transition>
  </div>
</template>

<script>
  import "animate.css"

  export default{
    // eslint-disable-next-line vue/multi-word-component-names
    name:"Test3",
    data(){
      return{
        isShow:true
      }
    }
  }
</script>

<style scoped>
  .demo2{
    background-color: skyblue;
    //transition: 0.5s linear;
  }
</style>

 

 

标签:38,vue,元素,leave,Vue,过渡,enter,脚手架,hello
From: https://www.cnblogs.com/REN-Murphy/p/17817770.html

相关文章

  • vue3异步组件
    父组件中,子组件的加载一般是按照先后顺序加载的,子组件加载后才会加载父组件。一个页面的子组件很多,由于会先加载子组件,那么父组件可能会出现比较长的白屏等待时间大型项目,可能需要拆分应用为更小的块,并仅在需要时再从服务器加载相关组件Vue提供defineAsyncComponent方法:import......
  • vuejs3.0 从入门到精通——Element Plus 组件库
    ElementPlus组件库一、ElementPlus 基于Vue3,面向设计师和开发者的组件库。二、完整导入https://element-plus.org/zh-CN/guide/quickstart.html#完整引入 如果你对打包后的文件大小不是很在乎,那么使用完整导入会更方便。//main.tsimport{createApp}from'vu......
  • 使用TS进行Vue-Router的Meta类型扩展
    目录1、前言2、解决1、前言使用Vue-Router时,会将一些字段信息附加到路由的Meta对象里面,比如图标icon,标题,权限等,如下:{path:'/billboard/board/:boardId',name:'billboardBoard',props:true,component:()=>import('@/views/billboard/board.vue'),meta:{......
  • [Vue] 大屏自适应问题
     可视化大屏需要自适应各种屏幕,使用了DataV的dv-full-screen-container ,v-scale-screen,但都达不到要求,dv-full-screen-container随着屏幕缩放或分辨率变化,文字也相应变化了,v-scale-screen+DataV控件时,第一次加载页面时,DataV边框宽高没有调整过来,还需要稍微拉伸一下,触发......
  • vue上传文件夹目录
    在input上面添加webkitdirectorydirectory这两个属性就能开启选择目录模式<inputref="fileIptRef"class="file-ipt"type="file"webkitdirectorydirectorymultiple@change="handleFileSelect"/>//文件上传输入框的refconstfileIptRef:any=ref(......
  • VUE上传文件夹的三种解决方案
     本文章向大家介绍VUE上传文件夹的三种解决方案,主要内容包括上传分步:、直接上代码、使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。 ​对于大文件的处理,无论是用户端还是服务端,如果一次性进行读取发送、接收都是不可取,很......
  • vue+element拖动排序功能
    vue+element拖动排序功能安装npminstallvuedraggable-S引用importdraggablefrom'vuedraggable'注册组件components:{draggable},通过draggable标签来使用代码<draggablev-model="urlPic":move="onMove"@start=......
  • vue3中使用Pinia
    Pinia是一个用于Vue的状态管理库,类似Vuex,是Vue的另一种状态管理方案三大核心:state(存储的值),getters(计算属性),actions也可支持同步(改变值的方法,支持同步和异步)npminstallpinia@nextoryarnaddpinia@next模块化封装创建实例新建store/index.ts(src目录下新建store......
  • 37-Vue脚手架-nextTick(使用nextTick优化TodoList案例)
    this.$nextTick(十分常用的功能)语法:this.$nextTick(回调函数)作用:在下一次DOM更新结束后执行其指定的回调什么时候用:当改变数据后,要基于更新后的新DOM进行某些操作时,要在nextTick所指定的回调函数中执行 案例:使用 $nextTick优化TodoList案例,在UserItem中添加一个编辑按......
  • vue计算属性computed简单使用
    computed的作用computed用data中现有的属性计算出一个新的属性,叫做计算属性,计算属性和data中属性在{{}}写法和属性一样,例如计算属性name写为<div>{{name}}</div>computed的定义位置computed的定义位置和methods还有data为同级写法为computed:{}<script>exportdefault{data()......