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

思路
這里用span元素代表外層白色圓圈
兩個紅色小球分別用span的兩個偽類::before和::after代表
根據(jù)效果圖,可以大概得出思路
先利用span生成一個正方形,設(shè)置好邊框 兩個偽類元素為絕對定位,分別位于正方形的左上和右下 然后分別對其進行圓角處理 最后添加旋轉(zhuǎn)動畫即可
Demo代碼
HTML
<!DOCTYPE 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;
border: 10px solid white;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
position: relative;
animation: rotation 2s linear infinite;
}
span::before{
position: absolute;
content: '';
top: 15px;
left: 15px;
width: 20px;
height: 20px;
background: red;
border-radius: 50%;
}
span::after{
position: absolute;
content: '';
bottom: 15px;
right: 15px;
width: 20px;
height: 20px;
background: red;
border-radius: 50%;
}
@keyframes rotation {
0% { transform: rotate(0deg) }
100% { transform: rotate(360deg)
}
}
原理詳解
步驟1
將span元素設(shè)置為
一個96??96px的正方形 邊框為10px,白色,solid
width : 96px;
height: 96px;
border: 10px solid #fff;
效果圖如下

步驟2
span::before和span::after設(shè)置
寬度、高度均為20px 絕對定位,其中before位于左上,after位于右下 背景色為紅色
/* before的設(shè)置*/
position: absolute;
content: '';
top: 0;
left: 0;
width: 20px;
height: 20px;
background: red;
/*after的設(shè)置*/
position: absolute;
content: '';
bottom: 0;
right: 0;
width: 20px;
height: 20px;
background: red;
效果圖如下

步驟3
稍微向正方形中心移動下::before和::after
/* before的設(shè)置*/
top: 15px;
left: 15px;
/*after的設(shè)置*/
bottom: 15px;
right: 15px;
效果圖如下

步驟4
對span、span::before、span::after設(shè)置圓角
border-radius: 50%;
效果圖如下

步驟5
為span添加動畫
animation: rotation 1s linear infinite;
/*動畫實現(xiàn)*/
@keyframes rotation {
0% { transform: rotate(0deg) }
100% { transform: rotate(360deg)
}
效果圖如下

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

評論
圖片
表情
