「HTML+CSS」自定義加載動畫【031】
效果展示

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 4s ease-in-out infinite;
}
span::after{
animation-delay: 2s;
}
@keyframes animloader {
0% , 100%{ transform: scale(0); opacity: 1;}
50% { transform: scale(1); opacity: 0;}
}
原理詳解
步驟1
使用span標簽,設置為
寬度、高度均為96px 相對定位
width: 96px;
height: 96px;
position: relative;
步驟2
利用span::before、span::after偽類元素作為白色圓圈部分
設置為
絕對定位(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%;
效果圖如下

步驟4
為span::before、span::after添加動畫
有三種狀態(tài)
初始(0%):大小為0(相對于原大?。?,顏色為(白,透明級別1) 中間(50%):大小為1(相對于原大?。?,顏色為(白,透明級別0) 末尾(100%):大小為0(相對于原大?。伾珵椋ò?,透明級別1)
變換過程
大?。?-->1-->0 顏色:白色逐漸變淺,再逐漸變深
animation: animloader 4s ease-in-out infinite;
@keyframes animloader {
0%, 100% {
transform: scale(0);
opacity: 1;
}
50% {
transform: scale(1);
opacity: 0;
}
}
效果圖如下

步驟5
從步驟4效果圖可以看出
只有一個白色圓圈
而其實我們是設置了兩個(一個before,一個after)
為了視覺上顯示兩個白色圓圈
我們對after進行動畫延時
這樣同一時刻就可以看到兩個白色圓圈啦
animation-delay: 2s;
效果圖如下

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