這次要分享一組簡約但有質感的滑鼠移入動畫。核心原理只有一句:滑過圖示或觸發其偽元素時,用 CSS 的 transition 與 animation 產生過渡與循環效果。

CSS 偽元素一直是省標籤的好幫手,本站先前也介紹過動畫條紋邊框、偽元素的多重堆疊、時尚焦點圖相框等應用,這次的圖示動畫同樣建立在這套基礎上。
結構極簡,一個大容器裡放幾個文字項,其餘全靠樣式變化:
<div class="hi-icon-effect-6"> <a href="#" class="hi-icon icon-mobile">Mobile</a> <a href="#" class="hi-icon icon-desktop">Desktop</a> <a href="#" class="hi-icon icon-partners">Partners</a> <a href="#" class="hi-icon icon-support">Support</a> <a href="#" class="hi-icon icon-security">Security</a> </div>
.hi-icon-effect-6 .hi-icon {
box-shadow: 0 0 0 4px rgba(255,255,255,1);
transition: background 0.2s, color 0.2s;
}
.no-touch .hi-icon-effect-6 .hi-icon:hover {
background: rgba(255,255,255,1);
color: #64bb5d;
}
.no-touch .hi-icon-effect-6 .hi-icon:hover:before {
animation: spinAround 2s linear infinite;
}
@keyframes spinAround {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
讀法很直觀:滑鼠移入時背景轉白、文字轉綠,同時 :before 偽元素啟動名為 spinAround 的動畫,兩秒一圈、等速、無限循環。transition 負責 0.2 秒的顏色過渡,animation 負責持續旋轉,兩者分工明確。
值得注意的一點是外層的 .no-touch 限定。這是為了在 JS 中偵測裝置是否支援觸控,只有在非觸控環境才套用 hover 效果,避免手機上出現「點一下卡住」的尷尬狀態。
其他幾個範例的寫法類似,只是換成縮放、抖動、光暈等關鍵影格。掌握「transition 管過渡、animation 管循環」這個分工,剩下的都只是數值調整。
CSS3 動畫其實不難,關鍵在於把屬性初值、終值與節奏寫清楚。剩下的,就交給瀏覽器替你補上中間的每一格。