/*! carousel transition plugin for Cycle2; version: 20130108 */ (function($) { "use strict"; $( document ).on('cycle-bootstrap', function( e, opts, API ) { if ( opts.fx !== 'carousel' ) return; API.getSlideIndex = function( el ) { var slides = this.opts()._carouselWrap.children(); var i = slides.index( el ); return i % slides.length; }; // override default 'next' function API.next = function() { var count = opts.reverse ? -1 : 1; if ( opts.allowWrap === false && ( opts.currSlide + count ) > opts.slideCount - opts.carouselVisible ) return; opts.API.advanceSlide( count ); opts.API.trigger('cycle-next', [ opts ]).log('cycle-next'); }; }); $.fn.cycle.transitions.carousel = { // transition API impl preInit: function( opts ) { opts.hideNonActive = false; opts.container.on('cycle-destroyed', $.proxy(this.onDestroy, opts.API)); // override default API implementation opts.API.stopTransition = this.stopTransition; // issue #10 for (var i=0; i < opts.startingSlide; i++) { opts.container.append( opts.slides[0] ); } }, // transition API impl postInit: function( opts ) { var pagerCutoffIndex, wrap; var vert = opts.carouselVertical; if (opts.carouselVisible && opts.carouselVisible > opts.slideCount) opts.carouselVisible = opts.slideCount - 1; var visCount = opts.carouselVisible || opts.slides.length; var slideCSS = { display: vert ? 'block' : 'inline-block', position: 'static' }; // required styles opts.container.css({ position: 'relative', overflow: 'hidden' }); opts.slides.css( slideCSS ); opts._currSlide = opts.currSlide; // wrap slides in a div; this div is what is animated wrap = $('
0; var currSlide = opts._currSlide; var maxCurr = opts.slideCount - opts.carouselVisible; if ( hops > 0 && opts.nextSlide > maxCurr && currSlide == maxCurr ) { hops = 0; } else if ( hops > 0 && opts.nextSlide > maxCurr ) { hops = opts.nextSlide - currSlide - (opts.nextSlide - maxCurr); } else if ( hops < 0 && opts.currSlide > maxCurr && opts.nextSlide > maxCurr ) { hops = 0; } else if ( hops < 0 && opts.currSlide > maxCurr ) { hops += opts.currSlide - maxCurr; } else currSlide = opts.currSlide; moveBy = this.getScroll( opts, vert, currSlide, hops ); opts.API.opts()._currSlide = opts.nextSlide > maxCurr ? maxCurr : opts.nextSlide; } else { if ( fwd && opts.nextSlide === 0 ) { // moving from last slide to first moveBy = this.getDim( opts, opts.currSlide, vert ); callback = this.genCallback( opts, fwd, vert, callback ); } else if ( !fwd && opts.nextSlide == opts.slideCount - 1 ) { // moving from first slide to last moveBy = this.getDim( opts, opts.currSlide, vert ); callback = this.genCallback( opts, fwd, vert, callback ); } else { moveBy = this.getScroll( opts, vert, opts.currSlide, hops ); } } props[ vert ? 'top' : 'left' ] = fwd ? ( "-=" + moveBy ) : ( "+=" + moveBy ); // throttleSpeed means to scroll slides at a constant rate, rather than // a constant speed if ( opts.throttleSpeed ) speed = (moveBy / $(opts.slides[0])[vert ? 'height' : 'width']() ) * opts.speed; opts._carouselWrap.animate( props, speed, opts.easing, callback ); }, getDim: function( opts, index, vert ) { var slide = $( opts.slides[index] ); return slide[ vert ? 'outerHeight' : 'outerWidth'](true); }, getScroll: function( opts, vert, currSlide, hops ) { var i, moveBy = 0; if (hops > 0) { for (i=currSlide; i < currSlide+hops; i++) moveBy += this.getDim( opts, i, vert); } else { for (i=currSlide; i > currSlide+hops; i--) moveBy += this.getDim( opts, i, vert); } return moveBy; }, genCallback: function( opts, fwd, vert, callback ) { // returns callback fn that resets the left/top wrap position to the "real" slides return function() { var pos = $(opts.slides[opts.nextSlide]).position(); var offset = 0 - pos[vert?'top':'left'] + (opts.carouselOffset || 0); opts._carouselWrap.css( opts.carouselVertical ? 'top' : 'left', offset ); callback(); }; }, // core API override stopTransition: function() { var opts = this.opts(); opts.slides.stop( false, true ); opts._carouselWrap.stop( false, true ); }, // core API supplement onDestroy: function( e ) { var opts = this.opts(); if ( opts._carouselResizeThrottle ) $( window ).off( 'resize', opts._carouselResizeThrottle ); opts.slides.prependTo( opts.container ); opts._carouselWrap.remove(); } }; })(jQuery);