Angular animation triggers and transitions are the core building blocks for adding motion to your app. You define a trigger on a component’s template and then specify different states and transitions between those states. The browser then animates the element when its state changes, giving you smooth, performant motion without writing manual CSS animations or JavaScript timers.
Formula
The total time for an Angular animation is simply the sum of the duration and the delay you set in the transition:
Total time = duration + delay
Where:
- duration – the time the animation actually runs (in seconds).
- delay – the time before the animation starts (in seconds).
This formula helps you plan the timing of your animations, especially when coordinating multiple elements.
Worked Example
Using the defaults from the calculator: duration = 0.5 s, delay = 0 s.
Total time = 0.5 + 0 = 0.5 seconds.
If you add a 0.2 s delay, the total becomes 0.7 s. The calculator shows the first output as the total time.
What Are Angular Animation Triggers?
In Angular, an animation trigger is a named set of states and transitions that you attach to an element in the template. You define it in the component’s animations array using the trigger() function from @angular/animations. The trigger name is used in the template with [@triggerName].
For example:
trigger('fadeIn', [
state('void', style({ opacity: 0 })),
transition('void => *', [
animate('0.5s ease-in')
])
])
Here, void is a special state meaning the element is not in the DOM. When an element with [@fadeIn] enters the view, it animates from opacity 0 to its natural state.
How Transitions Work
A transition defines what happens when the element changes from one state to another. You specify the direction (e.g., 'open => closed') and a list of animation steps. The most common steps are style(), animate(), and group().
Angular uses the Web Animations API under the hood, so animations run on the browser’s compositor thread when possible, making them smooth. You can also use keyframes for more complex sequences:
transition('* => *', [
animate('1s', keyframes([
style({ opacity: 0, offset: 0 }),
style({ opacity: 1, offset: 0.5 }),
style({ opacity: 0, offset: 1 })
]))
])
Common States and Wildcards
Angular provides several built-in state aliases:
void– element is not in the DOM (used for enter/leave).*– wildcard, matches any state.void => *– enter animation.* => void– leave animation.void => void– no animation, useful for list reordering.
You can also define custom states like state('open', ...) and state('closed', ...) and transition between them, for example when a button toggles a panel.
How to Choose Timing and Easing
Timing is specified as a string: 'duration delay easing'. Duration can be in milliseconds (500ms) or seconds (0.5s). Easing options include ease, ease-in, ease-out, ease-in-out, or a cubic-bezier function like cubic-bezier(0.25, 0.1, 0.25, 1).
General guidelines:
- Use 200–500 ms for UI elements like modals and dropdowns.
- Use 500–1000 ms for larger elements or page transitions.
- Use
ease-outfor elements entering the screen (fast start, slow end). - Use
ease-infor elements leaving (slow start, fast end).
Test on real devices – what feels smooth on a desktop may feel sluggish on mobile.
Common Problems and How to Fix Them
One frequent issue is animations not firing because the trigger isn’t bound correctly. Check that the trigger name in the template matches exactly, and that the component has the BrowserAnimationsModule imported (or NoopAnimationsModule for testing).
Another issue is elements not animating on initial load. Use :enter and :leave aliases (which are equivalent to void => * and * => void) to handle insertion/removal.
Performance problems often come from animating properties like width or height that trigger layout. Prefer transform and opacity for smoother animations.
Comparison with CSS Animations
CSS animations are simpler and work well for basic effects. Angular animations offer advantages when you need to coordinate multiple elements, respond to component state changes, or reuse animation logic across components. Angular animations also integrate with Angular’s change detection and can be tested with TestBed.
For simple hover effects, CSS is often enough. For complex enter/leave sequences or state-driven motion, Angular animations are more maintainable.
Frequently Asked Questions
Do I need to import BrowserAnimationsModule?
Yes, unless you’re using Angular’s NoopAnimationsModule for testing. BrowserAnimationsModule provides the animation engine. Without it, triggers are ignored and elements appear instantly.
Can I use Angular animations with Angular Universal (SSR)?
Yes, but you need to be careful. Animations that rely on browser APIs won’t run on the server. Use NoopAnimationsModule on the server to avoid errors, and let the client-side module handle the actual animations.
How do I animate a list when items are added or removed?
Use the :enter and :leave transitions on the repeated element. For reordering, use animateChild() and the query() function to animate the moved items smoothly.
What’s the difference between animate() and group()?
animate() runs a single animation step. group() runs multiple animation steps in parallel. Use group() when you want to animate different properties at the same time, like moving and fading simultaneously.
Can I use a callback when an animation finishes?
Yes, you can use the start and done events on the trigger binding: (@triggerName.start) and (@triggerName.done). This is useful for chaining actions or updating state after the animation completes.
Why is my animation not smooth?
Usually because you’re animating a property that causes layout thrashing. Stick to transform and opacity. Also check that you’re not animating too many elements at once – consider using group() or staggering with stagger().
Related
Formula used
in t | Animation duration | sec | 0.5 | 0 | 10 in d | Delay | sec | 0 | 0 | 10 out total | Total time | sec | t + d | 2
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- China’s IPv9 Decimal Network Project Explained
- Fix Wired Earbuds That Only Work in One Ear
- What to Check Before Buying the Dip in Indian Shares
- Cool Raspberry Pi Projects: 15 Ideas for Any Skill Level3
- Terraform vs Kubernetes: Key Differences
- Internet Connectivity in Remote Areas: Options0.80 USD/GB