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:
Configuring the Animation Plugin
After installation, configure the plugin within your application's main configuration file, typically main.ts
located in the src
folder.
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-stagger
class on the<ul>
element delays the animation of each<li>
to prevent simultaneous animations.Each
<li>
has theau-animate
class along withanimate-fade-in
andanimate-fade-out
to 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
addClass
Adds a specified CSS class to an element, initiating the corresponding animation if defined.
removeClass
removeClass
Removes 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-stagger
class 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-order
attribute 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-animate
class 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?