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) · 10.5 KB
/
ideal-image-slider.min.js
1
2
/*! Ideal Image Slider v1.0.2 */
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){var c=document.createEvent("CustomEvent");return c.initCustomEvent(b,!0,!0,a.target),a.target.dispatchEvent(c),c=null,!1},i=!0,j={x:0,y:0},k={x:0,y:0},l={touchstart:function(a){j={x:a.touches[0].pageX,y:a.touches[0].pageY}},touchmove:function(a){i=!1,k={x:a.touches[0].pageX,y:a.touches[0].pageY}},touchend:function(a){if(i)h(a,"fc");else{var b=k.x-j.x,c=Math.abs(b),d=k.y-j.y,e=Math.abs(d);Math.max(c,e)>20&&h(a,c>e?0>b?"swl":"swr":0>d?"swu":"swd")}i=!0},touchcancel:function(){i=!1}};for(var m in l)document.addEventListener(m,l[m],!1);var n=function(d){this.settings={selector:"",height:400,interval:4e3,transitionDuration:700,effect:"slide",disableNav:!1,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",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;h.setAttribute("role","listbox");var i=e(h.children),j=[];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","option"),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),j.push(d)}}.bind(this));var k=j;if(k.length<=1)return null;if(!this.settings.disableNav){var l,m;this.settings.previousNavSelector?l=document.querySelector(this.settings.previousNavSelector):(l=document.createElement("a"),h.appendChild(l)),this.settings.nextNavSelector?m=document.querySelector(this.settings.nextNavSelector):(m=document.createElement("a"),h.appendChild(m)),c(l,this.settings.classes.previousNav),c(m,this.settings.classes.nextNav),l.addEventListener("click",function(){return b(this._attributes.container,this.settings.classes.animating)?!1:(this.stop(),void this.previousSlide())}.bind(this)),m.addEventListener("click",function(){return b(this._attributes.container,this.settings.classes.animating)?!1:(this.stop(),void this.nextSlide())}.bind(this)),"ontouchstart"in document.documentElement&&(l.style.display="none",m.style.display="none",h.addEventListener("swr",function(){return b(this._attributes.container,this.settings.classes.animating)?!1:(this.stop(),void this.previousSlide())}.bind(this),!1),h.addEventListener("swl",function(){return b(this._attributes.container,this.settings.classes.animating)?!1:(this.stop(),void this.nextSlide())}.bind(this),!1))}this._attributes={container:h,slides:k,previousSlide:"undefined"!=typeof k[k.length-1]?k[k.length-1]:k[0],currentSlide:k[0],nextSlide:"undefined"!=typeof k[1]?k[1]:k[0],timerId:0,origChildren:i,touchX:null,touchY:null,touchIsMoving:!1},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),f(this._attributes.currentSlide,function(){this.settings.onInit.apply(this)}.bind(this)),f(this._attributes.previousSlide),f(this._attributes.nextSlide)};return n.prototype.get=function(a){return this._attributes.hasOwnProperty(a)?this._attributes[a]:void 0},n.prototype.set=function(a,b){return this._attributes[a]=b},n.prototype.start=function(){this._attributes.timerId=setInterval(this.nextSlide.bind(this),this.settings.interval),this.settings.onStart.apply(this),window.onblur=function(){this.stop()}.bind(this)},n.prototype.stop=function(){clearInterval(this._attributes.timerId),this._attributes.timerId=0,this.settings.onStop.apply(this)},n.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);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),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)},n.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);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),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)},n.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),a--;var b=this._attributes.slides;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.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)},n.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)},{Slider:n}}();