kb.erickguedes.com
CSS3: Estilização e Layout Moderno

Animações CSS

Aula 5 de 7

Transições CSS

Transições suavizam a mudança entre dois estados de um elemento.

Propriedades de Transição

.botao {
  background: #3498db;
  color: white;
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 6px;
  cursor: pointer;

  /* transition: property duration timing-function delay */
  transition: background 0.3s ease,
              transform 0.2s cubic-bezier(0.68, -0.55, 0.27, 1.55),
              box-shadow 0.3s ease 0.1s;
}

.botao:hover {
  background: #2980b9;
  transform: scale(1.05) translateY(-2px);
  box-shadow: 0 6px 20px rgba(0,0,0,0.2);
}

/* Timing functions */
.linear    { transition-timing-function: linear; }
.ease      { transition-timing-function: ease; }
.ease-in   { transition-timing-function: ease-in; }
.ease-out  { transition-timing-function: ease-out; }
.custom    { transition-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1); }
<button class="botao">Passe o Mouse</button>

Animações com @keyframes

/* Definição */
@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateX(-100px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.1); }
}

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

@keyframes fadeInUp {
  0% {
    opacity: 0;
    transform: translateY(30px);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Aplicação */
.animated {
  animation-name: slideIn;
  animation-duration: 0.6s;
  animation-timing-function: ease-out;
  animation-delay: 0.2s;
  animation-iteration-count: 1;
  animation-direction: normal;
  animation-fill-mode: backwards;
  /* backwards: aplica o primeiro keyframe antes do início */
  /* forwards: mantém o último keyframe após o fim */

  /* Atalho */
  animation: slideIn 0.6s ease-out 0.2s 1 normal backwards;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #eee;
  border-top-color: #3498db;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

/* Múltiplas animações */
.hero-title {
  animation:
    fadeInUp 0.6s ease-out,
    pulse 2s ease-in-out 1s infinite;
}

/* Pausar animação */
.animated.paused {
  animation-play-state: paused;
}

Transformações

.card {
  transition: transform 0.3s ease;
}

.card:hover {
  transform: translate(10px, -10px)    /* mover */
             rotate(2deg)               /* rotacionar */
             scale(1.02);               /* escalar */
}

/* Transform 3D */
.flip-card {
  perspective: 1000px;
}

.flip-inner {
  transition: transform 0.6s;
  transform-style: preserve-3d;
}

.flip-card:hover .flip-inner {
  transform: rotateY(180deg);
}

.flip-front, .flip-back {
  backface-visibility: hidden;
  position: absolute;
  inset: 0;
}

.flip-back {
  transform: rotateY(180deg);
}

/* Skew */
.skewed {
  transform: skewX(-5deg);
  transform-origin: left center;
}
<div class="flip-card">
  <div class="flip-inner">
    <div class="flip-front">Frente</div>
    <div class="flip-back">Verso</div>
  </div>
</div>

Performance

/* will-change: avisa o navegador sobre mudanças */
.animated-element {
  will-change: transform, opacity;
}

/* Boas práticas de performance */
/* ✅ Prefira: transform e opacity */
/* ❌ Evite: width, height, top, left (causam reflow) */

.elemento {
  transition: transform 0.3s ease, opacity 0.3s ease;
}

/* Invés de animar left/top */
.elemento-antigo {
  left: 100px; /* causa reflow ❌ */
}

/* Anime transform */
.elemento-novo {
  transform: translateX(100px); /* só composição ✅ */
}

/* Hardware acceleration */
.gpu-friendly {
  transform: translateZ(0);
  /* ou */
  will-change: transform;
}
<div class="elemento-novo">Animação performática</div>

Lab: Cartão Animado

Crie um cartão com hover (escala + sombra), um spinner loading e animação de entrada com @keyframes.

# Verifique performance no DevTools
# Performance tab > Record interação
# Veja green bars (composite) vs purple (layout/paint)

Animações CSS devem ser sutis e performáticas. Anime apenas transform e opacity para 60fps consistentes.