Ionic Animations is a utility that allows developers to build complex animations in a platform agnostic manner. Developers do not need to be using a particular framework such as React or Angular, nor do they even need to be building an Ionic app. As long as developers have access to v5.0 or greater of Ionic Framework, they will have access to all of Ionic Animations.
requestAnimationFrame loop. This reduces the scalability of your animations as the library is constantly computing values and using up CPU time.
Ionic Animations uses the Web Animations API to build and run your animations. In doing this, we offload all work required to compute and run your animations to the browser. As a result, this allows the browser to make any optimizations it needs and ensures your animations run as smoothly as possible. While most browsers support a basic implementation of Web Animations, we fallback to CSS Animations for browsers that do not support Web Animations. The performance difference in switching between these two should typically be negligible.
Developers using Ionic Core and TypeScript should install the latest version of
Developers using Angular should install the latest version of
@ionic/angular. Animations can be created via the
AnimationController dependency injection.
Developers using React should install the latest version of
@ionic/react. React wrappers are in beta. Please report any issues on GitHub!
Developers using Ionic Vue should install the latest version of
In the example above, an animation that changes the opacity on the
.square element and moves it from left to right along the X axis has been created. This animation will run an infinite number of times, and each iteration of the animation will last 1500ms.
By default, all Ionic Animations are paused until the
play method is called.
Ionic Animations allows you to control the intermediate steps in an animation using keyframes. Any valid CSS property can be used here, and you can even use CSS Variables as values.
Hyphenated CSS properties should be written using camel case when writing keyframes. For example,
border-radius should be written as
borderRadius. This also applies to the
In the example above, the
.square element will transition from a red background color, to a background color defined by the
--background variable, and then transition on to a green background color.
Each keyframe object contains an
offset is a value between 0 and 1 that defines the keyframe step. Offset values must go in ascending order and cannot repeat.
Multiple elements can be animated at the same time and controlled via a single parent animation object. Child animations inherit properties such as duration, easing, and iterations unless otherwise specified. A parent animation's
onFinish callback will not be called until all child animations have completed.
This example shows 3 child animations controlled by a single parent animation. Animations
squareB inherit the parent animation's duration of 2000ms, but animation
squareC has a duration of 5000ms since it was explicitly set.
Ionic Animations provides hooks that let you alter an element before an animation runs and after an animation completes. These hooks can be used to perform DOM reads and writes as well as add or remove classes and inline styles.
In this example, an inline opacity of 0.2 is set on the
.square element prior to the animation starting. Once the animation finishes, the background color of the element is set to
rgba(0, 255, 0, 0.5), and the inline opacity is cleared.
See Methods for a complete list of hooks.
Animations can be chained to run one after the other. The
play method returns a Promise that resolves when the animation has completed.
Ionic Animations gives developers the ability to create powerful gesture-based animations by integrating seamlessly with Ionic Gestures.
In this example we are creating a track along which we can drag the
.square element. Our
animation object will take care of moving the
.square element either left or right, and our
gesture object will instruct the
animation object which direction to move in.
Developers can also tailor their animations to user preferences such as
prefers-color-scheme using CSS Variables.
This method works in all supported browsers when creating animations for the first time. Most browsers are also capable of dynamically updating keyframe animations as the CSS Variables change.
Safari does not currently support dynamically updating keyframe animations. For developers who need this kind of support in Safari, they can use MediaQueryList.addListener().
Certain Ionic components allow developers to provide custom animations. All animations are provided as either properties on the component or are set via a global config.
Animating properties such as
width cause additional layouts and paints which can cause jank and degrade animation performance. On the other hand, animating properties such as
opacity are highly optimizable by the browser and typically do not cause much jank.
For information on which CSS properties cause layouts or paints to occur, see CSS Triggers.
For debugging animations in Chrome, there is a great blog post about inspecting animations using the Chrome DevTools: https://developers.google.com/web/tools/chrome-devtools/inspect-styles/animations.
It is also recommended to assign unique identifiers to your animations. These identifiers will show up in the Animations inspector in Chrome and should make it easier to debug:
Due to a bug in Safari versions 9-11, stepping through animations via
progressStepis not supported. This is supported on Safari 12+.
|All child animations of a given parent animation.|
|All elements attached to an animation.|
|The parent animation of a given animation object.|
|Group one or more animations together to be controlled by a parent animation.|
|Add one or more elements to the animation.|
|Add a class or array of classes to be added to all elements in an animation after the animation ends.|
|Add a function that performs a DOM read to be run after the animation ends.|
|Add a function that performs a DOM write to be run after the animation ends.|
|Add an array of property names to be cleared from the inline styles on all elements in an animation after the animation ends.|
|Add a class or an array of classes to be removed from all elements in an animation after the animation ends.|
|Add an object of styles to be applied to all elements in an animation after the animation ends.|
|Add a class or array of classes to be added to all elements in an animation before the animation starts.|
|Add a function that performs a DOM read to be run before the animation starts.|
|Add a function that performs a DOM write to be run before the animation starts.|
|Add an array of property names to be cleared from the inline styles on all elements in an animation before the animation starts.|
|Add a class or an array of classes to be removed from all elements in an animation before the animation starts.|
|Add an object of styles to be applied to all elements in an animation before the animation starts.|
|Set the direction the animation should play in.|
|Set the delay for the start of the animation in milliseconds.|
|Destroy the animation and clear all elements, child animations, and keyframes.|
|Set the duration of the animation in milliseconds.|
|Set the easing of the animation in milliseconds. See Easing Effects for a list of accepted easing values.|
|Set the start styles of the animation.|
|Set the start and end styles of the animation.|
|Set how the animation applies styles to its elements before and after the animation's execution.|
|Set the number of times the animation cycle should be played before stopping.|
|Set the keyframes for an animation.|
|Add a callback to be run upon the animation ending.|
|Pause the animation.|
|Play the animation.|
|Stop seeking through an animation.|
|Begin seeking through an animation.|
|Seek through an animation.|
|Stop the animation and reset all elements to their initial state.|
|Set the end styles of the animation.|