I just started digging into JS myself, so there are, of course, better ways to do this, but what about this one - Demo (Unprefixed version).
CSS
#box { position: relative; top: 20px; left: 20px; width: 100px; height: 100px; background: #393939; animation: move 2s linear infinite; animation-play-state: running; } @keyframes move { 100% { transform: rotate(360deg); } }
JS:
(function () { var box = document.getElementById('box'); box.onclick = function () { if (box.style.animationPlayState === "paused") { box.style.animationPlayState = "running"; } else { box.style.animationPlayState = "paused"; } }; })();
Based on the fact that the animation is started or paused, I change the animation-play-state onclick.
EDIT: added css code.
source share