Animation: playState 屬性

Baseline 已廣泛支援

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

Animation.playState 是一個只讀屬性,屬於 Web Animations API,它返回一個列舉值,描述動畫的播放狀態。

idle

動畫的當前時間未確定,並且沒有待處理的任務。

running

動畫正在執行。

paused

動畫已暫停,並且 Animation.currentTime 屬性未更新。

finished

動畫已達到其邊界之一,並且 Animation.currentTime 屬性未更新。

以前,Web Animations 定義了一個 pending 值來指示某些非同步操作(例如開始播放)尚未完成。現在,這由單獨的 Animation.pending 屬性指示。

示例

“愛麗絲的生長/縮小遊戲” 示例中,玩家可以透過 “愛麗絲哭泣成一池眼淚” 來獲得結局。在該遊戲中,出於效能原因,眼淚應該只在可見時才進行動畫。因此,它們必須在動畫開始時像這樣暫停

js
// Setting up the tear animations

tears.forEach((el) => {
  el.animate(tearsFalling, {
    delay: getRandomMsRange(-1000, 1000), // randomized for each tear
    duration: getRandomMsRange(2000, 6000), // randomized for each tear
    iterations: Infinity,
    easing: "cubic-bezier(0.6, 0.04, 0.98, 0.335)",
  });
  el.pause();
});

// Play the tears falling when the ending needs to be shown.

tears.forEach((el) => {
  el.play();
});

// Reset the crying tears animations and pause them.

tears.forEach((el) => {
  el.pause();
  el.currentTime = 0;
});

規範

規範
Web 動畫
# dom-animation-playstate

瀏覽器相容性

另見