simple-slideshow
Version:
Simple slideshow library in VanillaJS with no dependencies.
1 lines • 1.59 kB
JavaScript
!function(e,t){"function"==typeof define&&define.amd?define([],t):e.SlideShow=t()}(this,function(){function h(e,t,i){var n=document.createElement(e);return n.className=t,i&&n.appendChild(document.createTextNode(i)),n}function e(e,t){if(t={timeout:t&&t.timeout||5e3},this.element=document.querySelector(e),!this.element||!this.element.children.length)throw new Error("Element not found or no children.");this.element.classList.add("slideshow"),this.element.classList.add("preload");var i=this.element.querySelector("img"),n=function(){this.element.style.height=i.clientHeight+"px"}.bind(this);n(),window.addEventListener("resize",n),this._captions=[];for(var s=0;s<this.element.children.length;s++){var l,r=this.element.children[s];(i=r.querySelector("img"))&&i.title&&((l=h("div","caption")).appendChild(h("span","title",i.title)),l.appendChild(h("span","alt",i.alt)),r.appendChild(l),this._captions.push(l))}this.currentSlide=this.element.children[0],this.currentSlide.classList.add("show-animation"),setTimeout(function(){this.element.classList.remove("preload")}.bind(this));var o=0;this._ticker=setInterval(function(){this.currentSlide.classList.remove("show-animation"),o=(o+1)%this.element.children.length,this.currentSlide=this.element.children[o],this.currentSlide.classList.add("show-animation")}.bind(this),t.timeout)}return e.prototype={constructor:e,destroy:function(){clearInterval(this._ticker),this.currentSlide.classList.remove("show-animation");for(var e=0;e<this._captions.length;e++)this._captions[e].parentNode.removeChild(this._captions[e]);this._captions=[]}},e});