CSS Keyframes — Web Animation

CSS Keyframes CSS Keyframes memungkinkan kamu membuat animasi multi-step yang lebih kompleks dari transition. Dengan keyframes, kamu bisa mendefinisikan state a

CSS Keyframes

CSS Keyframes memungkinkan kamu membuat animasi multi-step yang lebih kompleks dari transition. Dengan keyframes, kamu bisa mendefinisikan state animasi di beberapa titik waktu.

Syntax @keyframes

/* Definisi animasi */
@keyframes namaAnimasi {
  from {
    /* state awal (sama dengan 0%) */
    opacity: 0;
  }
  to {
    /* state akhir (sama dengan 100%) */
    opacity: 1;
  }
}

/* Terapkan ke elemen */
.element {
  animation: namaAnimasi 1s ease-out;
}

Properti Animation

Ada 8 sub-properti animation:

.element {
  animation-name: bounce;           /* nama keyframes */
  animation-duration: 0.5s;         /* durasi */
  animation-timing-function: ease;  /* kurva kecepatan */
  animation-delay: 0s;              /* jeda sebelum mulai */
  animation-iteration-count: 1;     /* jumlah pengulangan */
  animation-direction: normal;      /* arah animasi */
  animation-fill-mode: forwards;    /* state setelah selesai */
  animation-play-state: running;    /* running atau paused */
}

/* Shorthand */
.element {
  animation: bounce 0.5s ease 0s 1 normal forwards running;
}

Iteration Count

/* Ulangi 3 kali */
.pulse {
  animation: pulse 1s ease-in-out 3;
}

/* Ulangi terus-menerus */
.spinner {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

Direction

/* normal — 0% → 100% (default) */
animation-direction: normal;

/* reverse — 100% → 0% */
animation-direction: reverse;

/* alternate — 0% → 100% → 0% → 100% ... */
animation-direction: alternate;

/* alternate-reverse — 100% → 0% → 100% → 0% ... */
animation-direction: alternate-reverse;

Fill Mode

Fill mode menentukan style elemen sebelum dan sesudah animasi:

/* none — kembali ke state awal setelah animasi selesai */
animation-fill-mode: none;

/* forwards — tetap di state akhir */
animation-fill-mode: forwards;

/* backwards — langsung apply state awal saat delay */
animation-fill-mode: backwards;

/* both — kombinasi forwards + backwards */
animation-fill-mode: both;

Multi-step Animation

Gunakan persentase untuk membuat animasi dengan banyak langkah:

@keyframes bounceIn {
  0% {
    opacity: 0;
    transform: scale(0.3);
  }
  50% {
    opacity: 1;
    transform: scale(1.05);
  }
  70% {
    transform: scale(0.95);
  }
  100% {
    transform: scale(1);
  }
}

.modal {
  animation: bounceIn 0.5s ease-out both;
}

Contoh: Loading Skeleton

@keyframes shimmer {
  0% {
    background-position: -200% 0;
  }
  100% {
    background-position: 200% 0;
  }
}

.skeleton {
  background: linear-gradient(
    90deg,
    #f0f0f0 25%,
    #e0e0e0 50%,
    #f0f0f0 75%
  );
  background-size: 200% 100%;
  animation: shimmer 1.5s ease-in-out infinite;
  border-radius: 4px;
}

Contoh: Typing Effect

@keyframes typing {
  from { width: 0; }
  to { width: 20ch; }
}

@keyframes blink {
  50% { border-color: transparent; }
}

.typewriter {
  font-family: monospace;
  overflow: hidden;
  white-space: nowrap;
  border-right: 3px solid #333;
  width: 0;
  animation:
    typing 3s steps(20) forwards,
    blink 0.7s step-end infinite;
}

CSS Keyframes memberi kamu kontrol penuh atas setiap tahap animasi. Di pelajaran berikutnya, kita akan mempelajari easing dan timing functions yang membuat animasi terasa lebih natural.

Yang akan kamu pelajari