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 März 2020.
Die pause()
Methode der Animation
-Schnittstelle der Web Animations API unterbricht die Wiedergabe der Animation.
Syntax
pause()
Parameter
Keine.
Rückgabewert
Keine.
Ausnahmen
InvalidStateError
DOMException
-
Wird ausgelöst, wenn die
currentTime
der Animationunresolved
ist (möglicherweise hat sie noch nicht begonnen zu spielen) und die Endzeit der Animation positive Unendlichkeit ist.
Beispiel
Animation.pause()
wird häufig im Alice in Web Animations API Land Wachsend/Schrumpfend Alice Spiel verwendet, vor allem weil Animationen, die mit der Element.animate()
Methode erstellt werden, sofort zu spielen beginnen und manuell pausiert werden müssen, wenn Sie dies vermeiden möchten:
// 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:
// 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);
bottle.addEventListener("mouseup", stopPlayingAlice);
Spezifikationen
Specification |
---|
Web Animations> # dom-animation-pause> |
Browser-Kompatibilität
Loading…
Siehe auch
- Web Animations API
Animation
für weitere Methoden und Eigenschaften, die Sie zur Steuerung von Webseitenanimationen verwenden können.Animation.reverse()
um eine Animation rückwärts abzuspielen.Animation.finish()
um eine Animation zu beenden.Animation.cancel()
um eine Animation abzubrechen.