【動畫消消樂】HTML+CSS 自定義加載動畫 059
效果展示

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: #ed556a;
}
section {
width: 650px;
height: 300px;
padding: 10px;
position: relative;
display: flex;
align-items: center;
justify-content: center;
border: 2px solid white;
}
span {
width: 64px;
height: 64px;
display: inline-block;
position: relative;
background: white;
animation: loading 2s ease infinite alternate-reverse;
}
@keyframes loading {
0% {
transform: scale(1) rotate(0deg)
}
100% {
transform: scale(.1) rotate(360deg)
}
}
原理詳解
步驟1
使用span標簽,設置為
寬度、高度均為64px 相對定位 背景色:白色
span {
width: 64px;
height: 64px;
position: relative;
background: white;
}
效果圖如下

步驟2
為span添加動畫
初始(0%):大小為1(相對原圖像)旋轉(zhuǎn)0度 末尾(10%):大小為.1(相對原圖像) 旋轉(zhuǎn)360度
動畫本質(zhì)上是兩個變化的疊加
大小從1變?yōu)?1(相對于原大?。?/section> 旋轉(zhuǎn)角度從0到360度
animation: loading 2s ease infinite ;
@keyframes loading {
0% {
transform: scale(1) rotate(0deg)
}
100% {
transform: scale(.1) rotate(360deg)
}
}
效果圖如下

步驟3
動畫設置為alternate-reverse
?alternate-reverse :動畫在奇數(shù)次(1、3、5...)反向播放,在偶數(shù)次(2、4、6...)正向播放。
animation: loading 2s ease infinite alternate-reverse;
效果圖如下

結(jié)語
希望對您有所幫助
如有錯誤歡迎小伙伴指正~
我是 海轟?(?ˊ?ˋ)?
如果您覺得寫得可以的話
請點個贊吧
謝謝支持??
評論
圖片
表情
