vanillajs-deck/js/animator.js

80 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2019-11-24 00:02:31 +00:00
// @ts-check
/**
* Handles animations and transitions
*/
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() {
2019-11-25 19:34:32 +00:00
/**
* True when an animation is in effect
* @type {boolean}
*/
2019-11-23 20:07:22 +00:00
this._transitioning = false;
2019-11-25 19:34:32 +00:00
/**
* The name of the beginning animation
* @type {string}
*/
2019-11-23 20:07:22 +00:00
this._begin = null;
2019-11-25 19:34:32 +00:00
/**
* The name of the ending animation
* @type {string}
*/
2019-11-23 20:07:22 +00:00
this._end = null;
}
2019-11-24 00:02:31 +00:00
/**
2019-11-25 19:34:32 +00:00
* True when animation is in effect
* @returns {boolean} True if animation is happening
2019-11-24 00:02:31 +00:00
*/
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
2019-11-25 19:34:32 +00:00
* @returns {boolean} True if there is a second animation to fire
2019-11-24 00:02:31 +00:00
*/
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);
}
}