jquery 在每个循环中运行Lottie动画,但等待动画完成?

r1wp621o  于 11个月前  发布在  jQuery
关注(0)|答案(1)|浏览(161)

我正在使用[lottie-web][1]在浏览器中播放一些SVG动画,但是我需要一次播放一个,并且只有在当前动画完成后才能转到下一个。
目前,lottie.play()一次播放所有的动画,因为我想在each()中的下一个循环之前,一次运行一次。
我觉得这应该是相对简单的,但努力使它的工作。我一直在玩lottie。onComplete,但没有运气到目前为止。也许我需要分别注册每个动画?
谢谢.

超文本标记语言

<div class="step one">
    <div class="animation" data-json="JSON DATA HERE"></div>
</div>
<div class="step two">
    <div class="animation" data-json="JSON DATA HERE"></div>
</div>
<div class="step three">
    <div class="animation" data-json="JSON DATA HERE"></div>
</div>

字符串

脚本

// Vars
var steps = $('.step');

// Loop through steps
steps.each( function(){
      
    // Find animation for this step
    var animation = $(this).find('.animation');
    // Convert from jQuery to vanilla js for use with lottie-web
    var anim = animation.get(0);

    // Lottie animation (vanilla js)
    var lottie = bodymovin.loadAnimation({
        container: anim,
        renderer: 'svg',
        loop: false,
        autoplay: false, // Don't autoplay, so we can play at desired time
        path: anim.getAttribute('data-json')
    });

    // Play animation
    lottie.play(); // Plays all, but should only play current animation

    // On animation complete
    lottie.onComplete = function(){

      // On complete, proceed to next .step
      console.log('complete');
    }

});

7tofc5zh

7tofc5zh1#

你可以像贝娄一样做假设你有三个洛蒂的参与人

<lottie-player id="player1" src='url_to_lottie_file_1'  style=""  background="transparent" loop="false" speed="1" ></lottie-player>
<lottie-player id="player2" src='url_to_lottie_file_2'  style=""  background="transparent" loop="false" speed="1" ></lottie-player>
<lottie-player id="player3" src='url_to_lottie_file_3'  style=""  background="transparent" loop="false" speed="1" ></lottie-player>

字符串
包含Lottie播放器插件

<script src="https://unpkg.com/@@lottiefiles/lottie-player@latest/dist/lottie-player.js" defer></script>


现在你想在第一个完成后开始第二个玩家。当第二个完成时,开始第三个。

let player1= document.querySelector("#player1");
let player2= document.querySelector("#player2");
let player3= document.querySelector("#player3");
       
player1.addEventListener('loop', completedLoop1);
function completedLoop1(){
    player2.play();
    //remove event listener
    player1.removeEventListener('loop', completeEvent1);            
}

player2.addEventListener('loop', completedLoop2);
function completedLoop1(){
     player3.play();
     //remove event listener
     player2.removeEventListener('loop', completeEvent2);
}
//start the animation
player1.play();

相关问题