Animation
Animations bring dynamic visual interactions to your Aurelia applications, enhancing user experience by smoothly transitioning elements' styles over time.
Animations enhance the interactivity and visual appeal of your Aurelia applications by allowing elements to transition smoothly between different styles, such as changes in size, color, or position. Aurelia's animation system is built around a simple animation interface within its templating library, providing flexibility to use any animation library or custom implementations.
This documentation focuses on using the official CSS animation plugin, aurelia-animator-css, for Aurelia. Alternatively, you can opt for other plugins like aurelia-animator-velocity or create your own animator based on Aurelia's animation interface.
Installing the Animation Plugin
To incorporate CSS animations into your Aurelia project, install the aurelia-animator-css plugin via NPM:
npm install aurelia-animator-cssConfiguring the Animation Plugin
After installation, configure the plugin within your application's main configuration file, typically main.ts located in the src folder.
import { PLATFORM } from 'aurelia-pal';
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging()
.plugin(PLATFORM.moduleName('aurelia-animator-css')); // Add the animation plugin here
aurelia.start().then(a => a.setRoot());
}Alternatively, you can configure the plugin to handle animation completion explicitly:
Ensure PLATFORM.moduleName is used when configuring the plugin if you're utilizing Webpack.
Defining Animations
Start by defining keyframe animations in your CSS file, such as animations.css. These animations describe the transformation from one style to another over a specified duration.
Next, create CSS classes that utilize these keyframe animations. These classes follow a naming convention with specific suffixes to integrate with Aurelia's animation lifecycle.
These classes utilize the following lifecycle hooks:
Enter
Element enters the DOM
au-enter
au-enter-active
au-entered
Leave
Element leaves the DOM
au-leave
au-leave-active
au-left
addClass
Adds a CSS class
[className]-add
[className]
removeClass
Removes a CSS class
[className]-remove
Applying Animations to Elements
To activate animations on specific elements, apply the au-animate class along with the desired animation classes you defined earlier. Aurelia will automatically handle the addition and removal of the appropriate animation classes based on the element's lifecycle events.
Example: Animating a Repeater
Consider a list of to-do items rendered using Aurelia's repeat.for binding. To animate each item as it enters and leaves the DOM, apply the au-animate class along with the specific animation classes.
In the above example:
The
au-staggerclass on the<ul>element delays the animation of each<li>to prevent simultaneous animations.Each
<li>has theau-animateclass along withanimate-fade-inandanimate-fade-outto handle entering and leaving animations.
Example: Animating Router Views
To animate views rendered within a router-view, add the au-animate class and the desired animation classes to the first child of the view template.
Additionally, configure the router-view to manage the order of animations between views:
For more details on the swap-order attribute and available options, refer to the Router Documentation.
Using Animation Methods
Aurelia's animator provides methods to programmatically add or remove CSS classes, triggering animations as needed.
addClass
addClassAdds a specified CSS class to an element, initiating the corresponding animation if defined.
removeClass
removeClassRemoves a specified CSS class from an element, triggering the corresponding exit animation if defined.
Examples
Animating a Repeater
The following example demonstrates animating a list of to-do items with fade-in and fade-out effects using Aurelia's repeater.
In this setup:
New to-do items fade in as they are added.
Removed to-do items fade out before being removed from the DOM.
The
au-staggerclass ensures that multiple items do not animate simultaneously, creating a staggered effect.
Animating Router Views
Add animation classes to the view templates to animate transitions between different views managed by Aurelia's router.
Configure the router-view in your application's main template:
This configuration ensures that when navigating between views:
The new view slides in from the right.
The old view slides out to the left.
The
swap-orderattribute manages the timing of entering and leaving animations to create a smooth transition.
Custom Animations
While the aurelia-animator-css plugin provides a convenient way to use CSS animations, Aurelia's animation system is designed to be flexible. You can create custom animation plugins by implementing Aurelia's animation interface, allowing integration with libraries like GreenSock (GSAP) or custom animation logic.
Creating a Custom Animator
Implement the Animation Interface:
Create a new class that implements the required methods (
enter,leave,addClass,removeClass).Register the Custom Animator:
Configure Aurelia to use your custom animator in
main.ts.Utilize the Custom Animator:
Apply the
au-animateclass and your custom animation classes to elements as needed, similar to using the CSS animator.
Summary
Aurelia's animation system offers a robust and flexible way to enhance your applications with smooth, visually appealing transitions. By leveraging the aurelia-animator-css plugin, you can easily implement standard CSS animations by defining keyframes and applying specific classes to your elements. For more advanced scenarios, Aurelia's interface allows the integration of custom animation libraries or the creation of bespoke animation solutions, empowering you to tailor animations precisely to your application's needs.
Last updated
Was this helpful?