動畫能讓網頁更有互動感,提升使用者體驗。常見應用:
transition
可以讓屬性在變化時「平滑過渡」。
transition: [property] [duration] [timing-function] [delay];
color
、background
、all
)0.3s
、1s
)ease
(預設,慢-快-慢)linear
(均速)ease-in
(慢開始)ease-out
(慢結束)cubic-bezier()
(自定義)<button class="btn">Hover Me</button>
.btn {
background: #4cafef;
color: white;
padding: 12px 24px;
border: none;
border-radius: 6px;
cursor: pointer;
transition: background 0.3s ease, transform 0.2s ease;
}
.btn:hover {
background: #357ab7;
transform: scale(1.05);
}
@keyframes
可以定義一個動畫,並透過 animation
屬性呼叫。
@keyframes animationName {
0% { ... }
50% { ... }
100% { ... }
}
.selector {
animation: animationName duration timing-function delay iteration-count direction fill-mode;
}
<div class="fade">Hello World</div>
.fade {
opacity: 0;
animation: fadeIn 2s ease forwards;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
<div class="card">I am a card</div>
.card {
width: 200px;
height: 120px;
background: #6a5acd;
color: white;
display: flex;
justify-content: center;
align-items: center;
border-radius: 8px;
transform: translateY(50px);
opacity: 0;
animation: slideUp 1s ease-out forwards;
}
@keyframes slideUp {
0% { transform: translateY(50px); opacity: 0; }
100% { transform: translateY(0); opacity: 1; }
}
👉 明天(Day 11),我們會進入JavaScript 入門:變數與資料型態,開始為網頁加上互動邏輯。