「HTML+CSS」自定義加載動(dòng)畫【030】
效果展示

Demo代碼
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: #263238;
}
section {
width: 650px;
height: 300px;
padding: 10px;
position: relative;
display: flex;
align-items: center;
justify-content: center;
border: 2px solid red;
}
span {
width : 96px;
height: 96px;
position: relative;
}
span::before,
span::after{
position: absolute;
content: '';
width: 96px;
height: 96px;
top: 0;
left: 0;
background: white;
border-radius: 50%;
animation: animloader 2s linear infinite;
}
span::after{
animation-delay: 1s;
}
@keyframes animloader {
0% { transform: scale(0); opacity: 1;}
100% { transform: scale(1); opacity: 0;}
}
原理詳解
步驟1
使用span標(biāo)簽,設(shè)置
寬度、高度均為96px
width: 96px;
height: 96px;
步驟2
使用span::before、span::after偽類元素充當(dāng)白色波紋
設(shè)置
絕對(duì)定位(top:0 left:0) 寬度、高度均為96px(這樣就是與原span重疊了) 顏色:白色
position: absolute;
content: '';
width: 96px;
height: 96px;
top: 0;
left: 0;
background: white;
效果圖如下

步驟3
span::before、span::after圓角化
border-radius: 50%;
效果圖如下

注:這里span、span::before、span::after三者都重疊在同一個(gè)位置了,其中span無顏色,其余為白色
步驟4
為span::before、span::after添加動(dòng)畫
初始狀態(tài):大小為0(相對(duì)于原大小),顏色為(白,透明級(jí)別1) 終止?fàn)顟B(tài):大小為1(相對(duì)于原大?。?,顏色為(白,透明級(jí)別0)
animation: animloader 2s linear infinite;
/*動(dòng)畫實(shí)現(xiàn)*/
@keyframes animloader {
0% {
transform: scale(0);
opacity: 1;
}
100% {
transform: scale(1);
opacity: 0;
}
}
效果圖如下

步驟5
為了使span::before、span::after二者之間的動(dòng)畫產(chǎn)生視覺差效果
對(duì)span::after進(jìn)行延時(shí)
這樣同一時(shí)刻就可以看到兩個(gè)圓的變換了
animation-delay: 1s;
效果圖如下

結(jié)語
學(xué)習(xí)來源:
?https://codepen.io/bhadupranjal/pen/vYLZYqQ
文章僅作為學(xué)習(xí)筆記,記錄從0到1的一個(gè)過程。
希望對(duì)您有所幫助,如有錯(cuò)誤歡迎小伙伴指正~
我是 海轟?(?ˊ?ˋ)?
如果您覺得寫得可以的話請(qǐng)點(diǎn)個(gè)贊吧
謝謝支持??
評(píng)論
圖片
表情
