vanillajs-deck/js/animator.js

71 lines
2.1 KiB
JavaScript
Raw Normal View History

2019-11-24 00:02:31 +00:00
// @ts-check
/**
* Handles animations and transitions
* @property {boolean} _transitioning True when animation in effect
* @property {string} _begin The beginning animation class
* @property {string} _end The ending animation class
*/
2019-11-23 20:07:22 +00:00
export class Animator {
2019-11-24 00:02:31 +00:00
/**
* Create an instance of the animation helper
*/
2019-11-23 20:07:22 +00:00
constructor() {
this._transitioning = false;
this._begin = null;
this._end = null;
}
2019-11-24 00:02:31 +00:00
/**
* True when animation in effect
* @returns {boolean}
*/
2019-11-23 20:07:22 +00:00
get transitioning() {
return this._transitioning;
}
2019-11-24 00:02:31 +00:00
/**
* True when ready to complete second part of animation
* @returns {boolean}
*/
2019-11-23 20:07:22 +00:00
get animationReady() {
return !!this._end;
}
2019-11-24 00:02:31 +00:00
/**
* Kicks off a new animation (old slide)
* @param {string} animationName Name of the animation
* @param {HTMLDivElement} host The div to be animated
* @param {Function} callback Function to call when the animation completes
*/
2019-11-23 20:07:22 +00:00
beginAnimation(animationName, host, callback) {
this._transitioning = true;
this._begin = `anim-${animationName}-begin`;
this._end = `anim-${animationName}-end`;
const animationEnd = () => {
host.removeEventListener("animationend", animationEnd);
host.classList.remove(this._begin);
this._transitioning = false;
callback();
}
host.addEventListener("animationend", animationEnd, false);
host.classList.add(this._begin);
}
2019-11-24 00:02:31 +00:00
/**
* Kicks off the final animation (new slide)
* @param {HTMLDivElement} host The div to animate
*/
2019-11-23 20:07:22 +00:00
endAnimation(host) {
this._transitioning = true;
const animationEnd = () => {
host.removeEventListener("animationend", animationEnd);
host.classList.remove(this._end);
this._transitioning = false;
this._begin = null;
this._end = null;
}
host.addEventListener("animationend", animationEnd, false);
host.classList.add(this._end);
}
}