Advanced

Teknik Lanjutan CSS Animation dengan Claude Code

Pelajari tentang teknik lanjutan CSS animation menggunakan Claude Code. Dilengkapi tips praktis dan contoh kode.

Kuasai Teknik Lanjutan CSS Animation

Pelajari cara mengimplementasikan teknik CSS animation praktis yang melampaui transition dan keyframe dasar secara efisien dengan Claude Code.

Animasi Terhubung Scroll

> Implementasikan animasi terhubung scroll hanya dengan CSS.
> Gunakan Scroll-driven Animations API.
/* Progress bar scroll */
.progress-bar {
  position: fixed;
  top: 0;
  left: 0;
  height: 3px;
  background: var(--color-accent);
  transform-origin: left;
  animation: scaleProgress linear;
  animation-timeline: scroll();
}

@keyframes scaleProgress {
  from { transform: scaleX(0); }
  to   { transform: scaleX(1); }
}

/* Animasi kemunculan elemen */
.reveal {
  opacity: 0;
  transform: translateY(30px);
  animation: fadeInUp linear both;
  animation-timeline: view();
  animation-range: entry 0% entry 100%;
}

@keyframes fadeInUp {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

View Transitions API

// Animasi transisi halaman
async function navigateWithTransition(url: string) {
  if (!document.startViewTransition) {
    window.location.href = url;
    return;
  }

  const transition = document.startViewTransition(async () => {
    const response = await fetch(url);
    const html = await response.text();
    const parser = new DOMParser();
    const doc = parser.parseFromString(html, 'text/html');
    document.body.innerHTML = doc.body.innerHTML;
    document.title = doc.title;
    window.history.pushState({}, '', url);
  });

  await transition.finished;
}
/* Kustomisasi View Transitions */
::view-transition-old(root) {
  animation: fade-out 0.3s ease-out;
}

::view-transition-new(root) {
  animation: fade-in 0.3s ease-in;
}

@keyframes fade-out {
  from { opacity: 1; transform: scale(1); }
  to   { opacity: 0; transform: scale(0.95); }
}

@keyframes fade-in {
  from { opacity: 0; transform: scale(1.05); }
  to   { opacity: 1; transform: scale(1); }
}

/* Transisi elemen individual */
.card-image {
  view-transition-name: card-hero;
}

::view-transition-old(card-hero),
::view-transition-new(card-hero) {
  animation-duration: 0.4s;
}

Keyframe Animation Kompleks

/* Animasi typing */
.typing {
  overflow: hidden;
  border-right: 2px solid var(--color-accent);
  white-space: nowrap;
  animation:
    typing 3s steps(30) 1s forwards,
    blink 0.75s step-end infinite;
  width: 0;
}

@keyframes typing {
  to { width: 100%; }
}

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

/* Animasi particle */
.particle {
  --delay: 0s;
  --x: 0px;
  --y: 0px;
  position: absolute;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--color-accent);
  animation: particle 1.5s var(--delay) ease-out infinite;
}

@keyframes particle {
  0% {
    opacity: 1;
    transform: translate(0, 0) scale(1);
  }
  100% {
    opacity: 0;
    transform: translate(var(--x), var(--y)) scale(0);
  }
}

Optimasi Performa

/* Memanfaatkan GPU acceleration */
.animated-element {
  /* Hanya animasikan transform dan opacity */
  will-change: transform, opacity;
  transform: translateZ(0); /* Buat layer independen */
}

/* Lepas will-change setelah animasi selesai */
.animated-element.done {
  will-change: auto;
}

/* Hindari layout shift */
.expand-animation {
  /* Gunakan max-height, bukan height */
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease;
}

.expand-animation.open {
  max-height: 500px; /* Tinggi yang cukup */
}

Dukungan Aksesibilitas

/* Hormati pengaturan pengurangan animasi */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  .typing {
    width: 100%;
    border-right: none;
  }

  .progress-bar {
    animation: none;
    transform: scaleX(1);
  }
}

Summary

CSS animation lanjutan memerlukan keseimbangan antara optimasi performa dan aksesibilitas. Dengan Claude Code, kamu bisa mengimplementasikan fitur terbaru seperti View Transitions API dan Scroll-driven Animations secara tepat. Dengan mengkombinasikan CSS Variables, parameter animasi juga bisa dikelola secara fleksibel. Untuk detail View Transitions API, lihat Chrome Developers.

#Claude Code #CSS Animation #View Transitions #Scroll #performance