I would like to start with a paused CSS animation then use jQuery to run the animation for a second before pausing again.
The css is below:
#animation {
-webkit-animation: one 5s infinite;
-webkit-animation-play-state: paused;
}
@-webkit-keyframes one {
0% { .... }
.....
100% { .... }
}
It has some complicated keyframes and the aim is that it would play for 1 second to the 20% position then stop.
I tried the jQuery below but it didn't work:
$("#click").click(function(){
$("#animation").css("-webkit-animation-play-state", "running").delay(1000).css("-webkit-animation-play-state", "paused");
});
(#click is the div I want to use as the trigger for the animation)
If I remove the delay and subsequent pause it works fine but obviously continues looping.
eg:
$("#click").click(function(){
$("#animation").css("-webkit-animation-play-state", "running");
});
What would you fine people suggest?
CSS helps to animate HTML elements without using JavaScript. You can play and pause the animation applied to HTML elements using animation-play-state property of CSS.
Use a transition time of 0.6s when you hover and an animation time of 0.01 when you hover off. This way, the animation will reset itself to its original position pretty much immediately and stop the funky behaviour.
The jQuery library provides several techniques for adding animation to a web page. These include simple, standard animations that are frequently used, and the ability to craft sophisticated custom effects.
The key to restarting a CSS animation is to set the animation-name of an animation to 'none' and then setting it back to the original animation.
jQuery delay() does not function when you are manipulating the css property. Use setTimeOut()
instead
$("#click").click(function(){
$("#animation").css("-webkit-animation-play-state", "running");
setTimeout(function() {
$("#animation").css("-webkit-animation-play-state", "paused");
}, 1000);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With