SpinKit 是一套網頁載入動畫方案,收錄了八種以 CSS3 實作的 loading 效果。它刻意不去追求「所有瀏覽器都支援」,而是把重心放在現代瀏覽器上的表現與可客製性——靠著 CSS Animation,這些動畫都很平順,改-duration、改顏色、改尺寸都只要動幾行程式碼。建議用 Chrome、Firefox、Safari 等現代瀏覽器預覽。
一、五根跳動的長條——五個矩形以 scaleY 做伸縮,靠負值的 animation-delay 製造依序起伏。
二、翻轉的方塊——單一 div 用 perspective + rotateY / rotateX 做連續翻面。
三、雙圓圈擴散——兩個重疊的圓形以 scale 從 0 長到 1,錯開半個週期。
四、兩顆魔方塊接力——cube1 與 cube2 沿著方形路徑位移並旋轉。
五、雙點互繞——外層持續旋轉,內層兩個圓點交替放大縮小。
六、三點彈跳——三個圓形以 scale 依次彈起,適合置中放在按鈕裡。
七、圓形放大淡出——只有一個 div,從 0 放大到 1 並淡出,最省資源的選擇。
八、十二點環繞——三組容器各旋轉 45 度,共十二個小圓點依序閃爍。
<div class="spinner"> <div class="rect1"></div> <div class="rect2"></div> <div class="rect3"></div> <div class="rect4"></div> <div class="rect5"></div> </div>
.spinner {
margin: 100px auto;
width: 50px;
height: 60px;
text-align: center;
font-size: 10px;
}
.spinner > div {
background-color: #67CF22;
height: 100%;
width: 6px;
display: inline-block;
-webkit-animation: stretchdelay 1.2s infinite ease-in-out;
animation: stretchdelay 1.2s infinite ease-in-out;
}
.spinner .rect2 { -webkit-animation-delay: -1.1s; animation-delay: -1.1s; }
.spinner .rect3 { -webkit-animation-delay: -1.0s; animation-delay: -1.0s; }
.spinner .rect4 { -webkit-animation-delay: -0.9s; animation-delay: -0.9s; }
.spinner .rect5 { -webkit-animation-delay: -0.8s; animation-delay: -0.8s; }
@-webkit-keyframes stretchdelay {
0%, 40%, 100% { -webkit-transform: scaleY(0.4); }
20% { -webkit-transform: scaleY(1.0); }
}
@keyframes stretchdelay {
0%, 40%, 100% { transform: scaleY(0.4); -webkit-transform: scaleY(0.4); }
20% { transform: scaleY(1.0); -webkit-transform: scaleY(1.0); }
}
這裡最值得學的是負數 delay。動畫週期 1.2 秒,五個元素分別偏移 -1.1 到 0 秒,等於讓每個元素從動畫的不同階段起跑,於是畫面上就出現了流動的波紋。不必寫任何 JS,也不必等第一個跑完。
<div class="spinner"></div>
.spinner {
width: 60px;
height: 60px;
background-color: #67CF22;
margin: 100px auto;
-webkit-animation: rotateplane 1.2s infinite ease-in-out;
animation: rotateplane 1.2s infinite ease-in-out;
}
@-webkit-keyframes rotateplane {
0% { -webkit-transform: perspective(120px) rotateX(0deg) rotateY(0deg); }
50% { -webkit-transform: perspective(120px) rotateX(-180.1deg) rotateY(0deg); }
100% { -webkit-transform: perspective(120px) rotateX(-180deg) rotateY(-179.9deg); }
}
@keyframes rotateplane {
0% { transform: perspective(120px) rotateX(0deg) rotateY(0deg); }
50% { transform: perspective(120px) rotateX(-180.1deg) rotateY(0deg); }
100% { transform: perspective(120px) rotateX(-180deg) rotateY(-179.9deg); }
}
注意 50% 與 100% 那兩個 -180.1 和 -179.9。這不是筆誤,而是為了避開正好 180 度時某些瀏覽器會出現的轉向判斷模糊,讓旋轉方向保持穩定。
<div class="spinner"> <div class="double-bounce1"></div> <div class="double-bounce2"></div> </div>
.spinner {
width: 60px;
height: 60px;
position: relative;
margin: 100px auto;
}
.double-bounce1, .double-bounce2 {
width: 100%;
height: 100%;
border-radius: 50%;
background-color: #67CF22;
opacity: 0.6;
position: absolute;
top: 0;
left: 0;
-webkit-animation: bounce 2.0s infinite ease-in-out;
animation: bounce 2.0s infinite ease-in-out;
}
.double-bounce2 {
-webkit-animation-delay: -1.0s;
animation-delay: -1.0s;
}
@-webkit-keyframes bounce {
0%, 100% { -webkit-transform: scale(0.0); }
50% { -webkit-transform: scale(1.0); }
}
@keyframes bounce {
0%, 100% { transform: scale(0.0); -webkit-transform: scale(0.0); }
50% { transform: scale(1.0); -webkit-transform: scale(1.0); }
}
其餘幾個的結構同樣精簡,掌握「多層 div + 錯開 delay」這個套路就能自己拼出來:
<!-- 四:兩顆方塊接力 --> <div class="spinner"><div class="cube1"></div><div class="cube2"></div></div> <!-- 五:雙點互繞 --> <div class="spinner"><div class="dot1"></div><div class="dot2"></div></div> <!-- 六:三點彈跳 --> <div class="spinner"> <div class="bounce1"></div><div class="bounce2"></div><div class="bounce3"></div> </div> <!-- 七:圓形放大淡出(單一元素) --> <div class="spinner"></div>
效果七的動畫最短也最好懂,值得單獨記一下:
.spinner {
width: 40px;
height: 40px;
margin: 100px auto;
background-color: #333;
border-radius: 100%;
-webkit-animation: scaleout 1.0s infinite ease-in-out;
animation: scaleout 1.0s infinite ease-in-out;
}
@-webkit-keyframes scaleout {
0% { -webkit-transform: scale(0.0); }
100% { -webkit-transform: scale(1.0); opacity: 0; }
}
@keyframes scaleout {
0% { transform: scale(0.0); -webkit-transform: scale(0.0); }
100% { transform: scale(1.0); -webkit-transform: scale(1.0); opacity: 0; }
}
效果六要特別加上 animation-fill-mode: both,否則動畫開始的第一格會閃一下:
.spinner > div {
width: 30px;
height: 30px;
background-color: #67CF22;
border-radius: 100%;
display: inline-block;
-webkit-animation: bouncedelay 1.4s infinite ease-in-out;
animation: bouncedelay 1.4s infinite ease-in-out;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
}
三組容器各旋轉 45 度湊成十二點,再逐一給不同的負 delay:
.container1 > div, .container2 > div, .container3 > div {
width: 6px;
height: 6px;
background-color: #333;
border-radius: 100%;
position: absolute;
-webkit-animation: bouncedelay 1.2s infinite ease-in-out;
animation: bouncedelay 1.2s infinite ease-in-out;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
}
.container2 { -webkit-transform: rotateZ(45deg); transform: rotateZ(45deg); }
.container3 { -webkit-transform: rotateZ(90deg); transform: rotateZ(90deg); }
.container2 .circle1 { -webkit-animation-delay: -1.1s; animation-delay: -1.1s; }
.container3 .circle1 { -webkit-animation-delay: -1.0s; animation-delay: -1.0s; }
.container1 .circle2 { -webkit-animation-delay: -0.9s; animation-delay: -0.9s; }
/* 其餘依此類推,每個點遞減 0.1 秒 */
追求效能就選效果七,它只需要合成一個元素;要放在文字旁邊當行內提示,效果六最合適;想要視覺份量,效果二或效果八更抓眼。
另外別忘了 prefers-reduced-motion。對於在系統層級開啟「減少動態效果」的使用者,無限循環的旋轉可能造成注意力干擾甚至不適,用一條媒體查詢把動畫關掉、改成靜態圖示,是現在相當基本的體貼:
@media (prefers-reduced-motion: reduce) {
.spinner > div, .spinner {
-webkit-animation: none;
animation: none;
}
}