@keyframes slide-color {
0% { margin-left: 100px; background: green; }
40% { margin-left: 150px; background: orange; }
60% { margin-left: 75px; background: blue; }
100% { margin-left: 100px; background: red; }
}
/* 等價 from/to */
@keyframes slide-color {
from { margin-left: 100px; background: green; }
40% { margin-left: 150px; background: orange; }
60% { margin-left: 75px; background: blue; }
to { margin-left: 100px; background: red; }
}
規則:百分比必須帶 %,from=0%,to=100%,順序不限,瀏覽器自動排序。
| 屬性 | 說明 | 常用值 |
|---|---|---|
| animation-name | 關鍵幀名稱 | slide-color / none |
| animation-duration | 一週期時長 | 2s、2000ms |
| animation-timing-function | 速度曲線 | ease、linear、ease-in-out、cubic-bezier() |
| animation-delay | 延遲啟動 | 1s、-500ms(負值立即進入進行中) |
| animation-iteration-count | 播放次數 | 3、infinite |
| animation-direction | 播放方向 | normal、reverse、alternate、alternate-reverse |
| animation-fill-mode | 動畫前後狀態 | none、forwards、backwards、both |
.demo {
width: 50px; height: 50px;
background: blue;
animation: slide-color 10s ease-in-out 2s infinite alternate both;
/* name duration timing delay count direction fill-mode */
}
fill-mode 決定保留或還原。transform、opacity、filter(合成器線程處理,60fps 流暢)。will-change: transform, opacity;。@media (prefers-reduced-motion: reduce) { * { animation: none !important; } }。現代瀏覽器全支援無前綴。IE10/11 需 -ms- 前綴(Autoprefixer 自動處理)。