Anime.js is a JavaScript library that lets us easily create complicated animations. It provides a staggering technique that enables us to animate multiple elements simultaneously. It also helps in creating animations with
To introduce staggering to our animation, we must use the anime.stagger()
method. This method can be used as a value for any property or property parameter, given that it accepts a numerical value or a numerical string like '50%'
.
The following is the syntax:
anime({
...
property/property parameter: anime.stagger(value, options),
...
});
Here, a value
is a number, a numerical string, or an array. The options
parameter (optional) is an object that accepts the following key-value pairs:
start
from
direction
easing
grid
axis
value
parameterAs mentioned above, the value
parameter can take a number, a string, or an array. The following examples demonstrate the use of each type. All of the blue boxes are made by styling a <div>
tag with a class called a box
.
Each element's value for the concerned property increases by the number passed as the value
parameter.
anime({
targets: '.box',
translateX: 500,
delay: anime.stagger(500),
...
});
We target all elements with the box
class and translate them right along the X-axis to
For the delay
parameter, we use the anime.stagger()
method and give it a number, 500
. Now, box-1 will have its delay
set to delay
for each targeted element increases by
Similarly, we can use staggering with percentage values given as a string.
anime({
targets: '.box',
borderRadius: anime.stagger('25%'),
...
});
Once again, we target all elements with the box
class. We specify the borderRadius
property and use the anime.stagger()
method with the value 25%
. Doing so sets the borderRadius
of the first box to
If we want to specify a range of values to be distributed among the elements, we can use an array of two values, that is, the start and end points.
anime({
targets: '.box',
translateX: anime.stagger([50, 250]),
...,
});
In this case, the translation is distributed from
Here,
According to the distribution formula, box-1 gets a translation of
Free Resources