function transitionCompleted(event) {
event.target.removeEventListener('transitionend', transitionCompleted);
}
function removeElement(event) {
event.target.parentNode.removeChild(event.target);
}
const TRANSITION_TYPES = [
'crossfade',
'throughBlack',
'slideleft'
];
class Renderer {
/**
* @param {object} container - DOM Element to use to render templates in to
*/
constructor(container) {
this.container = container;
}
// should take the current slide, which is already rendered, and render
// the next slide. It then should transition to the next slide and destroy
// the current slide when completed.
transition(nextSlide, transitionType) {
if (!TRANSITION_TYPES.includes(transitionType)) {
throw new Error(`Transition type ${transitionType} is not valid`);
}
return new Promise((resolve, reject) => {
// Add the transition type as a class on the container so the css transitions will work
this.container.className = transitionType;
const currentSlideWrapper = document.querySelectorAll('.current');
// Create a container for the next slide
const nextSlideWrapper = document.createElement('div');
nextSlideWrapper.className = 'slide next';
nextSlideWrapper.appendChild(nextSlide.render());
//nextSlideWrapper.addEventListener('transitionend', transitionCompleted, false);
this.container.appendChild(nextSlideWrapper);
// Timeout needed so that the element gets added to the DOM and gets painted
// before we remove the class so that the browser the class change up.
setTimeout(() => {
if (currentSlideWrapper.length) {
const wrapper = currentSlideWrapper[0];
wrapper.addEventListener('transitionend', removeElement, false);
wrapper.className = 'slide previous';
}
nextSlideWrapper.className = 'slide current';
resolve(nextSlide);
}, 0);
});
}
}
const container =...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.