Keyframe animations let you create smooth, step-by-step changes in your app's look or position. They make your app feel alive and interactive.
Keyframe animations in Angular
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Angular
import { trigger, state, style, animate, keyframes, transition } from '@angular/animations'; @Component({ selector: 'app-example', templateUrl: './example.component.html', animations: [ trigger('animationName', [ transition('startState => endState', [ animate('duration easing', keyframes([ style({ property: 'value', offset: 0 }), style({ property: 'value', offset: 0.5 }), style({ property: 'value', offset: 1 }) ])) ]) ]) ] })
trigger defines the animation name to use in the template.
keyframes lets you specify multiple steps with style and offset from 0 to 1.
Examples
Angular
animations: [ trigger('fadeIn', [ transition(':enter', [ animate('1s ease-in', keyframes([ style({ opacity: 0, offset: 0 }), style({ opacity: 1, offset: 1 }) ])) ]) ]) ]
Angular
animations: [ trigger('moveRight', [ transition('* => *', [ animate('500ms ease-out', keyframes([ style({ transform: 'translateX(0)', offset: 0 }), style({ transform: 'translateX(100px)', offset: 1 }) ])) ]) ]) ]
Sample Program
This component shows a button that bounces up and down when clicked. The bounce uses keyframes to move the button smoothly in steps.
Angular
import { Component } from '@angular/core'; import { trigger, transition, animate, keyframes, style } from '@angular/animations'; @Component({ selector: 'app-bounce-button', template: ` <button [@bounce]="state" (click)="toggleState()" aria-label="Bounce button"> Click me </button> `, animations: [ trigger('bounce', [ transition('inactive => active', [ animate('600ms ease-in-out', keyframes([ style({ transform: 'translateY(0)', offset: 0 }), style({ transform: 'translateY(-30px)', offset: 0.3 }), style({ transform: 'translateY(0)', offset: 0.6 }), style({ transform: 'translateY(-15px)', offset: 0.8 }), style({ transform: 'translateY(0)', offset: 1 }) ])) ]) ]) ] }) export class BounceButtonComponent { state = 'inactive'; toggleState() { this.state = this.state === 'inactive' ? 'active' : 'inactive'; } }
Important Notes
Always include aria-label or other accessibility attributes for interactive elements.
Use offset values between 0 and 1 to control animation steps precisely.
Test animations on different screen sizes to ensure they look good everywhere.
Summary
Keyframe animations let you create smooth, multi-step visual changes.
Use Angular's keyframes inside animate for detailed control.
They make your app feel more dynamic and engaging.
Practice
1. What is the main purpose of using
keyframe animations in Angular?easy
Solution
Step 1: Understand what keyframe animations do
Keyframe animations allow defining multiple steps of style changes over time, creating smooth transitions.Step 2: Identify Angular's use of keyframes
Angular uses keyframes inside theanimatefunction to control detailed animation steps.Final Answer:
To create smooth, multi-step visual changes in components -> Option BQuick Check:
Keyframe animations = smooth multi-step visual changes [OK]
Hint: Keyframes control step-by-step visual changes [OK]
Common Mistakes:
- Confusing animations with data handling
- Thinking keyframes manage routing
- Mixing animations with HTTP requests
2. Which of the following is the correct syntax to import keyframe animation functions in Angular?
easy
Solution
Step 1: Identify the correct Angular package for animations
Angular animations functions likekeyframesandanimateare imported from '@angular/animations'.Step 2: Check the exact function names and import syntax
The correct functions arekeyframesandanimate, imported with curly braces from '@angular/animations'.Final Answer:
import { keyframes, animate } from '@angular/animations'; -> Option AQuick Check:
Correct import = import { keyframes, animate } from '@angular/animations'; [OK]
Hint: Animations import from '@angular/animations' with exact names [OK]
Common Mistakes:
- Importing from '@angular/core' instead of '@angular/animations'
- Using wrong function names like 'keyframe' or 'animation'
- Missing curly braces in import statement
3. Given the Angular animation code below, what will be the final background color after the animation completes?
animate('2s', keyframes([
style({ backgroundColor: 'red', offset: 0 }),
style({ backgroundColor: 'blue', offset: 0.5 }),
style({ backgroundColor: 'green', offset: 1 })
]))medium
Solution
Step 1: Understand keyframe offsets and colors
The animation starts at red (offset 0), changes to blue at halfway (offset 0.5), and ends at green (offset 1).Step 2: Determine final style after animation
At offset 1 (end), the background color is green, so the final color after animation is green.Final Answer:
green -> Option DQuick Check:
Final offset color = green [OK]
Hint: Final offset (1) style is final animation state [OK]
Common Mistakes:
- Choosing the first or middle color instead of final
- Confusing offset values with time
- Assuming default color remains after animation
4. Identify the error in this Angular animation code snippet:
trigger('fadeIn', [
transition(':enter', [
animate('1s', keyframes([
style({ opacity: 0, offset: 0 }),
style({ opacity: 1 })
]))
])
])medium
Solution
Step 1: Check keyframe style objects for offsets
Each style in keyframes should have anoffsetbetween 0 and 1 to define timing precisely.Step 2: Identify missing offset
The second style lacks anoffsetproperty, which is required for keyframes to work correctly.Final Answer:
Missing offset value in second style inside keyframes -> Option CQuick Check:
All keyframe styles need offset [OK]
Hint: Every keyframe style needs an offset between 0 and 1 [OK]
Common Mistakes:
- Omitting offset in some keyframe styles
- Using wrong transition selectors
- Passing duration as number instead of string
5. You want to create an Angular animation that moves an element from left to right and changes its opacity from 0 to 1 in 3 seconds, with three key steps: start (left: 0px, opacity: 0), middle (left: 50px, opacity: 0.5), and end (left: 100px, opacity: 1). Which of the following
keyframes arrays correctly implements this?hard
Solution
Step 1: Verify all keyframe styles have offsets
Each style must have anoffsetbetween 0 and 1 to define animation timing precisely.Step 2: Check property values and units
Left positions must be strings with units like 'px'. [ style({ left: '0px', opacity: 0, offset: 0 }), style({ left: '50px', opacity: 0.5, offset: 0.5 }), style({ left: '100px', opacity: 1, offset: 1 }) ] correctly uses '0px', '50px', '100px' and includes all offsets.Step 3: Identify the correct option
[ style({ left: '0px', opacity: 0, offset: 0 }), style({ left: '50px', opacity: 0.5, offset: 0.5 }), style({ left: '100px', opacity: 1, offset: 1 }) ] has all offsets and correct units; others miss offsets or units, making them invalid.Final Answer:
Option A with all offsets and px units -> Option AQuick Check:
Offsets + units correct = [ style({ left: '0px', opacity: 0, offset: 0 }), style({ left: '50px', opacity: 0.5, offset: 0.5 }), style({ left: '100px', opacity: 1, offset: 1 }) ] [OK]
Hint: All keyframes need offsets and units for position values [OK]
Common Mistakes:
- Missing offset properties in some keyframes
- Using numbers without units for CSS properties
- Partial offsets causing animation errors
