When it comes to animations on the web, developers need to measure the animation’s requirements with the right technology — CSS or JavaScript. Many animations are manageable with CSS but JavaScript will always provide more control. With document.getAnimations
, however, you can use JavaScript to manage CSS animations!
The document.getAnimations
method returns an array of CSSAnimation
objects. CSSAnimation
provides a host of information about the animation: playState
, timeline
, effect
, and events like onfinish
. You can then modify those objects to adjust animations:
// Make all CSS animations on the page twice as fast document.getAnimations().forEach((animation) => animation.playbackRate *= 2; ); // Stop all CSS animations on the page document.getAnimations().forEach((animation) => animation.cancel(); );
How could adjusting CSS animations on the fly be useful to developers? Maybe use the Battery API to stop all animations when the device battery is low. Possibly to stop animations when the user has scrolled past the animation itself.
I love the way you can use JavaScript to modify CSS animations. Developers used to need to choose between CSS and JavaScript — now we have the tools to make them work together!
Regular Expressions for the Rest of Us
Sooner or later you’ll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In…
Create Twitter-Style Buttons with the Dojo Toolkit
I love that JavaScript toolkits make enhancing web pages incredibly easy. Today I’ll cover an effect that I’ve already coded with MooTools: creating a Twitter-style animated “Sign In” button. Check out this five minute tutorial so you can take your static…
Record Text Selections Using MooTools or jQuery AJAX
One technique I’m seeing more and more these days (CNNSI.com, for example) is AJAX recording of selected text. It makes sense — if you detect users selecting the terms over and over again, you can probably assume your visitors are searching that term on Google…
Source link