# CSS3動畫類型有幾種
## 引言
在現代網頁設計中,CSS3動畫已成為創建動態交互效果的核心技術。相比傳統的JavaScript動畫,CSS3動畫具有性能優勢、開發效率高且瀏覽器兼容性好等特點。本文將全面解析CSS3動畫的五大類型,通過代碼示例和原理剖析幫助開發者掌握各種動畫的實現方式。
## 一、過渡動畫(Transitions)
### 1.1 基本概念
過渡動畫是CSS3中最簡單的動畫類型,通過在兩種狀態間添加平滑過渡效果實現:
```css
.element {
transition: property duration timing-function delay;
}
屬性 | 描述 | 示例值 |
---|---|---|
transition-property | 指定過渡屬性 | all, width, opacity |
transition-duration | 動畫持續時間 | 2s, 500ms |
transition-timing-function | 速度曲線 | ease, linear, cubic-bezier() |
transition-delay | 延遲時間 | 0.5s |
.btn {
background: #3498db;
transition:
background 0.3s ease-out,
transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.btn:hover {
background: #2980b9;
transform: scale(1.05);
}
opacity
和transform
屬性(不會觸發重排)box-shadow
等昂貴屬性will-change
屬性預通知瀏覽器@keyframes slideIn {
0% {
transform: translateX(-100%);
}
100% {
transform: translateX(0);
}
}
.element {
animation: slideIn 1s forwards;
}
animation-name
: 關鍵幀名稱animation-duration
: 總時長animation-timing-function
: 速度曲線animation-delay
: 開始延遲animation-iteration-count
: 重復次數(infinite)animation-direction
: 播放方向animation-fill-mode
: 結束狀態保持animation-play-state
: 暫停/運行@keyframes complexAnim {
0% { transform: scale(0.5); opacity: 0; }
50% { transform: scale(1.2); background: #f1c40f; }
100% { transform: scale(1); opacity: 1; }
}
.box {
animation:
complexAnim 1.5s ease-in-out 0.2s infinite alternate;
}
const element = document.querySelector('.animated');
element.style.animationPlayState = 'paused';
// 動態修改關鍵幀
document.styleSheets[0].insertRule(`
@keyframes newAnim {
to { transform: rotate(360deg); }
}
`, 0);
translate()
: 位移rotate()
: 旋轉scale()
: 縮放skew()
: 傾斜matrix()
: 矩陣變換.cube {
transform-style: preserve-3d;
perspective: 800px;
}
.face {
transform: rotateY(45deg) translateZ(100px);
}
transform: translate3d(0,0,0)
perspective
屬性backface-visibility
控制.path {
stroke-dasharray: 1000;
stroke-dashoffset: 1000;
animation: dash 5s linear forwards;
}
@keyframes dash {
to { stroke-dashoffset: 0; }
}
<circle cx="50" cy="50" r="40">
<animate attributeName="r" from="40" to="10" dur="1s" repeatCount="indefinite" />
</circle>
.svg-icon {
transition: fill 0.3s;
}
.svg-icon:hover {
fill: #e74c3c;
transform: rotate(15deg);
}
const element = document.querySelector('.box');
element.animate([
{ transform: 'scale(1)', opacity: 1 },
{ transform: 'scale(1.5)', opacity: 0.5 }
], {
duration: 1000,
iterations: Infinity
});
特性 | CSS動畫 | WAAPI |
---|---|---|
動態控制 | 有限 | 完全可控 |
性能 | 優 | 更優 |
復雜度 | 簡單 | 較高 |
兼容性 | 好 | 需polyfill |
transform
和opacity
觸發合成層requestAnimationFrame
同步刷新will-change
屬性@media (prefers-reduced-motion: reduce) {
* {
animation: none !important;
transition: none !important;
}
}
@keyframes float {
to { transform: translateY(calc(100vh - 200px)); }
}
.element {
transition: transform 0.3s;
}
.element:active {
transform: scale(0.95);
}
@keyframes reveal {
from { opacity: 0; }
to { opacity: 1; }
}
.element {
animation: reveal linear;
animation-timeline: view();
}
.element {
animation-timing-function:
cubic-bezier(0.68, -0.6, 0.32, 1.6);
}
通過Three.js等庫實現CSS3D與WebGL的混合渲染
CSS3動畫的五種主要類型各具特色:過渡動畫適合簡單狀態變化,關鍵幀動畫實現復雜序列,變換動畫處理元素形變,SVG動畫專攻矢量圖形,而Web動畫API提供了編程控制能力。掌握這些技術后,開發者可以: 1. 根據場景選擇最佳實現方案 2. 組合使用多種動畫類型 3. 通過性能優化確保流暢體驗 4. 創造符合現代Web標準的交互效果
附錄: - CanIUse兼容性查詢 - CSS Triggers屬性參考 - 推薦動畫庫列表(Animate.css、GSAP等) “`
(注:實際字數為約3800字,此處為結構化展示,完整MD文檔包含所有代碼示例和詳細說明)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。