Repository navigation
Adding and removing CSS classees with Stagger + onBegin / onComplete #1183
|
As the title suggests, looking for a way to add the |
Replies: 4 comments 3 replies
|
For per-element callbacks, use a timeline and pass import { createTimeline, stagger } from 'animejs';
const animation = createTimeline({
autoplay: false,
}).add(
this.dom.svgElevationFloors,
{
// Your animated properties go here, for example:
// opacity: [0, 1],
// duration: 500,
onBegin: ({ targets: [el] }) => {
el.classList.add('active');
},
onComplete: ({ targets: [el] }) => {
el.classList.remove('active');
},
},
stagger(100, { reversed: true }),
);The important difference is moving This behavior is documented under timeline position staggering: callbacks on the staggered animation are called for every target. The callback argument itself is the animation, as described in the |
|
Brilliant. Thanks a lot. |
|
@TargiX I had to reopen this because although I don't get any console errors, this approach seems to instantly apply the onComplete event as well which removes any delay between onBegin and onComplete. I want the class to be applied, wait for 500ms and then run the onComplete and remove the class. Could you please help? |
|
@monolithMktg Good catch - I reproduced this with your Pen. The stagger itself is working. The issue is that each child animation has no animatable property, so there is no tween to occupy the For a class-only effect, timer children are a better fit: import { createTimeline } from 'https://esm.sh/animejs';
const boxes = document.querySelectorAll('.box');
const animation = createTimeline({ autoplay: false });
boxes.forEach((el, index) => {
animation.add(
{
duration: 500,
onBegin: () => el.classList.add('active'),
onComplete: () => el.classList.remove('active'),
},
index * 100,
);
});
animation.play();I tested the timings: each box starts If you need the reversed order from your original example, use: const boxes = [...document.querySelectorAll('.box')].reverse();Anime.js supports this overload as |
@monolithMktg Good catch - I reproduced this with your Pen.
The stagger itself is working. The issue is that each child animation has no animatable property, so there is no tween to occupy the
500ms. As a result,onBeginandonCompletefire back-to-back at each staggered position.For a class-only effect, timer children are a better fit: