Kurs Angular · Moduł 13: UI i stylowanie

Animacje w Angular

2 min czytania ·
W tej lekcji4

Animacje to jak płynne ruchy w kata - choreografia, która ożywia aplikację.

Konfiguracja

// app.config.ts
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';

export const appConfig: ApplicationConfig = {
  providers: [
    provideAnimationsAsync()  // Lazy loading animacji
  ]
};

Podstawowe Animacje

import {
  trigger,
  transition,
  style,
  animate,
  state,
  query,
  stagger,
  keyframes,
  group
} from '@angular/animations';

@Component({
  selector: 'app-samurai-list',
  animations: [
    // Fade in/out
    trigger('fadeInOut', [
      transition(':enter', [
        style({ opacity: 0, transform: 'translateY(-20px)' }),
        animate('300ms ease-out', style({ opacity: 1, transform: 'translateY(0)' }))
      ]),
      transition(':leave', [
        animate('200ms ease-in', style({ opacity: 0, transform: 'translateY(20px)' }))
      ])
    ]),

    // Stan aktywny/nieaktywny
    trigger('activeState', [
      state('inactive', style({
        transform: 'scale(1)',
        opacity: 0.8
      })),
      state('active', style({
        transform: 'scale(1.05)',
        opacity: 1,
        boxShadow: '0 4px 20px rgba(0,0,0,0.2)'
      })),
      transition('inactive <=> active', animate('200ms ease-in-out'))
    ]),

    // Lista z efektem stagger
    trigger('listAnimation', [
      transition('* => *', [
        query(':enter', [
          style({ opacity: 0, transform: 'translateX(-50px)' }),
          stagger('50ms', [
            animate('300ms ease-out', style({ opacity: 1, transform: 'translateX(0)' }))
          ])
        ], { optional: true })
      ])
    ])
  ],
  template: `
    <div [@listAnimation]="samuraiList().length">
      @for (samurai of samuraiList(); track samurai.id) {
        <div
          class="samurai-card"
          [@fadeInOut]
          [@activeState]="samurai.id === selectedId() ? 'active' : 'inactive'"
          (click)="select(samurai.id)"
        >
          {{ samurai.name }}
        </div>
      }
    </div>
  `
})
export class SamuraiListComponent {
  samuraiList = signal<Samurai[]>([]);
  selectedId = signal<number | null>(null);

  select(id: number): void {
    this.selectedId.set(id);
  }
}

Animacje Keyframes

trigger('attack', [
  transition('* => attacking', [
    animate('500ms', keyframes([
      style({ transform: 'translateX(0)', offset: 0 }),
      style({ transform: 'translateX(100px) rotate(45deg)', offset: 0.5 }),
      style({ transform: 'translateX(0)', offset: 1 })
    ]))
  ])
])

Animacje Grupowe

trigger('cardFlip', [
  transition('front => back', [
    group([
      animate('300ms', style({ transform: 'rotateY(180deg)' })),
      query('.front', animate('300ms', style({ opacity: 0 }))),
      query('.back', animate('300ms', style({ opacity: 1 })))
    ])
  ])
])

Widzisz błąd w tej lekcji?

Sprawdź się

Odpowiedz na pytania z tej lekcji. Wybierz odpowiedź, a od razu zobaczysz, czy jest poprawna.

  1. 1. Co reprezentuje ':enter' w transition Angular animations?

  2. 2. Do czego służy funkcja stagger() w animacjach Angular?

To 2 z 3 pytań do tej lekcji. Pozostałe rozwiążesz w grze.

Zadania praktyczne w grze

  • Układanie w poziomie

    Ułóż w jednej linii wywołanie animate() z czasem trwania i stylem końcowym.

  • Edytor kodu

    Uzupełnij animację fadeIn dla pojawiającego się elementu.

  • Układanie w pionie

    Uporządkuj linie definicji animacji wejścia (kolejność od góry do dołu w edytorze).

  • Klikanie w kolejności

    Zbuduj przejście między dwoma stanami animacji: transition('inactive <=> active', ...)

  1. Pytanietrigger i transition
  2. Pytaniestagger
  3. Ułóż w kolejnościWywołanie animate()
  4. Zadanie z kodemPraktyka: Animacja wejścia
  5. Ułóż w kolejnościDefinicja animacji wejścia
  6. PytanieFunkcja keyframes
  7. Kliknij w kolejnościStan i przejście animacji

Przydatne artykuły