这段代码创建了一个动态的加载动画,通过 CSS 技术实现了方块的移动和透明度变化效果,为页面添加了视觉吸引力和动态感。
大家复制代码时,可能会因格式转换出现错乱,导致样式失效。建议先少量复制代码进行测试,若未能解决问题,私信我,我会发送完整的压缩包给你
演示效果
HTML&CSS
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>公众号关注:前端Hardy</title>
<style>
body {
margin: 0;
padding: 0;
background: #e8e8e8;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.loader {
scale: 3;
height: 50px;
width: 40px;
}
.box {
position: relative;
opacity: 0;
left: 10px;
}
.side-left {
position: absolute;
background-color: #286cb5;
width: 19px;
height: 5px;
transform: skew(0deg, -25deg);
top: 14px;
left: 10px;
}
.side-right {
position: absolute;
background-color: #2f85e0;
width: 19px;
height: 5px;
transform: skew(0deg, 25deg);
top: 14px;
left: -9px;
}
.side-top {
position: absolute;
background-color: #5fa8f5;
width: 20px;
height: 20px;
rotate: 45deg;
transform: skew(-20deg, -20deg);
}
.box-1 {
animation: from-left 4s infinite;
}
.box-2 {
animation: from-right 4s infinite;
animation-delay: 1s;
}
.box-3 {
animation: from-left 4s infinite;
animation-delay: 2s;
}
.box-4 {
animation: from-right 4s infinite;
animation-delay: 3s;
}
@keyframes from-left {
0% {
z-index: 20;
opacity: 0;
translate: -20px -6px;
}
20% {
z-index: 10;
opacity: 1;
translate: 0px 0px;
}
40% {
z-index: 9;
translate: 0px 4px;
}
60% {
z-index: 8;
translate: 0px 8px;
}
80% {
z-index: 7;
opacity: 1;
translate: 0px 12px;
}
100% {
z-index: 5;
translate: 0px 30px;
opacity: 0;
}
}
@keyframes from-right {
0% {
z-index: 20;
opacity: 0;
translate: 20px -6px;
}
20% {
z-index: 10;
opacity: 1;
translate: 0px 0px;
}
40% {
z-index: 9;
translate: 0px 4px;
}
60% {
z-index: 8;
translate: 0px 8px;
}
80% {
z-index: 7;
opacity: 1;
translate: 0px 12px;
}
100% {
z-index: 5;
translate: 0px 30px;
opacity: 0;
}
}
</style>
</head>
<body>
<div class="loader">
<div class="box box-1">
<div class="side-left"></div>
<div class="side-right"></div>
<div class="side-top"></div>
</div>
<div class="box box-2">
<div class="side-left"></div>
<div class="side-right"></div>
<div class="side-top"></div>
</div>
<div class="box box-3">
<div class="side-left"></div>
<div class="side-right"></div>
<div class="side-top"></div>
</div>
<div class="box box-4">
<div class="side-left"></div>
<div class="side-right"></div>
<div class="side-top"></div>
</div>
</div>
</body>
</html>
HTML 结构
- loader: 创建一个类名为“loader”的 div 元素,用于包含整个加载动画。
- box box-1, box box-2, box box-3, box box-4: 创建四个小方块,每个方块包含三个部分:
- side-left: 左侧部分。
- side-right: 右侧部分。
- side-top: 顶部部分。
CSS 样式
- body: 设置页面的边距、填充、背景色、显示方式和高度。
- .loader: 设置加载动画容器的样式,包括尺寸和缩放比例。
- .box: 设置小方块的样式,包括位置、透明度和初始偏移。
- .side-left, .side-right, .side-top: 设置方块的各个部分的样式,包括尺寸、背景色、位置和变形效果。
- .box-1, .box-2, .box-3, .box-4: 分别设置四个方块的动画效果和延迟时间。
- @keyframes from-left: 定义从左侧进入的动画效果,包括透明度变化和位置移动。
- @keyframes from-right: 定义从右侧进入的动画效果,包括透明度变化和位置移动。