css3教程,一个利用animation动画做的loading效果
发布于 作者:苏南大叔 来源:程序如此灵动~

本文描述一个利用css3
的animation
特性写的一个loading
效果,效果非常不错,建议学习。css
的动画虽然也是以普通css
代码方式使用的,但是它有着自己的独特逻辑。
苏南大叔的程序如此灵动博客,记录苏南大叔和计算机代码的故事。测试环境:chrome@107.0.5304.88
。
最小结构单元
<div class="loading">
<span></span>
</div>
<style>
@keyframes load {
25% {
border-radius: 50%;
background-color: #dba617;
}
50% {
border-radius: 0;
background-color: black;
}
}
.loading {
width: 105px;
height: 105px;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
.loading span {
width: 50px;
height: 50px;
background-color: rgb(255, 255, 255);
animation: load 5s linear infinite;
}
</style>
重点在于:首先定义动画load
的内容:
@keyframes load{}
然后再调用:
animation: load 5s linear infinite;
其中的5s
就是用来执行一个周期的动画的时长。
而span
正常情况下是个inline
的布局,所以,通过flex
的方式,使得span
支棱起来。关于flex
布局的解读,请参考:
复制多个,呼吸效果
复制多个span
,每个应用都这个动画,然后使用animation-delay
属性设置不同的动画延迟。
span:nth-child(1) {
animation-delay: 0.5s;
}
span:nth-child(2) {
animation-delay: 1s;
}
span:nth-child(3) {
animation-delay: 1.5s;
}
span:nth-child(4) {
animation-delay: 2s;
}
其中css
中的nth-child
是从1开始计数的,这个和以往的认知概念有所不同。参考文字:
https://newsn.net/say/css-nth-child.html
完整代码
<div class="loading">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
<style>
.loading {
width: 105px;
height: 105px;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
@keyframes load {
25% {
border-radius: 50%;
background-color: #dba617;
}
50% {
border-radius: 0;
background-color: black;
}
}
.loading span {
width: 50px;
height: 50px;
background-color: rgb(255, 255, 255);
animation: load 5s linear infinite;
/* animation-delay: 0.5s; */
}
span:nth-child(1) {
animation-delay: 0.5s;
}
span:nth-child(2) {
animation-delay: 1s;
}
span:nth-child(3) {
animation-delay: 1.5s;
}
span:nth-child(4) {
animation-delay: 2s;
}
</style>
结束语
更多css
教程文章,请点击苏南大叔的博客:


