亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 ScrollMagic 和 TimelineMax 控??制 Lottie 動畫

使用 ScrollMagic 和 TimelineMax 控??制 Lottie 動畫

慕田峪7331174 2021-12-02 19:10:58
我正在嘗試使用 ScrollMagic 來控制一個 TimelineMax,它移動 Lottie 動畫的播放頭。因此,當用戶滾動時,動畫會根據滾動的速度和方向播放。我非常接近,需要一點幫助才能將效果帶回家。首先,我包括我的圖書館<script src="https://cdnjs.cloudflare.com/ajax/libs/bodymovin/5.5.9/lottie.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.1.3/TweenMax.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.7/ScrollMagic.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.7/plugins/animation.gsap.js"></script>然后...  // init scrollmagic  var controller = new ScrollMagic.Controller();現在,動畫設置...  // Manage Animation  var animation = bodymovin.loadAnimation({    container: document.getElementById('lottie'), // Required    path: '/static/animations/animation.json', // Required    renderer: 'svg', // Required    loop: false, // Optional    autoplay: false, // Optional    name: "Welcome to Awesomeness", // Name for future reference. Optional.  });這是我掙扎的地方:  // Setup Timeline  var lottieControl = new TimelineMax({ repeat: -1, yoyo: true }); // <-- don't loop and let the timeline go back and forth  lottieControl.to({ frame:0 }, 1, { // <-- is this right? I'm telling the timeline to start at frame 0    onUpdate:function(){      animation.goToAndStop(Math.round(this.target.frame), true) // <-- move the playback head of the animation to the target frame that has been rounded and use frames not time (true)    },    ease:Linear.easeNone  })最后,把這一切重新組合起來......  // Attach to scroll  var lottieScene = new ScrollMagic.Scene({        duration: '80%',        offset: 1    })    .setPin("#header-scroll")    .setTween(lottieControl)    .addTo(controller);對于我的一生,我無法確定我是否goToAndStop正確使用了該方法。謝謝你的時間。
查看完整描述

1 回答

?
喵喵時光機

TA貢獻1846條經驗 獲得超7個贊

經過數小時的測試,我找到了答案。我看錯了。我需要將時間線進度與要轉到的框架聯系起來。在本例中,有 300 幀,因此乘以動畫中的幀數。


這是修改后的代碼:


<script src="https://cdnjs.cloudflare.com/ajax/libs/bodymovin/5.5.9/lottie.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.1.3/TweenMax.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.7/ScrollMagic.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.7/plugins/animation.gsap.js"></script>


<script>

  // init scrollmagic

  var controller = new ScrollMagic.Controller();


  // Manage Animation

  var animation = bodymovin.loadAnimation({

    container: document.getElementById('lottie'), // Required

    path: '/static/animations/scroll_animation.json', // Required

    renderer: 'svg', // Required

    loop: false, // Optional

    autoplay: false, // Optional

    name: "Welcome to Awesomeness",

  });


  // Setup Timeline

  var tl = new TimelineMax();

  tl.to({frame:0}, 1, {

    frame: animation.totalFrames-1,

    onUpdate:function(){

      animation.goToAndStop((Math.round(this.progress() * 300)), true)

    },

    ease: Linear.easeNone

  })



  // Attach to scroll

  var lottieScene = new ScrollMagic.Scene({

        duration: '100%',

        offset: 1

    })

    .setPin("#header-scroll")

    .setTween(tl)

    .addTo(controller);


</script>


查看完整回答
反對 回復 2021-12-02
  • 1 回答
  • 0 關注
  • 254 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號