What is staggering in anime.js?

Staggering in anime.js

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 follow-throughParts of the animation continue moving after the animation has ended to create more physically realistic animations. and overlapHaving different parts of animation be executed together to create more fluid animations..

Syntax

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

The value parameter

As 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.

Number

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 500500px.

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 00ms, box-2 to 500500ms, box-3 to 10001000ms, and so on. The delay for each targeted element increases by 500500ms.

String

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 00%, the next box to 2525%, and the last box to 5050%. The percentage increases by 2525 for each element.

Array

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 5050px to 250250px equally among all elements using the following formula.

Here, startstart and endend are 5050px and 250250px respectively. nn is the number of targeted elements and is equal to 55. ii is the element's index whose value needs to be found, keeping in mind the first index is 00.

According to the distribution formula, box-1 gets a translation of 5050px, box-2 gets 100100px, and so on.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved