「HTML+CSS」自定義加載動(dòng)畫【020】
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: #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-radius: 50%;
display: inline-block;
position: relative;
border-color: linear-gradient(0deg, red 33%, #2376b7 100%);;
background: linear-gradient(0deg, red 33%, #2376b7 100%);
animation: rotation 1s linear infinite;
}
span::before{
content: '';
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50% , -50%);
width : 88px;
height: 88px;
border-radius: 50%;
background: #263238;
}
@keyframes rotation {
0% { transform: rotate(0deg) }
100% { transform: rotate(360deg) }
}
Part3原理詳解
步驟1
設(shè)置span標(biāo)簽
寬度、高度均為96px 背景色為漸變色:linear-gradient(0deg, red 33%, #2376b7 100%);
width : 96px;
height: 96px;
background: linear-gradient(0deg, red 33%, #2376b7 100%);
效果圖如下

步驟2
設(shè)置span::before
寬度、高度均為88px 背景色為#263238,與全局背景色保持一致 位于span正中間(上下左右居中) 絕對(duì)定位
content: '';
/*使用絕對(duì)定位 */
position: absolute;
/*左上頂點(diǎn)左移50% 下移50%*/
left: 50%;
top: 50%;
/*向左移動(dòng)自身50%寬度距離 再向上移自身高度50%的距離*/
/* 這樣就可以使span::before位于span正中間了*/
transform: translate(-50% , -50%);
width : 88px;
height: 88px;
background-color: #263238;
效果圖如下

注:這里用黃顏色作為了span::before的背景色是為了便于演示,實(shí)際色與全局背景色保持一致
理論效果如下

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

注:這里用黃顏色作為了span::before的背景色是為了便于演示,實(shí)際色與全局背景色保持一致
理論效果如下

步驟4
為span添加動(dòng)畫
順時(shí)針旋轉(zhuǎn) 2s 無限循環(huán)
animation: rotation 1s linear infinite;
@keyframes rotation {
0% { transform: rotate(0deg) }
100% { transform: rotate(360deg) }
}
效果圖如下

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

評(píng)論
圖片
表情
