这篇文章介绍了如何使用纯CSS实现8个小球围成方形逆时针旋转的Loading特效。这个特效可以用在网页的加载过程中,使用户等待的时间更加有趣和轻松。
实现方法
使用CSS3的@keyframes规则,通过不断改变每个小球的大小,来实现看上去像在旋转的效果。同时,使用了CSS的transform属性来控制小球的尺寸。
实现步骤
- 创建一个HTML文件,并在文件中添加一个div容器,用于包含8个小球。
- 使用CSS对div容器进行样式设置,使其居中并设置宽高。
- 创建8个div元素,用于表示8个小球,并对其进行样式设置,如设置宽高、边框、圆角等。
- 使用CSS3的@keyframes规则,对每个小球设置尺寸变化的动画效果。
代码实现
以下是实现该Loading特效的HTML和CSS代码:
<div class="loading">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<style>
.loading,
.loading > div {
position: relative;
box-sizing: border-box;
}
.loading {
display: block;
font-size: 0;
color: #000;
}
.loading.la-dark {
color: #333;
}
.loading > div {
display: inline-block;
float: none;
background-color: currentColor;
border: 0 solid currentColor;
}
.loading {
width: 26px;
height: 26px;
}
.loading > div {
position: absolute;
top: 50%;
left: 50%;
width: 12px;
height: 12px;
margin-top: -6px;
margin-left: -6px;
border-radius: 100%;
animation: ball-square-spin 1s infinite ease-in-out;
}
.loading > div:nth-child(1) {
top: 0;
left: 0;
animation-delay: -1.125s;
}
.loading > div:nth-child(2) {
top: 0;
left: 50%;
animation-delay: -1.25s;
}
.loading > div:nth-child(3) {
top: 0;
left: 100%;
animation-delay: -1.375s;
}
.loading > div:nth-child(4) {
top: 50%;
left: 100%;
animation-delay: -1.5s;
}
.loading > div:nth-child(5) {
top: 100%;
left: 100%;
animation-delay: -1.625s;
}
.loading > div:nth-child(6) {
top: 100%;
left: 50%;
animation-delay: -1.75s;
}
.loading > div:nth-child(7) {
top: 100%;
left: 0;
animation-delay: -1.875s;
}
.loading > div:nth-child(8) {
top: 50%;
left: 0;
animation-delay: -2s;
}
.loading.la-sm {
width: 12px;
height: 12px;
}
.loading.la-sm > div {
width: 6px;
height: 6px;
margin-top: -3px;
margin-left: -3px;
}
.loading.la-2x {
width: 52px;
height: 52px;
}
.loading.la-2x > div {
width: 24px;
height: 24px;
margin-top: -12px;
margin-left: -12px;
}
.loading.la-3x {
width: 78px;
height: 78px;
}
.loading.la-3x > div {
width: 36px;
height: 36px;
margin-top: -18px;
margin-left: -18px;
}
@keyframes ball-square-spin {
0%,
40%,
100% {
transform: scale(0.4);
}
70% {
transform: scale(1);
}
}
</style>