forked from Codeinwp/Ideal-Image-Slider-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathideal-image-slider.min.js
2 lines (2 loc) · 15.9 KB
/
ideal-image-slider.min.js
1
2
/*! Ideal Image Slider v1.2.0 */
var IdealImageSlider=function(){"use strict";var a=function(a){a=a||{};for(var b=1;b<arguments.length;b++){var c=arguments[b];if(c)for(var d in c)c.hasOwnProperty(d)&&("object"==typeof c[d]?deepExtend(a[d],c[d]):a[d]=c[d])}return a},b=function(a,b){return b?a.classList?a.classList.contains(b):new RegExp("(^| )"+b+"( |$)","gi").test(a.className):!1},c=function(a,b){b&&(a.classList?a.classList.add(b):a.className+=" "+b)},d=function(a,b){b&&(a.classList?a.classList.remove(b):a.className=a.className.replace(new RegExp("(^|\\b)"+b.split(" ").join("|")+"(\\b|$)","gi")," "))},e=function(a){return Array.prototype.slice.call(a)},f=function(a,b){if(!a.style.backgroundImage){var c=new Image;c.setAttribute("src",a.getAttribute("data-src")),c.onload=function(){a.style.backgroundImage="url("+a.getAttribute("data-src")+")","function"==typeof b&&b(this)}}},g=function(){var a="(-webkit-min-device-pixel-ratio: 1.5),(min--moz-device-pixel-ratio: 1.5),(-o-min-device-pixel-ratio: 3/2),(min-resolution: 1.5dppx)";return window.devicePixelRatio>1?!0:window.matchMedia&&window.matchMedia(a).matches?!0:!1},h=function(a,b,c){a.style.webkitTransitionDuration=a.style.MozTransitionDuration=a.style.msTransitionDuration=a.style.OTransitionDuration=a.style.transitionDuration=c+"ms",a.style.webkitTransform=a.style.MozTransform=a.style.msTransform=a.style.OTransform="translateX("+b+"px)"},i=function(a){a.style.removeProperty("-webkit-transition-duration"),a.style.removeProperty("-moz-transition-duration"),a.style.removeProperty("-ms-transition-duration"),a.style.removeProperty("-o-transition-duration"),a.style.removeProperty("transition-duration"),a.style.removeProperty("-webkit-transform"),a.style.removeProperty("-moz-transform"),a.style.removeProperty("-ms-transform"),a.style.removeProperty("-o-transform"),a.style.removeProperty("transform")},j={vars:{start:{},delta:{},isScrolling:void 0,direction:null},start:function(a){if(!b(this._attributes.container,this.settings.classes.animating)){var d=a.touches[0];j.vars.start={x:d.pageX,y:d.pageY,time:+new Date},j.vars.delta={},j.vars.isScrolling=void 0,j.vars.direction=null,this.stop(),this.settings.beforeChange.apply(this),c(this._attributes.container,this.settings.classes.touching)}},move:function(a){if(!b(this._attributes.container,this.settings.classes.animating)&&!(a.touches.length>1||a.scale&&1!==a.scale)){var c=a.touches[0];j.vars.delta={x:c.pageX-j.vars.start.x,y:c.pageY-j.vars.start.y},"undefined"==typeof j.vars.isScrolling&&(j.vars.isScrolling=!!(j.vars.isScrolling||Math.abs(j.vars.delta.x)<Math.abs(j.vars.delta.y))),j.vars.isScrolling||(a.preventDefault(),h(this._attributes.previousSlide,j.vars.delta.x-this._attributes.previousSlide.offsetWidth,0),h(this._attributes.currentSlide,j.vars.delta.x,0),h(this._attributes.nextSlide,j.vars.delta.x+this._attributes.currentSlide.offsetWidth,0))}},end:function(){if(!b(this._attributes.container,this.settings.classes.animating)){var a=+new Date-j.vars.start.time,e=Number(a)<250&&Math.abs(j.vars.delta.x)>20||Math.abs(j.vars.delta.x)>this._attributes.currentSlide.offsetWidth/2,f=j.vars.delta.x<0?"next":"previous",g=this.settings.transitionDuration?this.settings.transitionDuration/2:0;j.vars.isScrolling||(e?(j.vars.direction=f,"next"==j.vars.direction?(h(this._attributes.currentSlide,-this._attributes.currentSlide.offsetWidth,g),h(this._attributes.nextSlide,0,g)):(h(this._attributes.previousSlide,0,g),h(this._attributes.currentSlide,this._attributes.currentSlide.offsetWidth,g)),setTimeout(j.transitionEnd.bind(this),g)):"next"==f?(h(this._attributes.currentSlide,0,g),h(this._attributes.nextSlide,this._attributes.currentSlide.offsetWidth,g)):(h(this._attributes.previousSlide,-this._attributes.previousSlide.offsetWidth,g),h(this._attributes.currentSlide,0,g)),g&&(c(this._attributes.container,this.settings.classes.animating),setTimeout(function(){d(this._attributes.container,this.settings.classes.animating)}.bind(this),g)))}},transitionEnd:function(){if(j.vars.direction){i(this._attributes.previousSlide),i(this._attributes.currentSlide),i(this._attributes.nextSlide),d(this._attributes.container,this.settings.classes.touching),d(this._attributes.previousSlide,this.settings.classes.previousSlide),d(this._attributes.currentSlide,this.settings.classes.currentSlide),d(this._attributes.nextSlide,this.settings.classes.nextSlide),this._attributes.currentSlide.setAttribute("aria-hidden","true");var a=this._attributes.slides,b=a.indexOf(this._attributes.currentSlide);"next"==j.vars.direction?(this._attributes.previousSlide=this._attributes.currentSlide,this._attributes.currentSlide=a[b+1],this._attributes.nextSlide=a[b+2],"undefined"==typeof this._attributes.currentSlide&&"undefined"==typeof this._attributes.nextSlide?(this._attributes.currentSlide=a[0],this._attributes.nextSlide=a[1]):"undefined"==typeof this._attributes.nextSlide&&(this._attributes.nextSlide=a[0]),f(this._attributes.nextSlide)):(this._attributes.nextSlide=this._attributes.currentSlide,this._attributes.previousSlide=a[b-2],this._attributes.currentSlide=a[b-1],"undefined"==typeof this._attributes.currentSlide&&"undefined"==typeof this._attributes.previousSlide?(this._attributes.currentSlide=a[a.length-1],this._attributes.previousSlide=a[a.length-2]):"undefined"==typeof this._attributes.previousSlide&&(this._attributes.previousSlide=a[a.length-1]),f(this._attributes.previousSlide)),c(this._attributes.previousSlide,this.settings.classes.previousSlide),c(this._attributes.currentSlide,this.settings.classes.currentSlide),c(this._attributes.nextSlide,this.settings.classes.nextSlide),this._attributes.currentSlide.setAttribute("aria-hidden","false"),this.settings.afterChange.apply(this)}}},k=function(d){this.settings={selector:"",height:400,interval:4e3,transitionDuration:700,effect:"slide",disableNav:!1,keyboardNav:!0,previousNavSelector:"",nextNavSelector:"",classes:{container:"ideal-image-slider",slide:"iis-slide",previousSlide:"iis-previous-slide",currentSlide:"iis-current-slide",nextSlide:"iis-next-slide",previousNav:"iis-previous-nav",nextNav:"iis-next-nav",animating:"iis-is-animating",touchEnabled:"iis-touch-enabled",touching:"iis-is-touching",directionPrevious:"iis-direction-previous",directionNext:"iis-direction-next"},onInit:function(){},onStart:function(){},onStop:function(){},onDestroy:function(){},beforeChange:function(){},afterChange:function(){}},"string"==typeof d?this.settings.selector=d:"object"==typeof d&&a(this.settings,d);var h=document.querySelector(this.settings.selector);if(!h)return null;var i=e(h.children),k=[];h.innerHTML="",Array.prototype.forEach.call(i,function(b){if(b instanceof HTMLImageElement||b instanceof HTMLAnchorElement){var d=document.createElement("a"),e="",f="";if(b instanceof HTMLAnchorElement){e=b.getAttribute("href"),f=b.getAttribute("target");var i=b.querySelector("img");if(null===i)return;b=i}"undefined"!=typeof b.dataset?(a(d.dataset,b.dataset),d.dataset.src=b.dataset.src?b.dataset.src:b.src,g()&&b.dataset["src-2x"]&&(d.dataset.src=b.dataset["src-2x"])):b.getAttribute("data-src")?d.setAttribute("data-src",b.getAttribute("data-src")):d.setAttribute("data-src",b.getAttribute("src")),e&&d.setAttribute("href",e),f&&d.setAttribute("target",f),b.getAttribute("className")&&c(d,b.getAttribute("className")),b.getAttribute("id")&&d.setAttribute("id",b.id),b.getAttribute("alt")&&(d.innerHTML=b.getAttribute("alt")),d.setAttribute("role","tabpanel"),d.setAttribute("aria-hidden","true"),d.style.cssText+="-webkit-transition-duration:"+this.settings.transitionDuration+"ms;-moz-transition-duration:"+this.settings.transitionDuration+"ms;-o-transition-duration:"+this.settings.transitionDuration+"ms;transition-duration:"+this.settings.transitionDuration+"ms;",h.appendChild(d),k.push(d)}}.bind(this));var l=k;if(l.length<=1)return h.innerHTML="",Array.prototype.forEach.call(i,function(a){h.appendChild(a)}),null;if(!this.settings.disableNav){var m,n;this.settings.previousNavSelector?m=document.querySelector(this.settings.previousNavSelector):(m=document.createElement("a"),h.appendChild(m)),this.settings.nextNavSelector?n=document.querySelector(this.settings.nextNavSelector):(n=document.createElement("a"),h.appendChild(n)),c(m,this.settings.classes.previousNav),c(n,this.settings.classes.nextNav),m.addEventListener("click",function(){return b(this._attributes.container,this.settings.classes.animating)?!1:(this.stop(),void this.previousSlide())}.bind(this)),n.addEventListener("click",function(){return b(this._attributes.container,this.settings.classes.animating)?!1:(this.stop(),void this.nextSlide())}.bind(this)),("ontouchstart"in window||window.DocumentTouch&&document instanceof DocumentTouch)&&(this.settings.effect="slide",m.style.display="none",n.style.display="none",c(h,this.settings.classes.touchEnabled),h.addEventListener("touchstart",j.start.bind(this),!1),h.addEventListener("touchmove",j.move.bind(this),!1),h.addEventListener("touchend",j.end.bind(this),!1)),this.settings.keyboardNav&&document.addEventListener("keyup",function(a){a=a||window.event;var c="number"==typeof a.which?a.which:a.keyCode;if(37==c){if(b(this._attributes.container,this.settings.classes.animating))return!1;this.stop(),this.previousSlide()}else if(39==c){if(b(this._attributes.container,this.settings.classes.animating))return!1;this.stop(),this.nextSlide()}}.bind(this))}this._attributes={container:h,slides:l,previousSlide:"undefined"!=typeof l[l.length-1]?l[l.length-1]:l[0],currentSlide:l[0],nextSlide:"undefined"!=typeof l[1]?l[1]:l[0],timerId:0,origChildren:i},this.settings.height&&(this._attributes.container.style.height=this.settings.height+"px"),c(h,this.settings.classes.container),c(h,"iis-effect-"+this.settings.effect),Array.prototype.forEach.call(this._attributes.slides,function(a){c(a,this.settings.classes.slide)}.bind(this)),c(this._attributes.previousSlide,this.settings.classes.previousSlide),c(this._attributes.currentSlide,this.settings.classes.currentSlide),c(this._attributes.nextSlide,this.settings.classes.nextSlide),this._attributes.currentSlide.setAttribute("aria-hidden","false"),f(this._attributes.currentSlide,function(){this.settings.onInit.apply(this)}.bind(this)),f(this._attributes.previousSlide),f(this._attributes.nextSlide)};return k.prototype.get=function(a){return this._attributes?this._attributes.hasOwnProperty(a)?this._attributes[a]:void 0:null},k.prototype.set=function(a,b){return this._attributes?this._attributes[a]=b:null},k.prototype.start=function(){this._attributes&&(this._attributes.timerId=setInterval(this.nextSlide.bind(this),this.settings.interval),this.settings.onStart.apply(this),window.onblur=function(){this.stop()}.bind(this))},k.prototype.stop=function(){this._attributes&&(clearInterval(this._attributes.timerId),this._attributes.timerId=0,this.settings.onStop.apply(this))},k.prototype.previousSlide=function(){this.settings.beforeChange.apply(this),d(this._attributes.previousSlide,this.settings.classes.previousSlide),d(this._attributes.currentSlide,this.settings.classes.currentSlide),d(this._attributes.nextSlide,this.settings.classes.nextSlide),this._attributes.currentSlide.setAttribute("aria-hidden","true");var a=this._attributes.slides,b=a.indexOf(this._attributes.currentSlide);this._attributes.nextSlide=this._attributes.currentSlide,this._attributes.previousSlide=a[b-2],this._attributes.currentSlide=a[b-1],"undefined"==typeof this._attributes.currentSlide&&"undefined"==typeof this._attributes.previousSlide?(this._attributes.currentSlide=a[a.length-1],this._attributes.previousSlide=a[a.length-2]):"undefined"==typeof this._attributes.previousSlide&&(this._attributes.previousSlide=a[a.length-1]),f(this._attributes.previousSlide),c(this._attributes.previousSlide,this.settings.classes.previousSlide),c(this._attributes.currentSlide,this.settings.classes.currentSlide),c(this._attributes.nextSlide,this.settings.classes.nextSlide),this._attributes.currentSlide.setAttribute("aria-hidden","false"),c(this._attributes.container,this.settings.classes.directionPrevious),setTimeout(function(){d(this._attributes.container,this.settings.classes.directionPrevious)}.bind(this),this.settings.transitionDuration),this.settings.transitionDuration&&(c(this._attributes.container,this.settings.classes.animating),setTimeout(function(){d(this._attributes.container,this.settings.classes.animating)}.bind(this),this.settings.transitionDuration)),this.settings.afterChange.apply(this)},k.prototype.nextSlide=function(){this.settings.beforeChange.apply(this),d(this._attributes.previousSlide,this.settings.classes.previousSlide),d(this._attributes.currentSlide,this.settings.classes.currentSlide),d(this._attributes.nextSlide,this.settings.classes.nextSlide),this._attributes.currentSlide.setAttribute("aria-hidden","true");var a=this._attributes.slides,b=a.indexOf(this._attributes.currentSlide);this._attributes.previousSlide=this._attributes.currentSlide,this._attributes.currentSlide=a[b+1],this._attributes.nextSlide=a[b+2],"undefined"==typeof this._attributes.currentSlide&&"undefined"==typeof this._attributes.nextSlide?(this._attributes.currentSlide=a[0],this._attributes.nextSlide=a[1]):"undefined"==typeof this._attributes.nextSlide&&(this._attributes.nextSlide=a[0]),f(this._attributes.nextSlide),c(this._attributes.previousSlide,this.settings.classes.previousSlide),c(this._attributes.currentSlide,this.settings.classes.currentSlide),c(this._attributes.nextSlide,this.settings.classes.nextSlide),this._attributes.currentSlide.setAttribute("aria-hidden","false"),c(this._attributes.container,this.settings.classes.directionNext),setTimeout(function(){d(this._attributes.container,this.settings.classes.directionNext)}.bind(this),this.settings.transitionDuration),this.settings.transitionDuration&&(c(this._attributes.container,this.settings.classes.animating),setTimeout(function(){d(this._attributes.container,this.settings.classes.animating)}.bind(this),this.settings.transitionDuration)),this.settings.afterChange.apply(this)},k.prototype.gotoSlide=function(a){this.settings.beforeChange.apply(this),this.stop(),d(this._attributes.previousSlide,this.settings.classes.previousSlide),d(this._attributes.currentSlide,this.settings.classes.currentSlide),d(this._attributes.nextSlide,this.settings.classes.nextSlide),this._attributes.currentSlide.setAttribute("aria-hidden","true"),a--;var b=this._attributes.slides,e=b.indexOf(this._attributes.currentSlide);this._attributes.previousSlide=b[a-1],this._attributes.currentSlide=b[a],this._attributes.nextSlide=b[a+1],"undefined"==typeof this._attributes.previousSlide&&(this._attributes.previousSlide=b[b.length-1]),"undefined"==typeof this._attributes.nextSlide&&(this._attributes.nextSlide=b[0]),f(this._attributes.previousSlide),f(this._attributes.currentSlide),f(this._attributes.nextSlide),c(this._attributes.previousSlide,this.settings.classes.previousSlide),c(this._attributes.currentSlide,this.settings.classes.currentSlide),c(this._attributes.nextSlide,this.settings.classes.nextSlide),this._attributes.currentSlide.setAttribute("aria-hidden","false"),e>a?(c(this._attributes.container,this.settings.classes.directionPrevious),setTimeout(function(){d(this._attributes.container,this.settings.classes.directionPrevious)}.bind(this),this.settings.transitionDuration)):(c(this._attributes.container,this.settings.classes.directionNext),setTimeout(function(){d(this._attributes.container,this.settings.classes.directionNext)}.bind(this),this.settings.transitionDuration)),this.settings.transitionDuration&&(c(this._attributes.container,this.settings.classes.animating),setTimeout(function(){d(this._attributes.container,this.settings.classes.animating)}.bind(this),this.settings.transitionDuration)),this.settings.afterChange.apply(this)},k.prototype.destroy=function(){clearInterval(this._attributes.timerId),this._attributes.timerId=0,this._attributes.container.innerHTML="",Array.prototype.forEach.call(this._attributes.origChildren,function(a){this._attributes.container.appendChild(a)}.bind(this)),d(this._attributes.container,this.settings.classes.container),d(this._attributes.container,"iis-effect-"+this.settings.effect),this._attributes.container.style.height="",this.settings.onDestroy.apply(this)},{_hasClass:b,_addClass:c,_removeClass:d,Slider:k}}();