What are targets in Anime.js?
What is Anime.js?
Anime.js is a lightweight and easy-to-use JavaScript library for creating animations.
Note: If you are unfamiliar with anime.js, click here to read a basic overview first.
To animate any element, we call the anime() function and pass a parameter object with certain targets key.
anime({targets: values,...});
The value of the targets key is the element or elements we want to animate.
The value can be the following:
- A CSS selector
- A
or list of DOM nodesDOM node An object in the DOM - A JavaScript object
- An array of the previously mentioned values
CSS selector
We can use any CSS selector as a target. This includes class selectors and ID selectors, to name a few.
Note: Click here to learn more about CSS selectors.
However, since targets key.
Explanation
The above example demonstrates target selection using type, class, and ID. We use a <p> tag and two <div> tags for each selection.
- Line 1–6: We create an animation using the
anime()function. We set thetargetskey to'p'. This selects all the paragraph elements. The animation itself is a color shift from black to red over three seconds. - Line 7–12: We create an animation with the
targetskey set to'.box'. All elements having theboxclass will be selected for the animation. We specify the animation to change the box's color to green over three seconds. - Line 13–18: We create an animation with
targetskey set to'#box2'. Only the element with the ID,box2, will be animated to move vertically.
DOM node or list of DOM nodes
Another way to choose elements for animation is by passing a DOM object or a list of DOM objects as the value for targets.
Explanation
In the above example, we first select objects from the DOM and then apply animations to each object.
- Line 1: We select the element with the ID,
bluebox, from the DOM and store it in theblue_boxvariable. - Line 2: We select all elements with the
pinkclass from the DOM and store them inpink_boxes. - Line 3–8: We create an animation with
targetsset toblue_box. We specify the animation to translate the box left by 500px. As a result, the element with the IDblueboxis translated. - Line 9–14: We create an animation with
targetsset topink_boxes. By this point,pink_boxesis a list of five DOM nodes. We specify the animation to rotate each box by 360 degrees. As a result, all elements with classpinkare rotated.
JavaScript object
We can also pass a JavaScript object as a target value. However, the object must contain at least one numerical value.
Explanation
In this example, we animate a simple loading-progress meter.
- Line 1: We select the element with the ID,
loadingDisplay, from the DOM. This element will be used to display the JavaScript object that we want to animate. - Line 2–5: We create a JavaScript object with one numerical value,
progress. - Line 6–15: We create an animation and pass the
loadingobject as a target. We can now specify animations on the numerical values of theloadingobject. In this case, we want theprogressvalue to transition to100. - Line 13–15: We use the
updatecallback to update thepelement after each animation frame.
Array
We may also mix and match our method of selecting a target by using an array. The array can consist of any of the previously discussed target values.
Explanation
For this example, we consider a <p> tag, and two <div> tags. Each tag is selected using one of the previously described methods.
- Line 1: We select the element with the ID,
box2, from the DOM. - Line 2: We select the element with the ID,
counter, from the DOM. This element will be used to display the JavaScript object (counterObj) that we animate. - Line 4–6: We create a JavaScript object,
counterObj, with a numerical value. - Line 8–20: We create an animation. For the
targetskey, we have passed an array consisting of a CSS selector ('.box'), a DOM node (box2), and a JavaScript object (counterObj). - Line 11–13: We specify the animation to move the boxes vertically downwards and increase the count value of
counterObjto999.
Free Resources