CSS Transitions
CSS Transition memungkinkan kamu menganimasi perubahan properti CSS secara halus. Alih-alih perubahan terjadi instan, transition membuat perubahan terjadi secara gradual dalam durasi tertentu.
Properti Transition
Ada 4 sub-properti transition:
.element {
/* transition-property — properti apa yang dianimasi */
transition-property: background-color;
/* transition-duration — berapa lama animasinya */
transition-duration: 0.3s;
/* transition-timing-function — kurva kecepatannya */
transition-timing-function: ease;
/* transition-delay — jeda sebelum animasi mulai */
transition-delay: 0s;
}
Shorthand Transition
Keempat properti bisa digabung dalam satu baris:
/* transition: property duration timing-function delay */
.button {
background: #3b82f6;
color: white;
padding: 12px 24px;
border-radius: 8px;
transition: background 0.3s ease;
}
.button:hover {
background: #1d4ed8;
}
Transition pada Hover & Focus
Transition paling sering digunakan untuk efek hover dan focus:
/* Tombol dengan efek hover */
.btn-primary {
background: #10b981;
transform: translateY(0);
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
transition: all 0.2s ease;
}
.btn-primary:hover {
background: #059669;
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
.btn-primary:active {
transform: translateY(0);
}
/* Input dengan efek focus */
.input-field {
border: 2px solid #e5e7eb;
outline: none;
transition: border-color 0.2s ease, box-shadow 0.2s ease;
}
.input-field:focus {
border-color: #3b82f6;
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}
Multiple Properties
Kamu bisa menganimasi beberapa properti sekaligus dengan koma:
.card {
background: white;
transform: scale(1);
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
/* Animasi 3 properti dengan timing berbeda */
transition:
transform 0.2s ease-out,
box-shadow 0.3s ease,
background 0.2s ease;
}
.card:hover {
transform: scale(1.02);
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
background: #f8fafc;
}
Properti yang Bisa Di-transition
Tidak semua properti CSS bisa dianimasi. Properti yang bisa di-transition adalah yang punya nilai numerik atau nilai warna:
| Bisa Dianimasi | Tidak Bisa |
|---|---|
opacity |
display |
transform |
visibility (snap) |
background-color |
font-family |
width, height |
position |
margin, padding |
float |
border-color |
content |
box-shadow |
cursor |
transition: all — Hati-hati!
transition: all 0.3s ease memang mudah, tapi bisa menyebabkan animasi yang tidak diinginkan dan masalah performa:
/* Kurang optimal */
.element {
transition: all 0.3s ease;
}
/* Lebih baik — hanya animasi yang dibutuhkan */
.element {
transition: transform 0.3s ease, opacity 0.3s ease;
}
Contoh Praktis: Navigation Link
.nav-link {
color: #64748b;
text-decoration: none;
padding-bottom: 2px;
border-bottom: 2px solid transparent;
transition: color 0.2s ease, border-color 0.2s ease;
}
.nav-link:hover {
color: #0f172a;
border-bottom-color: #3b82f6;
}
Transition sangat powerful untuk micro-interactions. Di pelajaran berikutnya, kita akan belajar CSS Keyframes untuk animasi multi-step yang lebih kompleks.