效果展示
在這里插入圖片描述
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標(biāo)簽淹接,設(shè)置為
- 寬度鲸鹦、高度均為96px
- 相對(duì)定位
width: 96px;
height: 96px;
position: relative;
步驟2
利用span::before忍疾、span::after偽類(lèi)元素作為白色圓圈部分
設(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%;
效果圖如下
在這里插入圖片描述
步驟4
為span::before、span::after添加動(dòng)畫(huà)
有三種狀態(tài)
- 初始(0%):大小為0(相對(duì)于原大信謇獭)绞幌,顏色為(白,透明級(jí)別1)
- 中間(50%):大小為1(相對(duì)于原大幸怀馈)莲蜘,顏色為(白,透明級(jí)別0)
- 末尾(100%):大小為0(相對(duì)于原大辛庇)票渠,顏色為(白,透明級(jí)別1)
變換過(guò)程
- 大蟹移:0-->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效果圖可以看出
只有一個(gè)白色圓圈
而其實(shí)我們是設(shè)置了兩個(gè)(一個(gè)before,一個(gè)after)
為了視覺(jué)上顯示兩個(gè)白色圓圈
我們對(duì)after進(jìn)行動(dòng)畫(huà)延時(shí)
這樣同一時(shí)刻就可以看到兩個(gè)白色圓圈啦
animation-delay: 2s;
效果圖如下
在這里插入圖片描述