Animation:finish 事件

Baseline 已廣泛支援

此功能已經成熟,並可在許多裝置和瀏覽器版本上使用。自 ⁨2020 年 3 月⁩起,它已在各瀏覽器中推出。

Animation 介面的 finish 事件會在動畫播放完畢時觸發,這可能發生在動畫自然完成時,也可能在呼叫 Animation.finish() 方法以立即結束動畫時。

注意: "paused" 播放狀態會覆蓋 "finished" 播放狀態;如果動畫同時處於暫停和完成狀態,則會報告 "paused" 狀態。你可以透過將其 startTime 設定為 document.timeline.currentTime - (Animation.currentTime * Animation.playbackRate) 來強制動畫進入 "finished" 狀態。

語法

在諸如 addEventListener() 之類的方法中使用事件名稱,或設定事件處理程式屬性。

js
addEventListener("finish", (event) => { })

onfinish = (event) => { }

事件型別

一個 AnimationPlaybackEvent 物件。繼承自 Event

Event AnimationPlaybackEvent

事件屬性

除了下面列出的屬性之外,父介面 Event 的屬性也可使用。

AnimationPlaybackEvent.currentTime 只讀

生成事件的動畫的當前時間。

AnimationPlaybackEvent.timelineTime 只讀

生成事件的動畫的時間線時間值。

示例

在 Alice in Web Animations API Land 的 Growing/Shrinking Alice Game 中,Animation.onfinish 被使用了多次。以下是一個例子,我們在元素的透明度動畫淡入完成後,重新添加了指標事件。

js
// Add an animation to the game's ending credits
const endingUI = document.getElementById("ending-ui");
const bringUI = endingUI.animate(keysFade, timingFade);

// Pause said animation's credits
bringUI.pause();

// This function removes pointer events on the credits.
hide(endingUI);

// When the credits are later faded in,
// we re-add the pointer events when they're done
bringUI.onfinish = (event) => {
  endingUI.style.pointerEvents = "auto";
};

規範

規範
Web 動畫
# dom-animation-onfinish
Web 動畫
# finish-event

瀏覽器相容性

另見