This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
abg-snapslider.min.js
1 lines (1 loc) · 11.3 KB
/
abg-snapslider.min.js
1
+function($){var s=function(s,t){return this.$element=$(s),this.$slider=this.$element.find("[data-snapslider-slider]"),this.$window=$(window),this.element=s,this.slider=this.$slider[0],this.options=t,this.slider?(this.sliderWidth=this.sliderItemCount=this.sliderItemIndex=this.sliderItemIndexTrans=this.sliderItemWidth=this.sliderItemsShown=this.sliderScrollLeft=this.sliderScrollDirection=this.sliderScrollExcess=this.sliderScrollTarget=this.sliderSnapTimeout=null,void this.initialise()):void(console&&console.warn&&console.warn("SnapSlider could not find a [data-snapslider-slider] and has not been enabled."))};s.DEFAULTS={ssEnablePagination:!0,ssEnablePagelist:!0,ssEnableZoom:!1,ssEnableZoomtopage:!1,ssDisableKeyboardEvents:!1,ssActivated:"ss--active",ssPaginationActivated:"ss--pagination-active",ssPagelistActivated:"ss--pagelist-active",ssPagelinksActivated:"ss--pagelinks-active",ssZoomActivated:"ss--zoom-active",ssBaseClass:"gallery",ssElementSeparator:"__",ssModifierSeparator:"--",ssGroupingSeparator:"-",ssPagerClass:"pager",ssPagerNextModifier:"next",ssPagerPrevModifier:"prev",ssPagerActiveModifier:"active",ssPagelistClass:"pagelist",ssPagelistListElement:"ul",ssPagelistItemElement:"li",ssPagelinksSelector:"",ssPagelinkClass:"pagelink",ssPagerContainerElement:"div",ssPagerElement:"a",ssPagerInnerElement:"span",ssPagerPrevHtml:"‹",ssPagerNextHtml:"›",ssHiddenModifier:"hidden",ssZoomModifier:"zoom",ssAnimDuration:250,ssScrollDelay:250,ssResizeDelay:100,ssSnapTolerance:.1,ssBgNormal:"",ssBgLarge:"",ssBgLargest:""},s.prototype.initialise=function(){this.$sliderItems=this.$slider.find("[data-snapslider-item]"),this.sliderItemCount=this.$sliderItems.length,this.sliderScrollLeft=this.slider.scrollLeft,this.sliderScrollDirection=0,this.sliderImageManagement=this.options.ssBgNormal&&this.options.ssBgLarge&&this.options.ssBgLargest,this.setItemWidth(),this.$slider.on("scroll",$.proxy(this.scroll,this)),this.$window.on("resize",$.proxy(this.resize,this)),this.options.ssEnablePagination&&this.enablePagination(),this.options.ssEnablePagelist&&this.enablePagelist(),this.options.ssPagelinksSelector&&this.enablePagelinks(),this.options.ssEnableZoom&&this.enableZoom(),!this.options.ssDisableKeyboardEvents&&this.enableKeyboardEvents(),this.sliderImageManagement&&this.setBackgroundImages(),this.snap(),this.$element.addClass(this.options.ssActivated)},s.prototype.enablePagination=function(){var s=this.options.ssBaseClass+this.options.ssElementSeparator+this.options.ssPagerClass+"s",t=this.options.ssBaseClass+this.options.ssElementSeparator+this.options.ssPagerClass,e=t+this.options.ssModifierSeparator+this.options.ssPagerPrevModifier,i=t+this.options.ssModifierSeparator+this.options.ssPagerNextModifier,n=document.createElement(this.options.ssPagerContainerElement),o=document.createElement(this.options.ssPagerElement),a=document.createElement(this.options.ssPagerElement);if("a"==this.options.ssPagerElement&&(o.href=a.href="#"),n.className=s,o.className=a.className=t,o.className+=" "+e,a.className+=" "+i,this.options.ssPagerInnerElement){var l=document.createElement(this.options.ssPagerInnerElement),r=document.createElement(this.options.ssPagerInnerElement);l.innerHTML=this.options.ssPagerPrevHtml,r.innerHTML=this.options.ssPagerNextHtml,o.appendChild(l),a.appendChild(r)}else o.innerHTML=this.options.ssPagerPrevHtml,a.innerHTML=this.options.ssPagerNextHtml;this.$sliderPagerPrev=$(o),this.$sliderPagerNext=$(a),$(o).on("click",$.proxy(function(s){s.preventDefault(),this.paginate(-1)},this)),$(a).on("click",$.proxy(function(s){s.preventDefault(),this.paginate(1)},this)),n.appendChild(o),n.appendChild(a),this.slider.parentNode.appendChild(n),this.setPaginationVisibility(),this.$element.addClass(this.options.ssPaginationActivated)},s.prototype.enablePagelist=function(){var s=this.options.ssBaseClass+this.options.ssElementSeparator+this.options.ssPagelistClass,t=s+this.options.ssGroupingSeparator+this.options.ssPagerClass,e=t+"s";this.sliderPagelistPagerActiveClass=t+this.options.ssModifierSeparator+this.options.ssPagerActiveModifier;for(var i=$('<table class="'+s+'"><tr><td><'+this.options.ssPagelistListElement+' class="'+e+'"></'+this.options.ssPagelistListElement+"></td></tr></table>"),n=i.find(this.options.ssPagelistListElement),o=0;o<this.sliderItemCount;o++){var a=$("<"+this.options.ssPagelistItemElement+' class="'+t+'"><'+this.options.ssPagerInnerElement+">"+(o+1)+"</"+this.options.ssPagerInnerElement+"></"+this.options.ssPagelistItemElement+">");a.on("click",$.proxy(this.goToPage,this,o)),n.append(a)}this.$pagelistItems=n.find("."+t),this.$slider.parent().append(i),this.setPagelistItemVisibility(),this.$element.addClass(this.options.ssPagelistActivated)},s.prototype.enablePagelinks=function(){if(this.$pagelinks=this.$element.find(this.options.ssPagelinksSelector),0==this.$pagelinks.length)return void(console&&console.warn&&console.warn("SnapSlider couldn't find any page links matching selector \""+this.options.ssPagelinksSelector+'". Pagelinks have not been enabled.'));if(this.$pagelinks.length!=this.sliderItemCount)return void(console&&console.warn&&console.warn("SnapSlider found "+this.$pagelinks.length+' page links matching selector "'+this.options.ssPagelinksSelector+'", but expected '+this.sliderItemCount+". Pagelinks have not been enabled."));for(var s=0;s<this.sliderItemCount;s++)$(this.$pagelinks[s]).on("click",function(s){s.preventDefault()}),$(this.$pagelinks[s]).on("click",$.proxy(this.goToPage,this,s));this.sliderPagelinkClass=this.options.ssBaseClass+this.options.ssElementSeparator+this.options.ssPagelinkClass,this.sliderPagelinkActiveClass=this.sliderPagelinkClass+this.options.ssModifierSeparator+this.options.ssPagerActiveModifier,this.sliderPagelinksEnabled=!0,this.setPagelinksActiveItem(),this.$pagelinks.addClass(this.sliderPagelinkClass),this.$element.addClass(this.options.ssPagelinksActivated)},s.prototype.enableZoom=function(){for(var s=0;s<this.sliderItemCount;s++)$(this.$sliderItems[s]).on("click",$.proxy(this.zoom,this,s));this.$slider.bind("webkitTransitionEnd oTransitionEnd otransitionend transitionend",$.proxy(this.transition,this)),this.sliderZoomClass=this.options.ssBaseClass+this.options.ssModifierSeparator+this.options.ssZoomModifier,this.sliderZoomed=this.$element.hasClass(this.sliderZoomClass),this.$element.addClass(this.options.ssZoomActivated)},s.prototype.enableKeyboardEvents=function(){!$("[data-snapslider]").attr("tabindex")&&$("[data-snapslider]").attr("tabindex","0"),this.$element.bind("keydown",$.proxy(this.keydown,this)),this.$element.bind("keyup",$.proxy(this.keyup,this))},s.prototype.scroll=function(){this.sliderScrollDirection=this.slider.scrollLeft>this.sliderScrollLeft?1:-1,this.sliderScrollLeft=this.slider.scrollLeft,this.setSnapTimeout(this.options.ssScrollDelay)},s.prototype.resize=function(){this.setItemWidth(),this.setSnapTimeout(this.options.ssResizeDelay)},s.prototype.transition=function(){this.sliderScrollLeft=this.slider.scrollLeft,this.setItemWidth(),this.options.ssEnableZoomtopage?this.goToPage(this.sliderItemIndexTrans):this.snap()},s.prototype.keydown=function(s){(37==s.keyCode||39==s.keyCode)&&s.preventDefault()},s.prototype.keyup=function(s){(37==s.keyCode||39==s.keyCode)&&(this.goToPage(this.sliderItemIndex+(37==s.keyCode?-1:1)),s.preventDefault())},s.prototype.setItemWidth=function(){this.sliderItemWidth=this.slider.scrollWidth/this.sliderItemCount,this.sliderItemsShown=Math.round(this.slider.clientWidth/this.sliderItemWidth)},s.prototype.setSnapTimeout=function(s){window.clearTimeout(this.sliderSnapTimeout),this.sliderSnapTimeout=window.setTimeout($.proxy(this.snap,this),s)},s.prototype.setPaginationVisibility=function(){var s=this.options.ssBaseClass+this.options.ssElementSeparator+this.options.ssPagerClass+this.options.ssModifierSeparator+this.options.ssHiddenModifier;this.sliderItemIndex<=0?this.$sliderPagerPrev.addClass(s):this.$sliderPagerPrev.removeClass(s),this.sliderItemIndex>=this.sliderItemCount-this.sliderItemsShown?this.$sliderPagerNext.addClass(s):this.$sliderPagerNext.removeClass(s)},s.prototype.setPagelistItemVisibility=function(){for(var s=0;s<this.sliderItemCount;s++)s>=this.sliderItemIndex&&s<this.sliderItemIndex+this.sliderItemsShown?$(this.$pagelistItems[s]).addClass(this.sliderPagelistPagerActiveClass):$(this.$pagelistItems[s]).removeClass(this.sliderPagelistPagerActiveClass)},s.prototype.setBackgroundImages=function(){for(var s=new RegExp(this.options.ssBgNormal+"|"+this.options.ssBgLarge+"|"+this.options.ssBgLargest,"ig"),t=0;t<this.sliderItemCount;t++){var e=$(this.$sliderItems[t]).find("[data-snapslider-bg]");itemVisible=t>=this.sliderItemIndex&&t<this.sliderItemIndex+this.sliderItemsShown;var i;i=this.sliderZoomed?itemVisible?this.options.ssBgLargest:this.options.ssBgLarge:itemVisible?this.options.ssBgLarge:this.options.ssBgNormal,e[0].style.backgroundImage=e[0].style.backgroundImage.replace(s,i)}},s.prototype.setPagelinksActiveItem=function(){this.$pagelinks.removeClass(this.sliderPagelinkActiveClass),$(this.$pagelinks[this.sliderItemIndex]).addClass(this.sliderPagelinkActiveClass)},s.prototype.snap=function(){if(this.sliderItemIndex=this.sliderScrollLeft/this.sliderItemWidth,this.sliderScrollExcess=this.sliderItemIndex%1,0==this.sliderScrollExcess)return this.sliderImageManagement&&this.setBackgroundImages(),this.options.ssEnablePagination&&this.setPaginationVisibility(),this.options.ssEnablePagelist&&this.setPagelistItemVisibility(),void(this.sliderPagelinksEnabled&&this.setPagelinksActiveItem());var s=this.sliderItemIndex;s=this.sliderScrollExcess<.5&&this.sliderScrollExcess<=this.options.ssSnapTolerance||this.sliderScrollExcess>.5&&this.sliderScrollExcess>=1-this.options.ssSnapTolerance?this.sliderScrollExcess<.5?Math.floor(s):Math.ceil(s):Math.floor(s)+Math.max(0,this.sliderScrollDirection),this.sliderItemIndex=s,this.animate(),this.sliderImageManagement&&this.setBackgroundImages(),this.options.ssEnablePagination&&this.setPaginationVisibility(),this.options.ssEnablePagelist&&this.setPagelistItemVisibility(),this.sliderPagelinksEnabled&&this.setPagelinksActiveItem()},s.prototype.animate=function(){this.sliderScrollTarget=this.sliderItemWidth*this.sliderItemIndex,this.$slider.animate({scrollLeft:this.sliderScrollTarget},this.options.ssAnimDuration)},s.prototype.paginate=function(s){this.goToPage(this.sliderItemIndex+(0>=s?-1:1))},s.prototype.goToPage=function(s){this.sliderItemIndex=Math.max(0,Math.min(this.sliderItemCount-this.sliderItemsShown,s)),this.animate(),this.sliderImageManagement&&this.setBackgroundImages(),this.options.ssEnablePagination&&this.setPaginationVisibility(),this.options.ssEnablePagelist&&this.setPagelistItemVisibility(),this.sliderPagelinksEnabled&&this.setPagelinksActiveItem()},s.prototype.zoom=function(s){this.$element.toggleClass(this.sliderZoomClass),this.sliderZoomed=this.$element.hasClass(this.sliderZoomClass),this.setItemWidth(),this.options.ssEnableZoomtopage?(this.sliderItemIndex=this.sliderItemIndexTrans=s,this.goToPage(s)):this.snap()};var t=$.fn.slapSlider;$.fn.snapSlider=function(t){return this.each(function(){var e=$(this),i=e.data("abg.snapslider"),n=$.extend({},s.DEFAULTS,e.data(),"object"==typeof t&&t);i||e.data("abg.snapslider",i=new s(this,n)),"string"==typeof t&&i[t]()})},$.fn.snapSlider.Constructor=s,$.fn.snapSlider.noConflict=function(){return $.fn.snapSlider=t,this},$(function(){$("[data-snapslider]").each(function(){var s=$(this),t=s.data("abg.snapslider")?"initialise":s.data();s.snapSlider(t)})})}(jQuery);