<h1 id="logo" class="run-animation">
Fancy Text (click to restart)
</h1>
@keyframes my-animation {
from {
opacity : 0;
left : -500px;
}
to {
opacity : 1;
left : 0;
}
}
.run-animation {
position: relative;
animation: my-animation 2s ease;
}
#logo {
text-align: center;
font-family : Palatino Linotype, Book Antiqua, Palatino, serif;
}
View Compiled
// This changes everything
"use strict";
// retrieve the element
var 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.
// This was, from the original tutorial, will no work in strict mode. Thanks Felis Phasma! The next uncommented line is the fix.
// element.offsetWidth = element.offsetWidth;
void element.offsetWidth;
// -> and re-adding the class
element.classList.add("run-animation");
}, false);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.