Cours Angular · Module 13: UI et styles

Les animations dans Angular

2 min de lecture ·
Dans cette leçon4

Les animations sont comme les mouvements fluides d'un kata - une chorégraphie qui donne vie à l'application.

Configuration

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

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

Animations de base

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)' }))
      ])
    ]),

    // Active/inactive state
    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'))
    ]),

    // List with stagger effect
    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);
  }
}

Animations par 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 })
    ]))
  ])
])

Animations de groupe

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 })))
    ])
  ])
])

Tu as repéré une erreur dans cette leçon ?

Teste tes connaissances

Réponds aux questions de cette leçon. Choisis une réponse pour voir tout de suite si elle est correcte.

  1. 1. Que représente ':enter' dans une transition d'animation Angular ?

  2. 2. À quoi sert la fonction stagger() dans les animations Angular ?

Voici 2 des 3 questions de cette leçon. Réponds aux autres dans le jeu.

Tâches pratiques dans le jeu

  • Tri horizontal

    Ordonne sur une seule ligne un appel à animate() avec une durée et un style final.

  • Éditeur de code

    Complète l'animation fadeIn pour un élément qui apparaît.

  • Tri vertical

    Ordonne les lignes de la définition d'une animation d'entrée (de haut en bas dans l'éditeur).

  • Clique dans l'ordre

    Construis une transition entre deux états d'animation : transition('inactive <=> active', ...)

  1. Questiontrigger et transition
  2. Questionstagger
  3. Remets dans l'ordreAppeler animate()
  4. Exercice de codePratique : Animation d'entrée
  5. Remets dans l'ordreDéfinition d'une animation d'entrée
  6. QuestionLa fonction keyframes
  7. Clique dans l'ordreÉtat et transition d'animation

Articles utiles