После создания анимации используйте anim.goToAndStop(0)
, чтобы приостановить ее:
const animData = ... // Let's pretend that you loaded this from a .json file
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.className = 'myAnimation';
// Create the animation
const anim = lottie.loadAnimation({
renderer: 'canvas',
loop: true,
autoplay: false,
rendererSettings: {
context: ctx,
scaleMode: 'noScale',
clearCanvas: true,
},
animationData: animData,
});
// Go to the first frame and pause
anim.goToAndStop(0);
Затем вы можете использовать что-то вроде OnScreen (https://github.com/silvestreh/onScreen), чтобы определить, когда анимация видна:
import OnScreen from 'onscreen';
const os = new OnScreen();
os.on('enter', '.myAnimation', (element, event) => {
anim.play(); // If animation becomes visible, play it
});
os.on('leave', '.myAnimation', (element, event) => {
anim.goToAndStop(0); // If animation goes off screen, reset it
});
Вышеприведенное относится к одной анимации, но с некоторыми незначительными изменениями ее можно расширить на несколько анимаций.