? Github Star: 19.3k[1]
? 官網(wǎng)[2]
SpinKit 是什么?
SpinKit
是基于 CSS3
的加載動(dòng)畫庫,僅使用 transform
和 opacity
來創(chuàng)建流暢且易用的動(dòng)畫。他利用 CSS3
動(dòng)畫的強(qiáng)大功能,創(chuàng)建了一系列流暢、易于定制的動(dòng)畫效果。接下來,我們將一起討論下 SpinKit
的使用方法和應(yīng)用場景。
SpinKit
的主要特點(diǎn)在于它的簡潔性和易用性。它不僅提供了多種風(fēng)格的動(dòng)畫效果,而每種動(dòng)畫都能通過簡單的 HTML
和 CSS
代碼輕松集成到項(xiàng)目。另外,通過學(xué)習(xí) SpinKit
源碼實(shí)現(xiàn),可以更好學(xué)習(xí)和應(yīng)用 CSS3
。

如上圖所示,SpinKit
提供 12 種動(dòng)畫效果。
快速開始
在項(xiàng)目中使用 SpinKit
是比較方便的,可以通過 npm
安裝也可以直接下載源碼到項(xiàng)目中。
npm install spinkit
引入 spinkit.css
或者 spinkit.min.css
即可。
<div class="example">
<div class="sk-chase">
<div class="sk-chase-dot"></div>
<div class="sk-chase-dot"></div>
<div class="sk-chase-dot"></div>
<div class="sk-chase-dot"></div>
<div class="sk-chase-dot"></div>
<div class="sk-chase-dot"></div>
</div>
</div>
默認(rèn)情況下,加載器的 width
和 height
均是 40px
,background-color
是 #333
。通過覆蓋 --sk-size
調(diào)整寬高 --sk-color
調(diào)整背景色。
:root {
--sk-color: #fff; // 設(shè)置為白色
}
.example {
padding: 10px;
background-color: #1abc9c;
}
效果如下:

實(shí)現(xiàn)原理
SpinKit
追逐點(diǎn)加載動(dòng)畫實(shí)現(xiàn)源碼:
/* 定義追逐動(dòng)畫的容器樣式 */
.sk-chase {
width: 40px;
height: 40px;
position: relative; /* 將元素定位為相對(duì)定位,以便絕對(duì)定位的子元素可以基于此元素定位 */
animation: sk-chase 2.5s infinite linear both; /* 應(yīng)用名為sk-chase的動(dòng)畫,持續(xù)2.5秒,無限循環(huán),線性時(shí)間函數(shù),動(dòng)畫前后狀態(tài)都保留 */
}
/* 定義追逐點(diǎn)的樣式 */
.sk-chase-dot {
width: 100%;
height: 100%;
position: absolute; /* 將元素定位為絕對(duì)定位,相對(duì)于其最近的已定位(非static)祖先元素定位 */
left: 0;
top: 0; /* 將點(diǎn)定位到容器的左上角 */
animation: sk-chase-dot 2.0s infinite ease-in-out both; /* 應(yīng)用名為sk-chase-dot的動(dòng)畫,持續(xù)2秒,無限循環(huán),ease-in-out時(shí)間函數(shù),動(dòng)畫前后狀態(tài)都保留 */
}
/* 定義追逐點(diǎn)的偽元素樣式 */
.sk-chase-dot:before {
content: ''; /* 創(chuàng)建一個(gè)偽元素 */
display: block; /* 使偽元素顯示為塊級(jí)元素 */
width: 25%;
height: 25%;
background-color: #fff; /* 設(shè)置偽元素的背景顏色為白色 */
border-radius: 100%; /* 設(shè)置偽元素的邊框?yàn)閳A形 */
animation: sk-chase-dot-before 2.0s infinite ease-in-out both; /* 應(yīng)用名為sk-chase-dot-before的動(dòng)畫,持續(xù)2秒,無限循環(huán),ease-in-out時(shí)間函數(shù),動(dòng)畫前后狀態(tài)都保留 */
}
/* 為每個(gè)追逐點(diǎn)設(shè)置不同的動(dòng)畫延遲,以創(chuàng)建連續(xù)的動(dòng)畫效果 */
.sk-chase-dot:nth-child(1) { animation-delay: -1.1s; }
.sk-chase-dot:nth-child(2) { animation-delay: -1.0s; }
/* ... 其他點(diǎn)的動(dòng)畫延遲設(shè)置類似,逐漸減少以創(chuàng)建追逐效果 */
/* 定義sk-chase動(dòng)畫的關(guān)鍵幀 */
@keyframes sk-chase {
100% { transform: rotate(360deg); } /* 動(dòng)畫結(jié)束時(shí),元素旋轉(zhuǎn)360度 */
}
/* 定義sk-chase-dot動(dòng)畫的關(guān)鍵幀 */
@keyframes sk-chase-dot {
80%, 100% { transform: rotate(360deg); } /* 動(dòng)畫的80%至100%時(shí)間,元素旋轉(zhuǎn)360度 */
}
/* 定義sk-chase-dot-before動(dòng)畫的關(guān)鍵幀 */
@keyframes sk-chase-dot-before {
50% {
transform: scale(0.4); /* 動(dòng)畫的50%時(shí)間,偽元素縮小到原來的40% */
}
100%, 0% {
transform: scale(1.0); /* 動(dòng)畫的開始和結(jié)束時(shí),偽元素恢復(fù)到原始大小 */
}
}
更多實(shí)現(xiàn)細(xì)節(jié),請(qǐng)參考源碼。
總結(jié)
SpinKit
以其簡潔的設(shè)計(jì)、多樣的動(dòng)畫樣式和易于集成的特點(diǎn),成為優(yōu)化用戶體驗(yàn)的首選加載動(dòng)畫庫。無論是在網(wǎng)頁還是在移動(dòng)應(yīng)用中,SpinKit
都能夠有效地傳達(dá)加載狀態(tài),減少用戶的等待焦慮,增強(qiáng)應(yīng)用的專業(yè)感和吸引力。希望本文的分享對(duì)你有幫助。
祝好!
引用鏈接
[1]
Github Star: 19.3k: https://github.com/tobiasahlin/SpinKit
[2]
官網(wǎng): https://tobiasahlin.com/spinkit/
該文章在 2024/10/12 10:13:18 編輯過