【動畫消消樂】HTML+CSS 自定義加載動畫 054
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;
??display:?inline-block;
??position:?relative;
}
span::before,?span::after?{
??content:?'';
??width:?96px;
??height:?96px;
??border:?6px?solid?white;
??position:?absolute;
??left:?0;
??top:?0;
??animation:?scaleOut?4s?ease-in-out?infinite;
}
span::after?{
??border-color:?red;
??animation-delay:?2s;
}
@keyframes?scaleOut?{
??0%?{
????transform:?scale(0);
??}
??100%?{
????transform:?scale(1)
??}
}
Part3原理詳解步驟1
使用span標簽,設置
- 寬度、高度均為96px
- 相對定位
width: 96px;
height: 96px;
position: relative;
步驟2
利用span::before和span::after,充當白色、紅色方框
同時設置
- 絕對定位( ?left: 0 top: 0)
- 寬度、高度均為96px
- 邊框:6px solid white
content: '';
width: 96px;
height: 96px;
border: 6px solid white;
position: absolute;
left: 0;
top: 0;
效果圖如下

步驟3
為區(qū)分before和after
設置span::after背景色為紅色
span::after {
border-color: red;
}
效果圖如下

注:span::before和after位置發(fā)生了重疊,圖中其實紅色與白色方塊位于同一位置,只是after后面設置,最后顯示為紅色了
步驟4
為span::before和span::after添加動畫
- 大小:初始(0%)大小為0(相對于原大小),最后變?yōu)?(相對于原大小)
animation: loading 4s ease-in-out infinite;
@keyframes loading {
0% {
transform: scale(0);
}
100% {
transform: scale(1)
}
}
效果圖如下

步驟5
步驟4所設置的動畫是為before和after同時設置的,二者的變化過程完全一致
為了視覺上分離before和after
我們對after動畫開始時間延遲(這樣before和after就可以分開顯示了)
span::after {
animation-delay: 2s;
}
效果圖如下
Part4結語希望對您有所幫助
如有錯誤歡迎小伙伴指正~
我是 海轟?(?ˊ?ˋ)?
如果您覺得寫得可以的話
請點個贊吧
謝謝支持??
評論
圖片
表情
