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

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;
border-radius: 50%;
display: inline-block;
position: relative;
border: 10px red;
border-style: dashed solid solid dotted;
border-bottom-color: transparent;
animation: rotation 2s linear infinite ;
}
span::after{
content: '';
position: absolute;
left: 51px;
top: 79px;
border: 20px solid transparent;
border-right-color: red;
transform: rotate(-45deg);
}
@keyframes rotation {
0% { transform: rotate(0deg) }
100% { transform: rotate(360deg)
}
}
原理詳解
步驟1
使用span標簽作為箭尾,設(shè)置為
寬度、高度均為96px 上邊框:10px 紅色 dashed(虛線) 下邊框:10px 透明 solid(實線) 左邊框:10px 紅色 dotted(點狀) 右邊框:10px 紅色 solid(實線)
width : 96px;
height: 96px;
border: 10px red;
border-style: dashed solid solid dotted;
border-bottom-color: transparent;
效果圖如下

步驟2
span標簽圓角化
border-radius: 50%;
效果圖如下

步驟3
使用span::after偽類元素 作為箭頭
設(shè)置為
絕對定位 邊框:20px solid,右邊框為紅色,上/下/左邊框為透明 旋轉(zhuǎn)-45度
position: absolute;
border: 20px solid transparent;
border-right-color: red;
transform: rotate(-45deg);
效果圖如下

步驟4
設(shè)置span::after的位置
注:
這里移動位置時候就需要一點點移動了 確保箭頭和箭尾可以很好的重合 這里肯定是可以用數(shù)學(xué)公式計算出來的,這里稍微需要點時間,之后有空再更新更為一般情況時需要移動的距離公式吧
/*這里箭頭需要移動的距離*/
left: 51px;
top: 79px;
效果圖如下

步驟5
為span添加動畫
順時針旋轉(zhuǎn)(0-360度) 2s 無限循環(huán)
animation: rotation 2s 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的一個過程。
希望對您有所幫助,如有錯誤歡迎小伙伴指正~
我是 海轟?(?ˊ?ˋ)?
如果您覺得寫得可以的話請點個贊吧
謝謝支持??
評論
圖片
表情
