一篇文章带您学会CSS的动画

ops/2024/10/21 19:33:26/

动画和过渡的区别

过渡:实现两个状态间的变化过程。

动画:实现多个状态间的变化过程。动画过程可控(重复播放,最终动画,是否暂停)

动画的实现步骤

1.定义动画

书写格式

css">@keyframes 动画名称{from{}to{}
}

2.使用格式

书写格式

css">animation:动画名称 动画花费的时间;

完整的书写格式

css">animation:动画名称 动画时长 速度曲线 延迟时间 重复次数 动画方向 执行完毕时状态;

说明:动画名称和动画时长必须赋值,取值不分先后顺序 , 第一个时间表示动画时长,第二个时间表示延迟时间。

动画属性说明:

属性作用取值
animation-name动画名称
animation-duration动画时长
animation-delay延迟时间
animation-fill-mode动画执行完毕时状态forwards:最后一帧状态。backwards:第一帧状态
animation-timing-function速度曲线steps(数字):逐帧动画
animation-iteration-count重复次数infinite 为无限循环
animation-direction动画执行方向alternate为反复
animation-play-state暂停动画paused为暂停,通常配合:hover一起使用

动画体验

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>/* 定义动画 */@keyframes ani {form{background-color: red;}to{background-color: blue;}}/* 使用动画 */div {width: 200px;height: 200px;border: 1px solid #000;margin: 100px auto;/*它变化颜色不是一下就过了,而是有一个过渡,我们称之为补帧动画*/animation: ani 10s;}</style>
</head>
<body><div></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>@keyframes ani {0% {background-color: red;transform: scale(1.1);}1% {background-color: rgb(65, 46, 46);transform: scale(1.5);}9% {background-color: rgb(169, 227, 36);transform: scale(2);}20% {background-color: rgb(10, 223, 155);transform: scale(2.2);}30% {background-color: rgb(10, 223, 155);transform: scale(2.2);}50% {background-color: aquamarine;transform: scale(0.5);}80% {background-color: rgb(85, 18, 218);transform: scale(0.1);}90% {background-color: rgb(255, 2, 204);transform: scale(0.9);}100% {background-color: rgb(253, 0, 0);transform: scale(1);}}div {width: 200px;height: 200px;border: 1px solid #000;margin: 100px auto;animation: ani 20s;}</style>
</head><body><div></div>
</body></html>

3.animation-delay 延迟

在动画里面,让一下元素慢一点执行动画。

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.box {display: flex;justify-content: space-evenly;align-items: end;width: 1200px;height: 600px;margin: 0 auto;border: 1px solid #000;}.box .son {width: 50px;height: 50px;border-radius: 50px;background-color: aqua;animation-name: mover;animation-duration: 30s;}.box:nth-child(1) {animation-delay: 10s;}.box:nth-child(2) {animation-delay: 10s;}.box:nth-child(3) {animation-delay: 20s;}@keyframes mover {0% {background-color: antiquewhite;transform: translateY(-200px);}10% {background-color: rgb(255, 145, 1);transform: translateY(-150px);}20% {background-color: rgb(156, 253, 0);transform: translateY(-100px);}30% {background-color: rgb(0, 255, 85);transform: translateY(-50px);}40% {background-color: rgb(0, 217, 255);transform: translateY(0px);}50% {background-color: rgb(0, 0, 255);transform: translateY(-150px);}60% {background-color: rgb(166, 0, 255);transform: translateY(-200px);}70% {background-color: rgb(255, 0, 195);transform: translateY(-250px);}80% {background-color: rgb(255, 0, 64);transform: translateY(-180px);}90% {background-color: rgb(17, 52, 24);transform: translateY(-100px);}100% {background-color: rgb(7, 255, 57);transform: translateY(0px);}}</style>
</head><body><div class="box"><div class="son"></div><div class="son"></div><div class="son"></div></div>
</body></html>

4.animation-fill-mode 动画执行完毕时状态

属性值:

forwards:动画结束后,状态停止在最后一帧。

backwards:动画结束后,状态固定在第一帧。

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>@keyframes move {0% {transform: translateX(400px);background-color: aqua;}10% {transform: translateX(410px);background-color: aquamarine;}20% {transform: translateX(450px);background-color: rgb(0, 94, 255);}50% {transform: translateX(400px);background-color: rgb(255, 89, 0);}70% {transform: translateX(200px);background-color: rgb(179, 255, 0);}90% {transform: translateX(50px);background-color: rgb(0, 255, 64);}100% {transform: translateX(0px);background-color: rgb(255, 0, 111);}}.box {width: 200px;height: 200px;background-color: pink;animation-name: move;animation-duration: 10s;/* forwards:动画结束后,状态停止在最后一帧。backwards:动画结束后,状态固定在第一帧。*/animation-fill-mode: backwards;}</style>
</head><body><div class="box"></div>
</body></html>

5.animation-timing-function 速度曲线

steps(数字):逐帧动画

默认的速度曲线:慢快满

linear:匀速

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>@keyframes move {0% {transform: translateX(0);}100% {transform: translateX(1000px);}}.box {width: 200px;height: 200px;background-color: blue;animation-name: move;animation-duration: 6s;}.box:nth-child(2) {/*linear:匀速*/animation-timing-function: linear;}</style>
</head>
<body><div class="box"></div><div class="box"></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>@keyframes move {0% {transform: translateX(0);}100% {transform: translateX(1000px);}}.box {width: 200px;height: 200px;background-color: blue;animation-name: move;animation-duration: 6s;}.box:nth-child(2) {animation-timing-function: linear;}.box:nth-child(3) {animation-timing-function: steps(8);}</style>
</head>
<body><div class="box"></div><div class="box"></div><div class="box"></div>
</body>
</html>

6.animation-iteration-count 重复次数

infinite 为无限循环

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>@keyframes move {0% {transform: translateX(0);}100% {transform: translateX(1000px);}}.box {width: 200px;height: 200px;background-color: blue;animation-name: move;animation-duration: 6s;/* 重复播放 */animation-iteration-count: infinite;}.box:nth-child(2) {animation-timing-function: linear;}.box:nth-child(3) {animation-timing-function: steps(8);}</style>
</head><body><div class="box"></div><div class="box"></div><div class="box"></div>
</body></html>

7.animation-direction 动画执行方向

alternate:反复

reverse:反向

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>@keyframes move {0% {transform: translateX(0);}100% {transform: translateX(1000px);}}.box {width: 200px;height: 200px;background-color: blue;animation-name: move;animation-duration: 6s;/* 重复播放 */animation-iteration-count: infinite;/* 下一次播放就会反着来 */animation-direction: alternate;}.box:nth-child(2) {animation-timing-function: linear;}.box:nth-child(3) {animation-timing-function: steps(8);}</style>
</head><body><div class="box"></div><div class="box"></div><div class="box"></div>
</body></html>

8.animation-play-state 暂停动画

paused为暂停,通常配合:hover一起使用。

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.box {width: 300px;height: 300px;background-color: red;animation-name: move;animation-duration:2s ;animation-iteration-count: infinite;animation-direction: alternate;}.box:hover {/* 鼠标经过的时候暂停 */animation-play-state: paused;}@keyframes move {0% {transform: translateY(0);}100% {transform: translateY(300px);}}</style>
</head>
<body><div class="box"></div>
</body>
</html>

注意:本文章是笔者的学习笔记,因为笔者的能力有限,会出现很多没有想过的问题,所以,如果您在浏览或者运行代码的时候出现问题,请在评论区留言,笔者看到后会在第一时间处理,谢谢。


http://www.ppmy.cn/ops/5564.html

相关文章

记一次nacos注册服务IP错误的解决方案

我们以常见的微服务脚手架RuoYi-Cloud-Plus 为例 场景 服务网关-》业务服务不通 在微服务场景中&#xff0c;网关会从注册中心获取服务的列表&#xff0c;这些数据以键值对的形式存储在本地缓存中&#xff0c;key是服务的名称&#xff0c;value是服务的IP地址和端口号&#…

Xamarin.Android中“ADB0020: Android ABI 不匹配。你正将应用支持的“armeabi-v7a;arm64-v8a”异常处理

这里写自定义目录标题 1、问题2、解决 1、问题 在Xamarin.Android中出现ADB0020: Android ABI 不匹配。你正将应用支持的“armeabi-v7a;arm64-v8a”ABI 部署到 ABI“x86_64;x86”的不兼容设备。应创建匹配其中一个应用 ABI 的仿真程序&#xff0c;或将“x86_64”添加到应用生成…

OllamaFunctions 学习笔记

OllamaFunctions 学习笔记 0. 引言1. 使用方法2. 用于提取 0. 引言 此文章展示了如何使用 Ollama 的实验性包装器&#xff0c;为其提供与 OpenAI Functions 相同的 API。 1. 使用方法 您可以按照与初始化标准 ChatOllama 实例类似的方式初始化 OllamaFunctions&#xff1a; …

腾讯云免费ssl证书申请与宝塔手动部署

1.在我的证书 - SSL 证书 - 控制台 (tencent.com)页面点击“申请免费证书” 2.在申请页面填写域名、邮箱&#xff0c;对于其中“验证方式”&#xff0c;如果服务器是部署在腾讯云的话&#xff0c;可以选“自动DNS” 3.等待审核通过之后&#xff0c;在我的证书 - SSL 证书 - 控…

Flutter 的 showDialog 和 showCupertinoDialog 有什么区别?

我将我的 App 里用的 Flutter 升级到了 3.19&#xff0c;没想到&#xff0c;以前我用 showDialog 和 AlertDialog 组合创建的二次确认框&#xff0c;变得无敌难看了&#xff0c;大幅度增加了整个框的圆角和里面默认按钮的圆角。不得已&#xff0c;我必须修改一下&#xff0c;以…

大数据测试:构建Hadoop和Spark分布式HA运行环境

随着大数据技术的不断发展&#xff0c;Hadoop和Spark已成为处理大规模数据的热门框架。在生产环境中&#xff0c;高可用性&#xff08;HA&#xff09;是至关重要的&#xff0c;以确保数据处理和分析任务不受中断。本文将详细介绍如何构建 Hadoop和Spark分布式HA运行环境&#x…

Flutter-----异步编程:Future和Stream

异步编程&#xff1a;使用 Future 和 async-await | Dart 什么是异步操作/异步操作的作用&#xff1f; Dart 代码运行在单个执行“线程”中。如果 Dart 代码在执行时阻塞&#xff0c;例如&#xff1a;处理一个需要长时间运行的计算操作或等待 I/O 完成。此时整个程序会被“冻…

Golang插件系统实现

插件可以在解耦的基础上灵活扩展应用功能&#xff0c;本文介绍了如何基于Golang标准库实现插件功能&#xff0c;帮助我们构建更灵活可扩展的应用。原文: Plugins with Go 什么是插件 简单来说&#xff0c;插件就是可以被其他软件加载的软件&#xff0c;通常用于扩展应用程序的功…