I have an animation I'd like to use for a rollover effect with the following basic states:
- resting: continuously loop between frames 0 and 55
- mouse enter: play once frames 56 to 78, then pause on 78
- mouse out: play once frames 79-95, then return to resting
I almost have it working except for a continuous loop on the resting state. The loop plays only once and then sits static. My code sits as follows:
Codepen: https://codepen.io/anon/pen/pBXKeN
var animationContainer = document.getElementById("animation-container");
var animation = lottie.loadAnimation({
wrapper: document.getElementById("animation-wrapper"),
renderer: "svg",
loop: false,
autoplay: false,
prerender: true,
animationData: animationData,
});
animation.addEventListener('DOMLoaded',resting);
animationContainer.addEventListener("mouseenter", hoverStart);
animationContainer.addEventListener("mouseleave", hoverEnd);
function resting() {
animation.removeEventListener("complete", resting);
console.log('resting');
animation.playSegments([0, 55], true);
}
function hoverStart() {
console.log('hover started');
animationContainer.removeEventListener("mouseenter", hoverStart);
animation.playSegments([56, 78], true);
animationContainer.addEventListener("mouseleave", hoverEnd);
}
function hoverEnd() {
console.log('hover ended');
animationContainer.removeEventListener("mouseleave", hoverEnd);
animation.playSegments([79, 95], true);
animation.addEventListener("complete", resting);
animationContainer.addEventListener("mouseenter", hoverStart);
}
I have tried setting loop to true, but this causes all 3 states to loop, which is not the desired effect for the mouseenter and mouseleave effects. Is there a way to get a single section to loop?
Also I'm happy to switch to jQuery if it makes things simpler.