当前位置: 首页 > news >正文

vue动画

1、动画实现

(1)、操作css的transition或animation

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

(3)、过渡的相关类名:

xxx-enter-active: 进入的时候激活样式

xxx-leave-active: 离开的时候激活样式

<template><div><button @click="isShow = !isShow">显示/隐藏</button><!-- 1、name="hello" 定义类名的时候使用,默认是v-2、appear 一开始就有动画3、transition最终是没有形成标签的,只是起到占位作用--><transition name="hello" appear><h1 v-show="isShow">你好啊!</h1></transition></div>
</template><script>
export default {name: "Test",data() {return {isShow: true,};},
};
</script><style scoped>
h1 {background-color: orange;
}/* 默认是v-enter-active */
.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>

2、过渡实现:

(1)、准备好样式:

元素进入的样式

v-enter:进入的起点

v-enter-active进入过程中

v-enter-to:进去的终点

元素离开的样式

v-leave:离开的起点

v-leave-active:离开的过程中

v-leave-to:离开的终点

(2):在目标元素外包裹,配置的name属性的属性值,替换上面样式名的v

(3)、如果要让页面一开始就显示动画,需要添加appear

<template><div><button @click="isShow = !isShow">显示/隐藏</button><transition name="hello" appear><h1 v-show="!isShow">你好啊!</h1></transition></div>
</template><script>
export default {name: "Test2",data() {return {isShow: true,};},
};
</script><style scoped>
h1 {background-color: orange;/* 3、写法一,设置过渡 */transition: 0.5s linear;
}/*1.1 hello-enter 进入的起点 */
.hello-enter {transform: translateX(-100%);
}/*1.2 hello-enter-to进入的终点*/
.hello-enter-to {transform: translateX(0);
}/*2.1 hello-leave离开的起点  */
.hello-leave {transform: translateX(0);
}/*2.2 hello-leave-to离开的终点 */
.hello-leave-to {transform: translateX(-100%);
}/* 3.2 写法二、设置进入离开的过程 */
/* .hello-enter-active,
.hello-leave-active {transition: 0.5s linear;
} */
</style>

(4)、备注:若多个元素需要过渡,则需要使用,,且每个元素要配合指定的key值

需求:样式互斥,交叉过渡

<template><div><button @click="isShow = !isShow">显示/隐藏</button><transition-group name="hello" appear><h1 v-show="!isShow" key="1">你好啊!</h1><h1 v-show="isShow" key="2">bdqn</h1></transition-group></div>
</template>
······

3、第三方动画库 Animate.css

安装动画库

npm install animate.css --save

使用时,需要导入

import "animate.css";

参考网址:

Animate.css | A cross-browser library of CSS animations.

具体使用demo

<template><div><button @click="isShow = !isShow">显示/隐藏</button><!-- 第二步:name="animate__animated animate__bounce" 固定的,必须要写 --><!-- 第三步: enter-active-class='效果' leave-active-class='效果'--><transition-group appear name="animate__animated animate__bounce" enter-active-class="animate__backInDown"leave-active-class="animate__backOutUp"><h1 style="background-color: orange;" v-show="!isShow" key="1">你好啊!</h1><h1 style="background-color: aquamarine;" v-show="isShow" key="2">bdqn</h1></transition-group></div>
</template><script>
// 第一步:引入动画库
import "animate.css";export default {name: "Test3",data() {return {isShow: true,};},
};
</script>

相关文章:

  • 索引语法SQL性能分析索引的使用
  • 依赖注入(DI)与自动装配:本质就是“赋值“吗?
  • PBKDF2全面指南(SpringBoot实现版)
  • AI agents系列之智能体框架介绍
  • Docker华为云创建私人镜像仓库
  • K-均值聚类机器学习算法的优缺点
  • C++第三方库【JSON】nlohman/json
  • CefSharp浏览器(AntdUI.Tabs)标签页关闭时资源释放ChromiumWebBrowser示例源码
  • 【文献笔记】LLM-based and retrieval-augmented control code generation
  • SmolVLM新模型技术解读笔记
  • 联邦学习与协作学习:数据隐私与模型协同进化的未来之路
  • 在SpringBoot中访问 static 与 templates 目录下的内容
  • 在 MySQL 单表存储 500 万数据的场景下,如何设计读取
  • 冲刺高分!挑战7天一篇nhanes机器学习SCI!DAY1-7
  • 1023 Have Fun with Numbers
  • Python基础语法——常量变量
  • 【Linux】进程的程序替换、自定义shell命令行解释器
  • 批量将多个文件按扩展名分类到不同文件夹
  • 如何实现动态请求地址(baseURL)
  • 数据库案例1--视图和索引
  • 市民建议公交广播增加“请勿大声喧哗”提示,上海交通委回复
  • 十四届全国人大常委会第十五次会议将于4月27日至30日举行
  • “替父追债被判寻衅滋事案”从犯获国赔,该案司法机关共赔偿217万元
  • 独家专访|阿来:人只活几十年,我得写点不一样的
  • 中超浙江队外援布彭扎坠亡,警方通报:排除刑事案件
  • 何立峰:着力推进外贸高质量发展,共同努力建设开放型世界经济