【動(dòng)畫(huà)消消樂(lè)】HTML+CSS 自定義加載動(dòng)畫(huà) 070
Part1效果展示

Part2Demo代碼
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">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<section><span></span></section>
</body>
</html>
CSS
html, body {
margin: 0;
height: 100%;
}
body {
display: flex;
justify-content: center;
align-items: center;
background: #ed556a;
/* background-color: #82466e; */
animation: backColor 4s infinite;
}
section {
width: 650px;
height: 300px;
padding: 10px;
position: relative;
display: flex;
align-items: center;
justify-content: center;
border: 2px solid white;
}
span {
width: 5px;
height: 5px;
display: inline-block;
position: relative;
border-radius: 4px;
color: white;
background: currentColor;
animation: animloader66 0.6s 0.6s linear infinite alternate;
}
span::before, span::after {
content: '';
width: 5px;
height: 5px;
border-radius: 4px;
background: currentColor;
position: absolute;
left: 0;
top: 15px;
animation: animloader66 0.6s 0.9s linear infinite alternate;
}
[data-index="66"] {
justify-content: normal;
padding-left: 15%;
}
span::after {
top: -15px;
animation-delay: 0s;
}
@keyframes animloader66 {
0% {
width: 5px;
}
100% {
width: 48px;
}
}
Part3原理詳解
步驟1
使用span標(biāo)簽,設(shè)置為
相對(duì)定位 寬度、高度均為5px 背景色:白色 color:白色 border-radius: 4px
span {
width: 5px;
height: 5px;
position: relative;
border-radius: 4px;
color: white;
background: white;
}
效果圖如下

步驟2
為span添加動(dòng)畫(huà)
動(dòng)畫(huà)描述效果為:span寬度從5px變化到48px,再回到5px
span {
animation: loading 0.6s 0s linear infinite alternate;
}
@keyframes loading {
0% {
width: 5px;
}
100% {
width: 48px;
}
}
效果圖如下
注:span事先是設(shè)置固定在頁(yè)面正中的,所以才會(huì)出現(xiàn)上圖效果
步驟3
使用span的兩個(gè)偽元素:span::before、span::after
同時(shí)設(shè)置為
絕對(duì)定位( left: 0 top: 15px) 寬度、高度均為5px 背景色:白色 border-radius: 4px
span::before, span::after {
content: '';
width: 5px;
height: 5px;
border-radius: 4px;
background: white;
position: absolute;
left: 0;
top: 15px;
}
位置關(guān)系如下

為span::before、span::after添加同span一樣的動(dòng)畫(huà),只是動(dòng)畫(huà)開(kāi)始延遲時(shí)間設(shè)置為0.6s
span::before, span::after {
animation: loading 0.6s 0.6s linear infinite alternate;
}
效果圖如下:

注:可以發(fā)現(xiàn),span和它的兩個(gè)偽元素的左部分一直是處于同一條直線上

步驟4
分離span::before、span::after
將span::after移動(dòng)至位于span上方15px處 且動(dòng)畫(huà)延遲時(shí)間修改為0.9s
span::after {
top: -15px;
animation-delay: 0.9s;
}
得到最終的效果圖

Part4結(jié)語(yǔ)
希望對(duì)您有所幫助
如有錯(cuò)誤歡迎小伙伴指正~
我是 海轟?(?ˊ?ˋ)?
如果您覺(jué)得寫(xiě)得可以的話
請(qǐng)點(diǎn)個(gè)贊吧
謝謝支持??
評(píng)論
圖片
表情
