Hızlı bir özet
Buradaki yaklaşım, karmaşık JS kodlarına güvenmek yerine, öge’deki CSS animasyonlarını tetikleyen bir sınıf eklemeyi/çıkarmayı içerir. Ki bundan daha önce bahsetmiştik.
<h1 id="elem">Animate</h1>
<button class="show">Show</button>
<button class="hide">Hide</button>
var elem = document.querySelector('#elem');
document.addEventListener('click', function (event) {
if (event.target.matches('.show')) {
elem.removeAttribute('hidden');
elem.classList.add('fadeInDown');
}
if (event.target.matches('.hide')) {
elem.setAttribute('hidden', 'true');
elem.classList.remove('fadeInDown');
}
}, false);
Getting Full Setup
* Apply a CSS animation to an element
* @param {Node} elem The element to animate
* @param {String} animation The type of animation to apply
* @param {Boolean} hide If true, apply the [hidden] attribute after the animation is done
*/
var animate = function (elem, animation, hide) {
// If there's no element or animation, do nothing
if (!elem || !animation) return;
// Remove the [hidden] attribute
elem.removeAttribute('hidden');
// Apply the animation
elem.classList.add(animation);
// Detect when the animation ends
elem.addEventListener('animationend', function endAnimation (event) {
// Remove the animation class
elem.classList.remove(animation);
// If the element should be hidden, hide it
if (hide) {
elem.setAttribute('hidden', 'true');
}
// Remove this event listener
elem.removeEventListener('animationend', endAnimation, false);
}, false);
};