CSS3 Animation實現載入動畫
阿新 • • 發佈:2018-12-27
利用CSS3中的animation
,可以實現很多很炫的效果。今天就來利用animation
屬性完成如上圖所示的載入效果。
1 基本構圖
首先來完成基本的構圖:
可以將上述圖形解析為四部分:
- 外部矩形
- 左側紅色矩形
- 右下部黃色矩形
- 右上角白色矩形
劃清圖形結構後,可以完成基本頁面繪製:
<style>
div {
box-sizing: border-box;
}
.logo {
width: 250px;
height: 250px;
margin: 10px auto;
position: relative ;
padding: 4px;
}
.red {
position: absolute;
left: 0;
top: 0;
width: 25%;
height: 100%;
background: red;
border-right: 4px solid black;
}
.yellow {
position: absolute;
left: 25%;
right:0;
bottom: 0;
height: 50%;
background: yellow;
border-top : 4px solid black;
}
.white {
position: absolute;
right:0;
top: 0;
height: 50%;
width: 25%;
background: white;
border-left: 4px solid black;
}
</style>
<body>
<div class="logo">
<div class="red"></div>
<div class="yellow"> </div>
<div class="white"></div>
</div>
</body>
可以看出,外部矩形的邊框並沒有繪製。具體原因暫且按下不表,後面會詳細介紹。
2 動畫分析
首先來看下,這裡的動畫總共分為7部分:
- 四邊邊框出現
- 紅色矩形出現
- 黃色矩形出現
- 白色矩形出現
後面三個矩形出現相對比較容易,難的是四個四邊邊框的動畫效果。由於同側邊框(左右邊框/上線邊框)並不是同步出現,單純靠一個矩形的伸縮無法實現,所以至少要依賴兩個矩形,這時::after
::before
兩個元素正好可以派上用場:
.logo::before, .logo::after {
position: absolute;
width: 100%;
height: 100%;
content: '';
border: 4px solid transparent;
box-sizing: border-box;
}
.logo::before {
z-index: 1; /*before在所有元素最前面,所以會被覆蓋,加上z-index*/
top: 0px;
left: 0px;
animation: border-before 9s infinite;
animation-direction: alternate;
animation-timing-function: linear;
}
.logo::after {
right: 0px;
bottom: 0px;
animation: border-after 9s infinite;
animation-direction: alternate;
animation-timing-function: linear;
}
3 邊框動畫
接下來就可以利用關鍵幀來實現邊框動畫了:
@keyframes border-before {
0% {
width: 0;
height: 0;
border-left-color: black;
}
11% {
height: 100%;
width: 0;
border-bottom-color: transparent; /*在25% - 50% 的過程中,boder-bottom-color變成黑色,預設是在25%-50%過程開始是執行,可以通過step設定*/
}
22%, 100% {
height: 100%;
width: 100%;
border-left-color: black;
border-bottom-color: black;
}
}
@keyframes border-after {
0%, 22% {
width: 0;
height: 0;
border-top-color: transparent;
border-right-color: transparent;
}
33% {
width: 0;
height: 100%;
border-right-color: black;
border-top-color: transparent;
}
44%, 100% {
height: 100%;
width: 100%;
border-top-color: black;
border-right-color: black;
}
}
4 內部矩形動畫
接下來內部矩形的動畫,相對就更簡單了:
.red {
position: absolute;
left: 0;
top: 0;
width: 25%;
height: 100%;
background: red;
border-right: 4px solid black;
animation: redmove 9s infinite;
animation-timing-function: linear;
animation-direction: alternate;
}
@keyframes redmove {
0%,
44% {
width: 0;
opacity: 0;
}
44.01% {
opacity: 1;
}
55%,
100% {
width: 25%;
opacity: 1;
}
}
.yellow {
position: absolute;
left: 25%;
right:0;
bottom: 0;
height: 50%;
background: yellow;
border-top: 4px solid black;
animation: moveyellow 9s infinite;
animation-timing-function: linear;
animation-direction: alternate;
}
@keyframes moveyellow {
0%, 55% {
height: 0;
opacity: 0;
}
55.01% {
opacity: 1;
}
66%, 100% {
height: 50%;
}
}
.white {
position: absolute;
right:0;
top: 0;
height: 50%;
width: 25%;
background: white;
border-left: 4px solid black;
animation: movewhite 9s infinite;
animation-direction: alternate;
animation-timing-function: linear;
}
@keyframes movewhite {
0%, 66% {
width: 0%;
opacity: 0;
}
66.01% {
opacity: 1;
}
77%, 100% {
width: 25%;
}
}