萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS3 打造閃爍跳躍進度條:animation + transform + nth-child 完整實作

純 CSS3 實現極具視覺衝擊力的 Loading 進度條:8 個彩色長條依序旋轉、縮放、淡入淡出,展示 animation、transform、@keyframes、結構性偽類 :nth-child(n) 綜合運用。

HTML 結構 (極簡)

<div class="center">
  <ul>
    <li><div></div></li> × 8
  </ul>
</div>

核心 CSS (已加入標準語法,生產需補 -webkit- 前綴)

/* 關鍵幀:水平位移 + 淡入淡出 */
@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); }

關鍵技術點解析

  1. :nth-child(n):結構性偽類選擇第 n 個子元素,避免為每個 li 命名 class,代碼極度 DRY。
  2. 雙動畫分離:bump 控制水平移動/透明度 (容器層);spin 控制垂直縮放/跳躍 (內層 div)。
  3. animation-delay 遞增:0.1s 間隔產生波浪式依序啟動效果。
  4. transform-origin: center:縮放中心點固定,避免抖動。
  5. rgba 半透明色彩:每條長條不同色相,疊加形成豐富視覺層次。

瀏覽器支援與前綴

  • IE10+、Firefox 16+、Chrome 43+、Safari 9+、Edge 12+ 完整支援無前綴。
  • 舊版需加 -webkit-、-moz- 前綴 (含 @-webkit-keyframes、-webkit-transform、-webkit-animation)。
  • IE9 及以下**不支援**,需 JS Polyfill 或優雅降級 (靜態圖片)。

現代簡化重構 (CSS Variables + 單一關鍵幀)

: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%,維護性大幅提升。

copyright © 萬盛學電腦網 all rights reserved