I’m using react-lottie to animate a set of Lottie animations on a React page. I have an array of animation data (items
) that each corresponds to a different animation, and I’m trying to cycle through these animations based on both user interaction and automatically.
Here’s a simplified version of the code I’m using:
Code setup:
- Items Array:
My items
array includes the animationData
which holds the json
object for Lottie, title
and description
are strings to show on the page.
const items = [
{
animationData: layer1Animation,
title: 'social gaming experiences',
description: 'we develop entertaining engagement formats on the best social gaming platforms...',
},
// other items
];
- Default Options for Lottie:
For the react-lottie, I should have options
object which holds the parameters:
const defaultOptions = {
loop: false,
autoplay: true,
animationData: items[activeIndex].animationData,
rendererSettings: {
preserveAspectRatio: 'xMidYMid slice',
},
};
- Event Listeners:
According to the answer below, I have added the eventListeners
to the lottie to animate the index once the previous one is finished:
const eventListeners = [
{
eventName: 'complete',
callback: handleAnimationComplete,
},
];
const handleAnimationComplete = () => {
setActiveIndex((prevIndex) => (prevIndex + 1) % items.length);
};
- Rendering:
As for the rendering, I am rendering the card_items
which holds the animation and content:
<div className={styles.container}>
<h3>but what are our actual solutions?</h3>
<div className={styles.cart_items}>
<div className={`${styles.cart_item} ${styles.active}`}>
<div className={styles.image_container}>
<Lottie
key={activeIndex}
options={defaultOptions}
eventListeners={eventListeners}
height={400}
/>
</div>
<div className={styles.content}>
<p>{items[activeIndex].title}</p>
<span>{items[activeIndex].description}</span>
</div>
</div>
</div>
<div className={styles.animation_slider}>
{items.map((_, index) => (
<div
key={index}
onClick={() => setActiveIndex(index)}
className={index === activeIndex ? styles.active : styles.inactive}
></div>
))}
</div>
</div>
Problem
The issue appears once the index changes. So, when the index changes and a new animation is loaded, there is a noticeable delay (approximately 0.5 seconds or more) before the new animation starts. The content updates immediately, but the Lottie animation does not, leading to a jarring visual effect where the content changes while the previous animation is still playing. This delay causes a poor user experience as it disrupts the fluidity of the animation transitions.
** I have tried:**
- I’ve set up event listeners to trigger
handleAnimationComplete
which updates the index. However, the delay persists despite these listeners firing correctly. - I’m using
key={activeIndex}
on the Lottie component to ensure the component re-renders when the index changes, but this does not resolve the timing issue.
Current result
How it should be:
As showed in above result, I’m not quite sure why there is such delay in the animation. Ideally, the animation should start immediately as soon as the index changes and the content updates seamlessly, but currently there is around 0.5s delay each animation starts after the content.
Thus, to conclude, why does my lottie animation starts to autoplay with some delay?
You can also check the code at this codesandbox.
23
it could be possible that the animation is less than or more that the timeout specified, try using the onComplete event listener instead of timeout
Add this to your options
eventListeners=[
{
eventName: 'complete',
callback: () => onComplete,
},
]
add a new function
const onComplete () => {
setActiveIndex((prevIndex) => (prevIndex + 1) % items.length);
}
LINK: https://github.com/chenqingspring/react-lottie?tab=readme-ov-file#props
24