
Unfortunately, at this time, there is no way to get the exact current “percentage completed” of a CSS keyframe animation. Obtaining the Current Keyvalue Percentage In order to do that, we have to get a bit more involved. You can’t pause it, transform it, resume it, and expect it to run fluidly from the new transformed state. However, when a CSS animation is paused using animation-play-state, the element is prevented from transforming the same way it is when an animation is running. You can change that CSS through JavaScript like this (mind your prefixes): = "paused" Į = "running" The animation-play-state property of CSS is incredibly helpful when you simply need to pause an animation and potentially continue it later. But what happens if you want to change the CSS animation mid-animation? This requires a bit of trickery! The animation-play-state Property Like we just learned, we can watch elements and react to animation-related events: animationStart, animationIteration, and animationEnd.

It removes the jump by letting the animation complete before applying the new state. Unless you hover over it at the perfect time, it will jump to a particular state before enlarging to the final hovered state.

The idea in this demo is to enlarge the heart and stop the animation when it is hovered over. And here is another one determining which animation (name) the event is firing for. Here is his useful article on capturing CSS animations with JavaScript. These events need to be vendor prefixed at this time, so in this demo, we use a function developed by Craig Buckler called PrefixedEvent, which has the parameters element, type, and callback to help make these events cross-browser. These animation events fire when the animation on an element ends, starts, or completes one iteration, respectively. Like: animationend, animationstart, and animationiteration for animations and transitionend for transitions. Some of the most useful yet little-known JavaScript tricks for manipulating CSS transitions and animations are the DOM events they fire. Rodney Rehm also wrote a particularly helpful and informational article on CSS transitions which can be found here. There are many different CSS properties that can be transitioned or animated, typically one that have numeric or color values. Note we’re changing background-size this time. The following is an example of that approach. Then set those CSS properties of that element equal to those values you just got. To pause an element’s transition, use getComputedStyle and getPropertyValue at the point in the transition you want to pause it. To trigger an element’s transition, toggle a class name on that element that triggers it. The solution is actually quite simple using JavaScript. There are countless questions on coding forums related to triggering and pausing an element’s transition. In this article, we will cover each of them separately. They offer more fine-grained control as you can control different stops of the animations.

When applied, they just run and do their thing. Transitions in CSS are applied to an element and specify that when a property changes it should do so over gradually over over a period of time. Let’s jump straight in! Quick Note: Animations and Transitions are Different While CSS animation does have some limitations, most of the time it’s more capable than we give it credit for! Not to mention, typically more performant.Ĭoupled with a touch of JavaScript, CSS animations and transitions are able to accomplish hardware-accelerated animations and interactions more efficiently than most JavaScript libraries. Web designers sometimes believe that animating in CSS is more difficult than animating in JavaScript. I’ve had this on my list to write about for way too long, so I was happy to let Zach dig into it and write up this comprehensive tutorial. Zach wrote to me telling me that, as a frequenter on coding forums like Stack Overflow, he sees the questions come up all the time about controlling CSS animations with JavaScript, and proved it with a bunch of links. The following is a guest post by Zach Saucier.
