iT邦幫忙

2025 iThome 鐵人賽

DAY 5
0
自我挑戰組

從 0 開始學 GSAP 馬拉松之旅系列 第 5

Day05 - 常用 Callbacks 簡介

  • 分享至 

  • xImage
  •  

雖然關於 Tween 的屬性與方法都還沒有詳細介紹,但透過之後的實作一邊介紹,似乎也是個可以嘗試的方向。在實作之前,今天想再分享常用的回調函數(Callback),分別是 onCompleteonUpdateonComplete

常見三大 Callbacks

Callback 觸發時機
onStart 動畫一開始時就觸發
onUpdate 動畫每一幀更新時都在觸發
onComplete 動畫完成時才觸發

只有文字上的描述似乎顯得單薄,以下是同時用了這三個 Callbacks 進行小嘗試的進度條:
GIF動畫

<body>
 <section class="flex h-screen justify-center items-center">
  <div class="w-40 h-auto flex flex-col items-start relative">
    <div class="progress-bar w-10 h-5 bg-orange-300 rounded-lg relative overflow-hidden">
      <div class="star w-5 h-5 bg-white absolute top-1/2 -translate-y-1/2 rounded-full"></div>
    </div>
    <p class="status pt-2 text-orange-400">Welcome!</p>
  </div>
</section>  
</body>
const progressBar = document.querySelector(".progress-bar");
const star = document.querySelector(".star");
const status = document.querySelector(".status");

gsap.to(progressBar, {
  width: 160,   
  duration: 5,
  ease: "power2.inOut",
  onStart: () => { status.textContent = "Welcome!"; },
  onUpdate: function() {
    const progress = this.progress();
    const maxWidth = 160 - 25; 
    star.style.left = progress * maxWidth + "px";
    status.textContent = "Loading... " + Math.round(progress * 100) + "%";
  },
  onComplete: () => { status.textContent = "Finished!"; }
});

備註:小星星是用 CSS clip path 製作出來的,就不特別分享啦~

後記:如何在鐵人賽上傳 gif

自從開賽就一直很苦惱無法直接上傳 gif,今天終於嘗試出來了!也在這裡分享一下:

  1. 先把影片透過線上網站轉成 gif
  2. 將 gif 上傳到 Cloudinary,上傳完畢後,點選 gif 的 COPY URL 選擇 Original
  3. 回到鐵人文章編輯區,選擇圖片按鈕,在 [ ] 中貼上剛剛複製好的連結,就 OK 了!

btw 很多人推薦的 imgur我已經試了兩天都無法註冊...如果可以,也很想嘗試看看這個網站
/images/emoticon/emoticon02.gif

默默來到第五天了,希望還有明天,明天見!
/images/emoticon/emoticon13.gif


上一篇
Day04 - Easing 基礎篇
下一篇
Day06 - 用 Stagger 做會動的文字
系列文
從 0 開始學 GSAP 馬拉松之旅6
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言