I tried google and am looking from this forum for a solution to my problem, but so far no luck. I would like to pause my CSS3 animation (image slide show) by clicking the image, and also return to the same animation by clicking the image.
I know how to pause the slide show, and I was also able to resume it once, but then it stops working if I try to pause and resume more than once. This is what my code looks like:
<!DOCTYPE html> <html> <head> <title></title> <style type="text/css"> .pic { position: absolute; opacity: 0; } #pic1 { -webkit-animation: pic1 4s infinite linear; } #pic2 { -webkit-animation: pic2 4s infinite linear; } @-webkit-keyframes pic1 { 0% {opacity: 0;} 5% {opacity: 1;} 45% {opacity: 1;} 50% {opacity: 0;} 100% {opacity: 0;} } @-webkit-keyframes pic2 { 0% {opacity: 0;} 50% {opacity: 0;} 55% {opacity: 1;} 95% {opacity: 1;} 100% {opacity: 0;} } </style> <script type="text/javascript"> function doStuff(){ var pic1 = document.getElementById("pic1"); var pic2 = document.getElementById("pic2"); pic1.style.webkitAnimationPlayState="paused"; pic2.style.webkitAnimationPlayState="paused"; pic1.onclick = function(){ pic1.style.webkitAnimationPlayState="running"; pic2.style.webkitAnimationPlayState="running"; } pic2.onclick = function(){ pic1.style.webkitAnimationPlayState="running"; pic2.style.webkitAnimationPlayState="running"; } } </script> </head> <body> <img id="pic1" class="pic" src="photo1.jpg" /> <img id="pic2" class="pic" src="photo2.jpg" onclick="doStuff()" /> </body> </html>
I do not want to use any JS libraries (e.g. jQuery) or any other external solution.
I assume that my functions inside the doStuff function still work, and therefore pause and resume only work once.
Is there a way to clear these functions after I clicked them once? Or am I trying to do this completely wrong? Help is appreciated. :)
javascript css3 animation resume
nqw1 Apr 27 2018-11-12T00: 00Z
source share