-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathjquery.carousel.min.js
8 lines (8 loc) · 16.5 KB
/
jquery.carousel.min.js
1
2
3
4
5
6
7
8
/*!
* jQuery Carousel Plugin
*
* @author Thomas Jaggi, http://responsive.ch
* @license Dual licensed under MIT and GPL 2
* @link https://github.com/backflip/jquery-carousel
*/
(function(g,h,f,c){var d="carousel",e={domSelectors:{frame:"."+d+"-frame",slider:"."+d+"-slider",slides:"."+d+"-slide"},templates:{navContainer:'<div class="'+d+'-navs" aria-hidden="true" role="presentation" />',navItemPrev:'<button class="'+d+'-nav" role="presentation">Show previous slide</button>',navItemNext:'<button class="'+d+'-nav" role="presentation">Show next slide</button>',counter:'<div class="'+d+'-counter" aria-hidden="true" role="presentation">%current% of %total%</div>',handleContainer:'<div class="'+d+'-handles" aria-hidden="true" role="presentation" />',handleItem:'<button class="'+d+'-handle" role="presentation">%index%</div>'},stateClasses:{isInitialized:"is-initialized",isEnabled:"is-enabled",isDisabled:"is-disabled",isActive:"is-active"},animation:{duration:300,step:1,easing:"ease-in-out"},behavior:{circular:false,autoplay:0,pauseAutoplayOnHover:true,keyboardNav:true},layout:{horizontal:true,groupedHandles:true,fixedHeight:true,responsive:true,visibleSlides:1,gutter:0},elements:{prevNext:true,handles:true,counter:true},events:{start:false,stop:false},initialSlide:0,touch:{enabled:true,thresholds:{speed:0.4,distance:0.3}},$syncedCarousels:null},j={getTransitionProperty:function(){var q=h.body||h.documentElement,s="transition",p,n=["Moz","ms","O","Webkit"],r=q.style;if(typeof r[s]==="string"){return s}s=s.charAt(0).toUpperCase()+s.substr(1);for(var o=0;o<n.length;o++){p=n[o]+s;if(typeof r[p]==="string"){return p}}return false},getTransitionEndEvent:function(){var n=j.getTransitionProperty(),o={transition:"transitionend",MozTransition:"transitionend",msTransition:"msTransitionEnd",OTransition:"oTransitionEnd",WebkitTransition:"webkitTransitionEnd"};return o[n]||false},getNamespacedEvents:function(p){var n=p.split(" ");for(var o=0;o<n.length;o++){n[o]=n[o]+"."+d}return n.join(" ")},enableButton:function(n){n.removeAttr("disabled").attr("tabindex",0)},disableButton:function(n){n.attr("disabled",true).attr("tabindex",-1)}},k={touch:(typeof g.Modernizr!=="undefined"&&typeof Modernizr.touch!=="undefined")?Modernizr.touch:(("ontouchstart" in g)||g.DocumentTouch&&h instanceof DocumentTouch),transition:j.getTransitionProperty()},m="carouselUpdate",l=false,b=false,a=0;var i=function(o,n){this.$dom={container:f(o),frame:null,slider:null,slides:null,navContainer:null,navItems:null,counter:null,handleContainer:null,handleItems:null};this.counterText=null;this.props={currentDomIndex:0,currentSlideIndex:0,total:0,visible:0};this.state={enabled:false,animating:false};this.settings=f.extend(true,{},n,this.$dom.container.data(d+"-options"))};i.prototype={init:function(){var n=this.$dom,q,p,o=null;if(n.container.data(d)||n.container.length===0){return this}this.index=a++;this.settings=f.extend(true,{},e,this.settings);n.container.addClass(this.settings.stateClasses.isInitialized);n.frame=n.container.find(this.settings.domSelectors.frame);n.slider=n.container.find(this.settings.domSelectors.slider);n.slides=n.container.find(this.settings.domSelectors.slides);if(this.settings.elements.prevNext){n.navContainer=f(this.settings.templates.navContainer).appendTo(n.container);q=f(this.settings.templates.navItemPrev).appendTo(n.navContainer);p=f(this.settings.templates.navItemNext).appendTo(n.navContainer);n.navItems=q.add(p)}if(this.settings.elements.counter){n.counter=f(this.settings.templates.counter).appendTo(n.container);this.counterText=n.counter.text()}if(this.settings.elements.handles){n.handleContainer=f(this.settings.templates.handleContainer).appendTo(n.container)}if(this.settings.behavior.circular){n.slides.each(function(r){f(this).data(d+"-index",r)})}if(!this.settings.layout.horizontal){n.container.addClass(d+"-vertical")}this.update();if(this.settings.layout.responsive){f(g).on(j.getNamespacedEvents("resize")+this.index,f.proxy(function(){if(o){clearTimeout(o)}o=setTimeout(f.proxy(function(){this.resize()},this),100)},this))}n.container.data(d,this);return this},update:function(n){f.extend(true,this.settings,n);this.$dom.slides=this.$dom.container.find(this.settings.domSelectors.slides);this.props.total=this.$dom.slides.length;this.props.currentDomIndex=(this.props.currentDomIndex>this.props.total)?this.props.total:this.props.currentDomIndex;this.props.currentSlideIndex=(this.props.currentSlideIndex>this.props.total)?this.props.total:this.props.currentDomIndex;this.props.visible=this._getVisibleSlides();if(this.settings.elements.handles){this.$dom.handleContainer.html(this._getHandles());this.$dom.handleItems=this.$dom.handleContainer.children()}this.resize();this.disable();this.enable()},resize:function(){var s=this.$dom.frame.width(),r,t=this.settings.layout.gutter,q=t?(this.settings.layout.horizontal?{"margin-left":0.5*t,"margin-right":0.5*t}:{"margin-top":0.5*t,"margin-bottom":0.5*t}):{},o=this.settings.layout.horizontal?Math.floor(s/this.props.visible):s,n,p=this.settings.layout.horizontal?this.props.total*(o+t):o;this.$dom.slides.outerWidth(o).css(q);this.$dom.slider.width(p).height("auto");if(this.settings.layout.fixedHeight){n=this._getHighestSlide();this.$dom.slides.css("min-height",n);r=this.settings.layout.horizontal?n:this.props.visible*(n+t);this.$dom.frame.height(r)}this.goTo(this.props.currentDomIndex,true)},enable:function(){var n=this;if(this.state.enabled||this.props.visible>this.props.total){return}if(this.settings.elements.prevNext){this.$dom.navItems.on(j.getNamespacedEvents("click"),function(){var q=f(this),o,p;o=q.index()===1?1:-1;p=o*n.settings.animation.step+n.props.currentDomIndex;n.goTo(p)})}if(this.settings.elements.handles){this.$dom.handleItems.on(j.getNamespacedEvents("click"),function(){var p=f(this),q=p.data(d+"-handle-index")||p.index(),o=n._getCurrentSlideIndex(q);n.goTo(o)})}if(this.settings.touch.enabled&&k.touch){this._touchEnable()}if(this.settings.behavior.keyboardNav){this.$dom.container.on(j.getNamespacedEvents("keydown"),f.proxy(function(s){var p=f(s.target),v=p.get(0).nodeName.toLowerCase(),o=f.inArray(v,["input","textarea"])!==-1,u=false;if(!(o||s.metaKey||s.ctrlKey)){var r=s.keyCode||s.which,q,t;if(f.inArray(r,[37,80])!==-1){this.prev();u=true}else{if(f.inArray(r,[39,78])!==-1){this.next();u=true}else{if(47<r&&r<58){q=r-49;t=n._getCurrentSlideIndex(q);this.goTo(t);u=true}}}}if(u){this._autoplayDisable();s.preventDefault()}},this))}this.$dom.slides.each(function(){var p=f(this),o=p.data(d+"-index")||n.$dom.slides.index(p),q=n.$dom.frame.scrollLeft();p.on(j.getNamespacedEvents("focus"),"*",function(){setTimeout(function(){n.$dom.frame.scrollLeft(q);if(o>n.props.currentSlideIndex&&o<(n.props.currentSlideIndex+n.props.visibleSlides)){n.goTo(o)}},0)})});if(this.settings.behavior.autoplay){this._autoplayEnable();if(this.settings.behavior.pauseAutoplayOnHover){this.$dom.container.on(j.getNamespacedEvents("mouseenter"),f.proxy(function(){this._autoplayDisable()},this));this.$dom.container.on(j.getNamespacedEvents("mouseleave"),f.proxy(function(){this._autoplayEnable()},this))}}this.$dom.container.addClass(this.settings.stateClasses.isEnabled);this.$dom.container.removeClass(this.settings.stateClasses.isDisabled);this.state.enabled=true;this._updateNav();if(!this.settings.layout.fixedHeight){this._updateHeight()}if(this.settings.$syncedCarousels){this.$dom.container.on(j.getNamespacedEvents(m),f.proxy(function(o,p){this.goTo(p.index,false,true)},this))}},disable:function(){if(!this.state.enabled){return}this.$dom.container.off(j.getNamespacedEvents(""));this.$dom.container.find("*").off(j.getNamespacedEvents(""));this.$dom.container.addClass(this.settings.stateClasses.isDisabled);this.$dom.container.removeClass(this.settings.stateClasses.isEnabled);this.state.enabled=false;this._updateNav();if(!this.settings.layout.fixedHeight){this._updateHeight()}},goTo:function(s,x,p){if(this.state.animating){return}var A=this,u=this._getValidatedTarget(s),t=this._getOriginalSlideIndex(u),r=this._getTargetPosition(u),w=(!k.transition&&this.settings.animation.easing!=="linear")?"swing":this.settings.animation.easing,q=x?0:this.settings.animation.duration,z=function(){if(A.settings.events.stop){A.settings.events.stop(u,t)}A._touchEnable();A.state.animating=false},o,n,B,v,y;if(!x){this.state.animating=true;A._touchDisable();if(this.settings.events.start){this.settings.events.start(u,t)}if(!k.transition){this.$dom.slider.animate(r,q,w,function(){z()})}else{o=k.transition;n=this.settings.layout.horizontal?"left":"top";B=j.getNamespacedEvents(j.getTransitionEndEvent());y=this.$dom.slider.css(o);v=n+" "+(q/1000)+"s "+w;this.$dom.slider.css(o,v);this.$dom.slider.css(r);this.$dom.slider.on(B,function(){A.$dom.slider.off(B);A.$dom.slider.css(o,y);z()})}if(this.settings.$syncedCarousels&&!p){this.settings.$syncedCarousels.trigger(j.getNamespacedEvents(m),{index:u})}}else{this.$dom.slider.css(r)}this.props.currentDomIndex=u;this.props.currentSlideIndex=t;this._updateNav();if(!this.settings.layout.fixedHeight){this._updateHeight()}},next:function(){this.goTo(this.props.currentDomIndex+this.settings.animation.step)},prev:function(){this.goTo(this.props.currentDomIndex-this.settings.animation.step)},destroy:function(){this.$dom.container.removeData(d).removeClass(this.settings.stateClasses.isInitialized);this.$dom.slides.removeData(d+"-index");this.$dom.frame.removeAttr("style");this.$dom.slider.removeAttr("style");this.$dom.slides.removeAttr("style");this.$dom.navContainer&&this.$dom.navContainer.remove();this.$dom.handleContainer&&this.$dom.handleContainer.remove();this.$dom.counter&&this.$dom.counter.remove();this.state.enabled=false;this.props.currentDomIndex=0;this.props.currentSlideIndex=0;this.$dom.container.off(j.getNamespacedEvents(""));this.$dom.container.find("*").off(j.getNamespacedEvents(""));f(g).off(j.getNamespacedEvents("resize")+this.index)},_autoplayEnable:function(){this.autoplay=setInterval(f.proxy(function(){this.next()},this),this.settings.behavior.autoplay)},_autoplayDisable:function(){clearInterval(this.autoplay);this.autoplay=null},_getHandles:function(){var n=f(),s=this.props.total,r=0,u,t;if(!this.settings.layout.groupedHandles){for(;r<s;r++){u=f(this.settings.templates.handleItem);t=u.text().replace("%index%",(r+1));u.text(t);f.merge(n,u)}}else{s=this.props.total/this.props.visible;for(;r<s;r++){var p=r*this.props.visible+1,q=(r+1)*this.props.visible,o;u=f(this.settings.templates.handleItem);t=u.text();if(q>this.props.total){q=this.props.total}if(p<q){o=p+" - "+q}else{o=q}t=t.replace("%index%",o);u.text(t).data(d+"-handle-index",p-1);f.merge(n,u)}}return n},_getHighestSlide:function(p){var n=0,o=this.$dom.slides;if(p){o=o.filter(p)}o.each(function(){var q=f(this).css("min-height",0).outerHeight();if(q>n){n=q}});return n},_getOriginalSlideIndex:function(n){var o=n;if(this.settings.behavior.circular){o=this.$dom.slides.eq(n).data(d+"-index")}return o},_getCurrentSlideIndex:function(o){var n=o;if(this.settings.behavior.circular){this.$dom.slides.each(function(){var q=f(this),r=q.index(),p=q.data(d+"-index");if(o===p){n=r;return false}})}return n},_getStyles:function(n){var q=["height","left","min-height","top","width"],p={},r;for(var o=0;o<q.length;o++){p[q[o]]=n.css(q[o])}if(k.transition){r=k.transition.charAt(0).toLowerCase()+k.transition.substr(1);p[r]=n.css(r)}return p},_getTargetPosition:function(n){var q=this.settings.layout.horizontal?this.$dom.slides.outerWidth():this.$dom.slides.outerHeight(),p=this.settings.layout.gutter,r=this.settings.layout.horizontal?"left":"top",o={};o[r]=-(n*(q+p)+0.5*p);return o},_getValidatedTarget:function(p){if(this.props.total<=this.props.visible){p=0}else{if(!this.settings.behavior.circular){if(p<0){p=0}else{if(p>=(this.props.total-this.props.visible)){p=this.props.total-this.props.visible}}}else{if(p<0){var n=this.props.total+p;if(this._shiftSlides(n,-1)){return 0}}else{if(this.props.total>this.props.visible&&p>(this.props.total-this.props.visible)){var o=(p-1)-(this.props.total-this.props.visible);if(this._shiftSlides(o,1)){return p-(o+1)}}}}}return p},_getVisibleSlides:function(){if(this.settings.layout.visibleSlides>0){return this.settings.layout.visibleSlides}else{var n=this,o=0,p=this.settings.layout.horizontal?this.$dom.frame.width():this.$dom.frame.height();this.$dom.slides.each(function(){var q=n.settings.layout.horizontal?f(this).outerWidth():f(this).outerHeight();if(q>o){o=q}});return Math.round(p/o)}},_shiftSlides:function(p,o){var n=(o===-1)?":gt("+(p-1)+")":":lt("+(p+1)+")",t=this.$dom.slides.filter(n);if(t.length>0){var r=this.settings.layout.horizontal?"left":"top",u=this.settings.layout.horizontal?this.$dom.slides.outerWidth():this.$dom.slides.outerHeight(),s=this.$dom.slider.position(),q={};q[r]=s[r]+o*t.length*u;if(o===-1){t.prependTo(this.$dom.slider)}else{t.appendTo(this.$dom.slider)}this.$dom.slides=this.$dom.slider.children();this.$dom.slider.css(q);return true}else{return false}},_getTouchDistance:function(n){var o={x:n.end.x-n.start.x,y:n.end.y-n.start.y};if(!this.settings.layout.horizontal){o={x:o.y,y:o.x}}return o},_touchEnable:function(){var n=this,p={start:{x:0,y:0},end:{x:0,y:0}},s={start:0,end:0},r={distance:n.settings.touch.thresholds.distance*n.$dom.frame.width(),speed:n.settings.touch.thresholds.speed*n.$dom.frame.width()},q={top:0,left:0},o={};o[j.getNamespacedEvents("touchstart")]=function(u){var t=u.originalEvent.targetTouches[0];p.start.x=t.pageX;p.start.y=t.pageY;q=n.$dom.slider.position();s.start=new Date().getTime();l=false;b=false};o[j.getNamespacedEvents("touchmove")]=function(x){if(b&&!l){return}var w=x.originalEvent.targetTouches[0],y=0,u={},t=n.settings.layout.horizontal?"left":"top",v;p.end.x=w.pageX;p.end.y=w.pageY;y=n._getTouchDistance(p);if(Math.abs(y.x)>Math.abs(y.y)||l){v=n.settings.layout.horizontal?q.left:q.top;u[t]=v+y.x;n.$dom.slider.css(u);if(n.settings.$syncedCarousels){n.settings.$syncedCarousels.each(function(){var z=f(this).data(d);z.$dom.slider.css(u)})}l=true;x.preventDefault()}else{b=true}};o[j.getNamespacedEvents("touchend")]=function(){if(!l||b){return}var y=n._getTouchDistance(p),v,x=n.props.currentDomIndex;if(Math.abs(y.x)>Math.abs(y.y)){s.end=new Date().getTime();v=Math.abs(y.x)/(s.end-s.start)*1000;if(Math.abs(y.x)>r.distance||v>r.speed){var t=n.settings.layout.horizontal?n.$dom.slides.outerWidth():n.$dom.slides.outerHeight(),u=(y.x>0)?-1:1,w=Math.abs(Math.round(y.x/t));if(w<1&&v>r.speed){w=1}x=n.props.currentDomIndex+u*w}n.goTo(x);if(n.settings.behavior.autoplay){n._autoplayDisable()}}};this.$dom.frame.on(o)},_touchDisable:function(){this.$dom.frame.off(j.getNamespacedEvents("touchstart touchmove touchend"))},_updateNav:function(){if(this.state.enabled){if(this.settings.elements.prevNext){j.enableButton(this.$dom.navItems.eq(0));j.enableButton(this.$dom.navItems.eq(1));if(!this.settings.behavior.circular){if(this.props.currentDomIndex===0){j.disableButton(this.$dom.navItems.eq(0))}else{j.enableButton(this.$dom.navItems.eq(0))}if(this.props.currentDomIndex===this.props.total-this.props.visible){j.disableButton(this.$dom.navItems.eq(1))}else{j.enableButton(this.$dom.navItems.eq(1))}}}if(this.settings.elements.handles){var n=this._getOriginalSlideIndex(this.props.currentDomIndex),o=(n>0)?":gt("+(n-1)+"):lt("+this.props.visible+")":":lt("+(n+this.props.visible)+")";if(this.settings.layout.groupedHandles){o=":eq("+Math.ceil(n/this.props.visible)+")"}j.enableButton(this.$dom.handleItems);this.$dom.handleItems.removeClass(this.settings.stateClasses.isActive);this.$dom.handleItems.filter(o).addClass(this.settings.stateClasses.isActive)}}else{if(this.settings.elements.prevNext){j.disableButton(this.$dom.navItems.eq(0));j.disableButton(this.$dom.navItems.eq(1))}if(this.settings.elements.handles){j.disableButton(this.$dom.handleItems)}}if(this.settings.elements.counter){var p=this._getOriginalSlideIndex(this.props.currentDomIndex)+1,r=p+(this.props.visible-1),q;if(this.props.visible>1){if(r>this.props.total){r=this.props.total}p+="-"+r}q=this.counterText.replace("%current%",p).replace("%total%",this.props.total);this.$dom.counter.text(q)}},_updateHeight:function(){var o=this.props.currentDomIndex,p=o+this.props.visible,s=(o>0)?":gt("+(o-1)+"):lt("+this.props.visible+")":":lt("+(p)+")",q=this._getHighestSlide(),r=this.$dom.frame.height(),n=this._getHighestSlide(s);if(q<r){q=r}this.$dom.slides.css("min-height",q);this.$dom.frame.animate({height:n},this.settings.animation.duration,f.proxy(function(){this.$dom.slides.css("min-height",n)},this))}};f.fn[d]=function(o){var n=arguments;return this.each(function(){var q=f(this),p=q.data(d);if(p){if(typeof o==="object"){p.update.apply(p,n)}else{p[o].apply(p,Array.prototype.slice.call(n,1))}}else{new i(this,o).init()}})};g[d.charAt(0).toUpperCase()+d.substr(1)]=i})(window,document,jQuery);