Animation: pause() Methode

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2020.

Die pause() Methode des Web Animations API's Animation Interface unterbricht die Wiedergabe der Animation.

Syntax

js
pause()

Parameter

Keine.

Rückgabewert

Keiner.

Ausnahmen

InvalidStateError DOMException

Wird ausgelöst, wenn die currentTime der Animation unresolved ist (vielleicht wurde sie noch nicht gestartet) und die Endzeit der Animation positive Unendlichkeit ist.

Beispiel

Animation.pause() wird häufig im Alice in Web Animations API Land Growing/Shrinking Alice Game verwendet, hauptsächlich weil Animationen, die mit der Element.animate() Methode erstellt wurden, sofort mit der Wiedergabe beginnen und manuell angehalten werden müssen, wenn Sie das vermeiden möchten:

js
// animation of the cupcake slowly getting eaten up
const nommingCake = document
  .getElementById("eat-me_sprite")
  .animate(
    [{ transform: "translateY(0)" }, { transform: "translateY(-80%)" }],
    {
      fill: "forwards",
      easing: "steps(4, end)",
      duration: aliceChange.effect.timing.duration / 2,
    },
  );

// doesn't actually need to be eaten until a click event, so pause it initially:
nommingCake.pause();

Zusätzlich beim Zurücksetzen:

js
// An all-purpose function to pause the animations on Alice, the cupcake, and the bottle that reads "drink me."
const stopPlayingAlice = () => {
  aliceChange.pause();
  nommingCake.pause();
  drinking.pause();
};

// When the user releases the cupcake or the bottle, pause the animations.
cake.addEventListener("mouseup", stopPlayingAlice, false);
bottle.addEventListener("mouseup", stopPlayingAlice, false);

Spezifikationen

Specification
Web Animations
# dom-animation-pause

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
pause

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Siehe auch