/ Gists / Restart CSS Animation
On gists

Restart CSS Animation

CSS CSS trick

restart.js Raw #

// https://css-tricks.com/restart-css-animation/

// retrieve the element
element = document.getElementById("logo");

// reset the transition by...
element.addEventListener("click", function(e) {
  e.preventDefault;
  
  // -> removing the class
  element.classList.remove("run-animation");
  
  // -> triggering reflow /* The actual magic */
  // without this it wouldn't work. Try uncommenting the line and the transition won't be retriggered.
  // Oops! This won't work in strict mode. Thanks Felis Phasma!
  // element.offsetWidth = element.offsetWidth;
  // Do this instead:
  void element.offsetWidth;
  
  // -> and re-adding the class
  element.classList.add("run-animation");
}, false);


// other solution
var elem = this;
elem.className = '';
setTimeout(function () {
  elem.className = 'run-animation';
}, 0);