前端vue transition过渡动画

首先新建一个基于uni-app框架为transition.vue的测试文件,在其中编写如下JavaScript、HTML和CSS代码:

xml 复制代码
<template><view class="content"><div id="Application"><div :class="cls" @click="run"></div></div></view></template>

<script>export default {data() {return {

cls: "demo"}},onLoad() {

},methods: {run() {if (this.cls == "demo") {this.cls = "demo-ani"} else {this.cls = "demo"}}}}</script>

<style>.content {display: flex;flex-direction: column;align-items: center;justify-content: center;}

/* .demo {width: 100px;height: 100px;background-color: red;} */

.demo {width: 100px;height: 100px;background-color: red;transition-property: width, height, background-color;transition-duration: 2s;transition-timing-function: linear;transition-delay: 1s;}

.demo-ani {width: 200px;height: 200px;background-color: blue;transition: width 2s, height 2s, background-color 2s;}</style>

如以上代码所示,CSS中定义的demo-ani类中指定了transition属性,这个属性中可以设置要过渡的属性以及动画时间。运行上面的代码,单击页面中的色块,可以看到色块变大的过程会附带动画效果,颜色变化的过程也附带动画效果。上面的示例代码实际上使用了简写方式,我们也可以逐条属性对动画效果进行设置。

其中,transition-property用来设置动画的属性;transition-duration用来设置动画的执行时长;transition-timing-function用来设置动画的执行方式,linear表示以线性的方式执行;transition-delay用来进行延时设置,即延时多长时间后开始执行动画。

8.1.2 keyframes动画

transition动画适合用来创建简单的过渡效果。CSS3中支持使用animation属性来配置更加复杂的动画效果。animation属性根据keyframes配置来执行基于关键帧的动画效果。新建一个名为keyframes.vue的测试文件。编写如下代码:

xml 复制代码
<template><view class="content"><div id="Application"><div :class="cls" @click="run"></div></div></view></template>

<script>export default {data() {return {

cls: "demo"}},onLoad() {

},methods: {run() {if (this.cls == "demo") {this.cls = "demo-ani"} else {this.cls = "demo"}}}}</script>

<style>.content {display: flex;flex-direction: column;align-items: center;justify-content: center;padding-top: 60px;}

/* .demo {width: 100px;height: 100px;background-color: red;} */



@keyframes animation1 {0% {background-color: red;width: 100px;height: 100px;}

25% {background-color: orchid;width: 200px;height: 200px;}

75% {background-color: green;width: 150px;height: 150px;}

100% {background-color: blue;width: 200px;height: 200px;}}

.demo {width: 100px;height: 100px;background-color: red;}

.demo-ani {animation: animation1 4s linear;width: 200px;height: 200px;background-color: blue;}</style>

在上面的CSS代码中,keyframes用来定义动画的名称和每个关键帧的状态,0%表示动画起始时的状态,25%表示动画执行到1/4时的状态,同理,100%表示动画的终止状态。对于每个状态,我们将其定义为一个关键帧,在关键帧中,可以定义元素的各种渲染属性,比如宽和高、位置、颜色等。在定义keyframes时,如果只关心起始状态与终止状态,也可以这样定义:

css 复制代码
@keyframes animationl {from {background-color:red; width: 100px; height: 100px;}to {background-color:orchid; width: 200px; height: 200px;}}

定义好了keyframes关键帧,在编写CSS样式代码时可以使用animation属性为其指定动画效果,如以上代码设置要执行的动画为名为animation1的关键帧动画,执行时长为4秒,执行方式为线性。animation的这些配置项也可以分别进行设置,示例如下:

css 复制代码
.demo-ani {/*设置关键帧动画名称 */animation-name:animationl;/*设置动画时长 */animation-duration:3s;/*设置动画播放方式:渐入渐出*/animation-timing-function:ease-in-out;/*设置动画播放的方向*/animation-direction:alternate;/*设置动画播放的次数 */animation-iteration-count: infinite;/*设置动画的播放状态*/animation-play-state:running;/*设置播放动画的延迟时间*/ animation-delay:1s;/*设置动画播放结束应用的元素样式*/ animation-fill-mode:forwards; width: 200px; height: 200px;background-color:blue;

}

通过上面的范例,我们已经基本了解了如何使用原生的CSS,有了这些基础,再使用Vue中提供的动画相关API时会非常容易。

阅读全文下载完整组件代码请关注微信公众号: 前端组件开发

相关推荐
Mintopia4 分钟前
🤖 AI 决策 + 意图OS:未来软件形态的灵魂共舞
前端·人工智能·react native
攀登的牵牛花5 分钟前
前端向架构突围系列 - 框架设计(四):依赖倒置原则(DIP)
前端·架构
Van_Moonlight5 分钟前
RN for OpenHarmony 实战 TodoList 项目:已完成未完成数量显示
javascript·开源·harmonyos
程序员爱钓鱼13 分钟前
Node.js 编程实战:测试与调试 —— 日志与监控方案
前端·后端·node.js
计算机学姐18 分钟前
基于SpringBoot的汉服租赁系统【颜色尺码套装+个性化推荐算法+数据可视化统计】
java·vue.js·spring boot·后端·mysql·信息可视化·推荐算法
+VX:Fegn089519 分钟前
计算机毕业设计|基于springboot + vue建筑材料管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
Mapmost21 分钟前
数字孪生项目效率翻倍!AI技术实测与场景验证实录
前端
小酒星小杜25 分钟前
在AI时代,技术人应该每天都要花两小时来构建一个自身的构建系统-Input篇
前端·程序员·架构
雪碧聊技术27 分钟前
ElementPlus徽章组件:展示日期面板每天未完成的待办数量
vue.js·日期选择器·elementplus·el-badge徽章组件
Cache技术分享33 分钟前
290. Java Stream API - 从文本文件的行创建 Stream
前端·后端