純 CSS3 實現極具視覺衝擊力的 Loading 進度條:8 個彩色長條依序旋轉、縮放、淡入淡出,展示 animation、transform、@keyframes、結構性偽類 :nth-child(n) 綜合運用。
<div class="center">
<ul>
<li><div></div></li> × 8
</ul>
</div>
/* 關鍵幀:水平位移 + 淡入淡出 */
@keyframes bump {
0% { opacity: 0; left: 535px; }
10%, 85% { opacity: 1; }
100% { opacity: 0; left: -10px; }
}
/* 關鍵幀:垂直縮放跳躍 */
@keyframes spin {
0%, 100% { height: 20px; top: 50px; }
50% { height: 100px; top: 0; }
}
/* 容器樣式 */
body { background: rgba(0,0,0,0.2); }
.center { text-align: center; margin-top: 40px; }
ul {
position: relative; display: block; margin: auto;
width: 600px; height: 10px;
padding: 0; list-style: none;
border-radius: 200px; border: 5px solid rgba(255,255,255,0.2);
background: rgba(255,255,255,0.4);
box-shadow: 0 0 5px 5px rgba(0,0,0,0.1);
margin-top: 100px;
}
ul li {
position: absolute; margin-top: -55px;
}
/* 8 條長條:各自延遲 + 顏色區分 */
ul li:nth-child(1) { animation: bump 1.5s infinite; animation-delay: 0.1s; }
ul li:nth-child(1) div { animation: spin 0.4s infinite; animation-delay: 0.1s; transform-origin: center; border-radius: 22px; position: absolute; height: 60px; width: 80px; background: rgba(120,120,120,0.3); }
ul li:nth-child(2) { animation: bump 1.5s infinite; animation-delay: 0.2s; }
ul li:nth-child(2) div { animation: spin 0.4s infinite; animation-delay: 0.2s; transform-origin: center; border-radius: 22px; position: absolute; height: 60px; width: 80px; background: rgba(120,0,0,0.3); }
ul li:nth-child(3) { animation: bump 1.5s infinite; animation-delay: 0.3s; }
ul li:nth-child(3) div { animation: spin 0.4s infinite; animation-delay: 0.3s; transform-origin: center; border-radius: 22px; position: absolute; height: 60px; width: 80px; background: rgba(120,120,0,0.3); }
ul li:nth-child(4) { animation: bump 1.5s infinite; animation-delay: 0.4s; }
ul li:nth-child(4) div { animation: spin 0.4s infinite; animation-delay: 0.4s; transform-origin: center; border-radius: 22px; position: absolute; height: 60px; width: 80px; background: rgba(0,120,0,0.3); }
ul li:nth-child(5) { animation: bump 1.5s infinite; animation-delay: 0.5s; }
ul li:nth-child(5) div { animation: spin 0.4s infinite; animation-delay: 0.5s; transform-origin: center; border-radius: 22px; position: absolute; height: 60px; width: 80px; background: rgba(0,120,120,0.3); }
ul li:nth-child(6) { animation: bump 1.5s infinite; animation-delay: 0.6s; }
ul li:nth-child(6) div { animation: spin 0.4s infinite; animation-delay: 0.6s; transform-origin: center; border-radius: 22px; position: absolute; height: 60px; width: 80px; background: rgba(0,0,120,0.3); }
ul li:nth-child(7) { animation: bump 1.5s infinite; animation-delay: 0.7s; }
ul li:nth-child(7) div { animation: spin 0.4s infinite; animation-delay: 0.7s; transform-origin: center; border-radius: 22px; position: absolute; height: 60px; width: 80px; background: rgba(120,0,120,0.3); }
ul li:nth-child(8) { animation: bump 1.5s infinite; animation-delay: 0.8s; }
ul li:nth-child(8) div { animation: spin 0.4s infinite; animation-delay: 0.8s; transform-origin: center; border-radius: 22px; position: absolute; height: 60px; width: 80px; background: rgba(120,120,120,0.3); }
li 命名 class,代碼極度 DRY。bump 控制水平移動/透明度 (容器層);spin 控制垂直縮放/跳躍 (內層 div)。-webkit-、-moz- 前綴 (含 @-webkit-keyframes、-webkit-transform、-webkit-animation)。:root { --bars: 8; --duration: 1.5s; }
ul li {
--i: 0; /* 由 JS 或 inline style 設定 0~7 */
animation: bump var(--duration) infinite;
animation-delay: calc((var(--i) + 1) * 0.1s);
}
ul li div {
animation: spin calc(var(--duration)/3.75) infinite;
animation-delay: calc((var(--i) + 1) * 0.1s);
background: hsl(calc(var(--i) * 45), 60%, 40%, 0.3); /* 自動分配色相 */
}
搭配少量 JS 設定 --i,代碼量減少 80%,維護性大幅提升。