
- CSS 教程
- CSS - 首頁
- CSS - 路線圖
- CSS - 簡介
- CSS - 語法
- CSS - 選擇器
- CSS - 包含
- CSS - 測量單位
- CSS - 顏色
- CSS - 背景
- CSS - 字型
- CSS - 文字
- CSS - 圖片
- CSS - 連結
- CSS - 表格
- CSS - 邊框
- CSS - 塊級邊框
- CSS - 內聯邊框
- CSS - 外邊距
- CSS - 列表
- CSS - 內邊距
- CSS - 游標
- CSS - 輪廓
- CSS - 尺寸
- CSS - 捲軸
- CSS - 內聯塊
- CSS - 下拉選單
- CSS - 可見性
- CSS - 溢位
- CSS - 清除浮動
- CSS - 浮動
- CSS - 箭頭
- CSS - 調整大小
- CSS - 引號
- CSS - 順序
- CSS - 位置
- CSS - 連字元
- CSS - 懸停
- CSS - 顯示
- CSS - 聚焦
- CSS - 縮放
- CSS - 平移
- CSS - 高度
- CSS - 連字元字元
- CSS - 寬度
- CSS - 不透明度
- CSS - Z 索引
- CSS - 底部
- CSS - 導航欄
- CSS - 覆蓋層
- CSS - 表單
- CSS - 對齊
- CSS - 圖示
- CSS - 圖片庫
- CSS - 註釋
- CSS - 載入器
- CSS - 屬性選擇器
- CSS - 組合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 計數器
- CSS - 剪裁
- CSS - 書寫模式
- CSS - Unicode 雙向
- CSS - min-content
- CSS - 所有
- CSS - 內嵌
- CSS - 隔離
- CSS - 滾動溢位
- CSS - Justify Items
- CSS - Justify Self
- CSS - 製表符大小
- CSS - 指標事件
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - 最大塊級尺寸
- CSS - 最小塊級尺寸
- CSS - 混合模式
- CSS - 最大內聯尺寸
- CSS - 最小內聯尺寸
- CSS - 偏移
- CSS - 重音顏色
- CSS - 使用者選擇
- CSS 高階
- CSS - 網格
- CSS - 網格佈局
- CSS - Flexbox
- CSS - 可見性
- CSS - 定位
- CSS - 圖層
- CSS - 偽類
- CSS - 偽元素
- CSS - @規則
- CSS - 文字效果
- CSS - 分頁媒體
- CSS - 列印
- CSS - 佈局
- CSS - 驗證
- CSS - 圖片精靈
- CSS - 重要
- CSS - 資料型別
- CSS3 教程
- CSS3 - 教程
- CSS - 圓角
- CSS - 邊框圖片
- CSS - 多重背景
- CSS - 顏色
- CSS - 漸變
- CSS - 盒陰影
- CSS - 盒裝飾中斷
- CSS - 游標顏色
- CSS - 文字陰影
- CSS - 文字
- CSS - 2D 變換
- CSS - 3D 變換
- CSS - 過渡
- CSS - 動畫
- CSS - 多列
- CSS - 盒尺寸
- CSS - 工具提示
- CSS - 按鈕
- CSS - 分頁
- CSS - 變數
- CSS - 媒體查詢
- CSS - 函式
- CSS - 數學函式
- CSS - 遮罩
- CSS - 形狀
- CSS - 樣式圖片
- CSS - 特異性
- CSS - 自定義屬性
- CSS 響應式
- CSS RWD - 簡介
- CSS RWD - 視口
- CSS RWD - 網格檢視
- CSS RWD - 媒體查詢
- CSS RWD - 圖片
- CSS RWD - 影片
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 轉換器
- CSS - 顏色選擇器和動畫
- CSS 資源
- CSS - 有用資源
- CSS - 討論
CSS - 動畫
CSS 動畫允許在不同樣式之間建立平滑過渡,無需使用 JavaScript。例如,
什麼是 CSS 動畫?
在 CSS 中,我們可以根據時間長度、使用者互動或狀態變化動態更改元素的樣式,這稱為 CSS 動畫。它使用 `@keyframes` 規則來建立動畫,並使用 animation 屬性將其應用於元素。
目錄
@keyframes 規則
`@keyframes` 規則用於定義動畫的關鍵幀,指定動畫元素在動畫的不同階段的外觀。考慮以下定義關鍵幀規則的程式碼。
.box{ animation: colorChange 5s infinite; } @keyframes colorChange { 0% { background-color: red; } 50% { background-color: green; } 100% { background-color: blue; } }
此程式碼將為具有類 .box 的元素定義動畫,動畫名稱為 colorChange,持續 5 秒,並且無限次重複。
關鍵幀規則是為名為 colorChange 的動畫定義的。
- 在動畫總持續時間的 0%(即 0 秒)時,背景顏色將為紅色。
- 在總時間的 50%(即 2.5 秒)時,背景顏色更改為綠色。
- 在總持續時間的 100%(即 5 秒)時,顏色更改為藍色。
動畫延遲屬性
我們可以使用 animation-delay 屬性設定動畫開始的延遲。檢視以下示例
您還可以為 animation-delay 屬性設定負值。如果您使用負值 -n,則動畫將開始,就像它已經播放了 n 秒一樣。
示例
在此示例中,球將在 2 秒後開始向左移動。
<!DOCTYPE html> <html lang="en"> <head> <style> .ball { width: 50px; height: 50px; background-color: #3498db; border-radius: 50%; position: absolute; left: 0; animation-name: moveRight; /* Set duration */ animation-duration: 2s; /* Set delay for animation */ animation-delay: 2s; } @keyframes moveRight { to { left: calc(100% - 50px); } } </style> </head> <body> <div class="container"> <div class="ball"></div> </div> </body> </html>
設定動畫迭代次數
我們可以使用 animation-iteration-count 屬性設定動畫重複的次數。
CSS 規範不支援此屬性的負值。它可以取正整數(例如,1、2、3 等)或關鍵字“infinite”作為值。
示例
在此示例中,我們將球的迭代次數設定為無限。
<!DOCTYPE html> <html lang="en"> <head> <style> .ball { width: 50px; height: 50px; background-color: #3498db; border-radius: 50%; position: absolute; left: 0; animation-name: moveRight; /* Set duration */ animation-duration: 2s; /* Set number of time animation repeats */ animation-iteration-count: infinite; } @keyframes moveRight { to { left: calc(100% - 50px); } } </style> </head> <body> <div class="container"> <div class="ball"></div> </div> </body> </html>
動畫方向屬性
我們可以使用 animation-direction 屬性指定動畫執行的方向。
以下是 animation-direction 屬性的有效值
- normal: 動畫正常播放(向前)。這是預設值。
- reverse: 動畫反向播放(向後)。
- alternate: 動畫先向前播放,然後向後播放。
- alternate-reverse: 動畫先向後播放,然後向前播放。
示例
在此示例中,我們使用內聯 CSS 設定動畫方向屬性。
<!DOCTYPE html> <html lang="en"> <head> <style> .ball { width: 50px; height: 50px; background-color: #3498db; border-radius: 50%; position: relative; left:0; animation-name: moveRight ; animation-duration: 2s; animation-iteration-count: infinite; } @keyframes moveRight { to { left: calc(100% - 50px); } } </style> </head> <body> <h2>animation-direction: normal</h2> <div class="ball" style="animation-direction: normal; "> </div> <h2>animation-direction: reverse</h2> <div class="ball" style="animation-direction: reverse;"> </div> <h2>animation-direction: alternate</h2> <div class="ball" style="animation-direction: alternate;"> </div> <h2>animation-direction: alternate-reverse</h2> <div class="ball" style="animation-direction: alternate-reverse;"> </div> </body> </html>
動畫計時函式
在 CSS 中,animation-timing-function 屬性用於定義動畫的速度曲線。它可以取以下值。
- ease: 動畫將緩慢開始,然後加速,然後緩慢結束(預設值)。
- linear: 從開始到結束速度相同的動畫。
- ease-in: 動畫緩慢開始。
- ease-out: 動畫緩慢結束。
- ease-in-out: 動畫緩慢開始和結束。
- cubic-bezier(n,n,n,n): 這讓我們可以為速度定義自己的值。要了解更多資訊,請檢視 三次貝塞爾曲線函式 文章。
示例
<!DOCTYPE html> <html lang="en"> <head> <style> .ball { width: 50px; height: 50px; background-color: #3498db; border-radius: 50%; position: relative; left:0; animation-name: moveRight ; animation-duration: 2s; animation-iteration-count: infinite; } @keyframes moveRight { to { left: calc(100% - 50px); } } </style> </head> <body> <h2>linear</h2> <div class="ball" style="animation-timing-function: linear;"> </div> <h2>ease</h2> <div class="ball" style="animation-timing-function: ease;"> </div> <h2>ease-in</h2> <div class="ball" style="animation-timing-function: ease-in;"> </div> <h2>ease-out</h2> <div class="ball" style="animation-timing-function: ease-out;"> </div> <h2>ease-in-out</h2> <div class="ball" style="animation-timing-function: ease-in-out;"> </div> <h2>cubic-bezier(0.25, 0.1, 0.25, 1)</h2> <div class="ball" style="animation-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);"> </div> </body> </html>
設定動畫填充模式
animation-fill-mode 屬性指定動畫未播放時(開始之前、結束之後或兩者)目標元素的樣式。
animation-fill-mode 屬性可以具有以下值
- none: 動畫不會在開始之前或結束之後應用任何樣式。這是預設值。
- forwards: 在動畫結束時,元素將保持最後一個關鍵幀規則設定的樣式。
- backwards: 在動畫結束時,元素將保持第一個關鍵幀規則設定的樣式。
- both: 動畫將遵循 forwards 和 backwards 的規則。
檢視以下程式碼的輸出以瞭解更多資訊
示例
<!DOCTYPE html> <html lang="en"> <head> <style> .box { padding: 10px; background-color: green; color: white; font-size: 16px; margin: 20px; animation-duration: 2s; animation-name: changeColor; } /* Animation Definition */ @keyframes changeColor { 0% { background-color: blue; } 100% { background-color: red ; } } /* Animation Fill Modes */ .none { animation-fill-mode: none; } .forwards { animation-fill-mode: forwards; } .backwards { animation-fill-mode: backwards; animation-delay: 2s; } .both { animation-fill-mode: both; animation-delay: 2s; } </style> </head> <body> <div class="box none">None</div> <div class="box forwards">Forwards</div> <div class="box backwards">Backwards</div> <div class="box both">Both</div> </body> </html>
動畫簡寫屬性
在 CSS 中,animation 屬性是以下屬性的簡寫
- animation-name: 設定動畫的名稱。
- animation-duration: 設定動畫的持續時間。
- animation-timing-function: 定義動畫的速度曲線。
- animation-delay: 設定動畫開始之前的延遲。
- animation-iteration-count: 設定動畫重複的次數。
- animation-direction: 定義動畫執行的方向。
- animation-fill-mode: 描述動畫的預執行和後執行樣式。
- animation-play-state: 描述動畫的播放/暫停狀態。
示例
<html lang="en"> <head> <style> .box { padding: 20px; background-color: #3498db; color: white; font-size: 16px; /* Name, duration, timing function, delay, repeat, direction, fill mode */ animation: changeColor 2s ease-in-out 1s infinite alternate both; } /* Animation Definition */ @keyframes changeColor { 0% { background-color: #3498db; } 100% { background-color: #e74c3c; } } </style> </head> <body> <div class="box">Animate Me!</div> </body> </html>
CSS 動畫屬性列表
以下是 animation 屬性的子屬性
屬性 | 描述 | 示例 |
---|---|---|
animation-composition | 指示當多個動畫對同一屬性具有同時影響時要應用的合成操作。 | |
animation-delay | 指示動畫是否應從動畫開始處或途中某個位置開始,以及元素載入與動畫序列開始之間應經過多長時間。 | |
animation-direction | 指示動畫的初始迭代是向前還是向後,以及之後的迭代是否應繼續沿相同方向或每次執行序列時都改變方向。 | |
animation-duration | 指示動畫完成一個迴圈需要多長時間。 | |
animation-fill-mode | 描述動畫應用於其目標的預執行和後執行樣式。 | |
animation-iteration-count | 指示動畫應重複多少次。 | |
animation-name | 它給出描述動畫關鍵幀的 @keyframes at-rule 的名稱。 | |
animation-play-state | 指示動畫序列應播放還是暫停。 | |
animation-timing-function | 描述用於指定動畫中關鍵幀過渡的加速曲線。 |