JavaScript: Restart all Animations of an Element

Recently built a demo that demonstrated a specific animation. Only problem: if you missed it, you had no way of restarting it. Instead of forcing the visitor to reload the page, this little JavaScript-snippet – attached to a button click – did the trick: const restartAnimations = ($el) => { $el.getAnimations().forEach((anim) => { anim.cancel(); anim.play(); …


This content originally appeared on Bram.us and was authored by Bramus!

Recently built a demo that demonstrated a specific animation. Only problem: if you missed it, you had no way of restarting it. Instead of forcing the visitor to reload the page, this little JavaScript-snippet – attached to a button click – did the trick:

const restartAnimations = ($el) => {
	$el.getAnimations().forEach((anim) => {
		anim.cancel();
		anim.play();
	});
};

Pass in an Element, and it will will loops all attached animations bound to it. For each animation it restarts them by triggering a cancel + play.

~

Demo

See the Pen
JavaScript Restart Animations
by Bramus (@bramus)
on CodePen.

~

🔥 Like what you see? Want to stay in the loop? Here's how:


This content originally appeared on Bram.us and was authored by Bramus!


Print Share Comment Cite Upload Translate Updates
APA

Bramus! | Sciencx (2022-07-20T20:16:58+00:00) JavaScript: Restart all Animations of an Element. Retrieved from https://www.scien.cx/2022/07/20/javascript-restart-all-animations-of-an-element/

MLA
" » JavaScript: Restart all Animations of an Element." Bramus! | Sciencx - Wednesday July 20, 2022, https://www.scien.cx/2022/07/20/javascript-restart-all-animations-of-an-element/
HARVARD
Bramus! | Sciencx Wednesday July 20, 2022 » JavaScript: Restart all Animations of an Element., viewed ,<https://www.scien.cx/2022/07/20/javascript-restart-all-animations-of-an-element/>
VANCOUVER
Bramus! | Sciencx - » JavaScript: Restart all Animations of an Element. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/20/javascript-restart-all-animations-of-an-element/
CHICAGO
" » JavaScript: Restart all Animations of an Element." Bramus! | Sciencx - Accessed . https://www.scien.cx/2022/07/20/javascript-restart-all-animations-of-an-element/
IEEE
" » JavaScript: Restart all Animations of an Element." Bramus! | Sciencx [Online]. Available: https://www.scien.cx/2022/07/20/javascript-restart-all-animations-of-an-element/. [Accessed: ]
rf:citation
» JavaScript: Restart all Animations of an Element | Bramus! | Sciencx | https://www.scien.cx/2022/07/20/javascript-restart-all-animations-of-an-element/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.