forked from malsup/cycle2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.cycle2.carousel.js
269 lines (227 loc) · 9.69 KB
/
jquery.cycle2.carousel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*! carousel transition plugin for Cycle2; version: 20130528 */
(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 i, j, slide, 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 = $('<div class="cycle-carousel-wrap"></div>')
.prependTo( opts.container )
.css({ margin: 0, padding: 0, top: 0, left: 0, position: 'absolute' })
.append( opts.slides );
opts._carouselWrap = wrap;
if ( !vert )
wrap.css('white-space', 'nowrap');
if ( opts.allowWrap !== false ) {
// prepend and append extra slides so we don't see any empty space when we
// near the end of the carousel. for fluid containers, add even more clones
// so there is plenty to fill the screen
// @todo: optimzie this based on slide sizes
for ( j=0; j < (opts.carouselVisible === undefined ? 2 : 1); j++ ) {
for ( i=0; i < opts.slideCount; i++ ) {
wrap.append( opts.slides[i].cloneNode(true) );
}
i = opts.slideCount;
while ( i-- ) { // #160, #209
wrap.prepend( opts.slides[i].cloneNode(true) );
}
}
wrap.find('.cycle-slide-active').removeClass('cycle-slide-active');
opts.slides.eq(opts.startingSlide).addClass('cycle-slide-active');
}
if ( opts.pager && opts.allowWrap === false ) {
// hide "extra" pagers
pagerCutoffIndex = opts.slideCount - visCount;
$( opts.pager ).children().filter( ':gt('+pagerCutoffIndex+')' ).hide();
}
opts._nextBoundry = opts.slideCount - opts.carouselVisible;
this.prepareDimensions( opts );
},
prepareDimensions: function( opts ) {
var dim, offset, pagerCutoffIndex, tmp;
var vert = opts.carouselVertical;
var visCount = opts.carouselVisible || opts.slides.length;
if ( opts.carouselFluid && opts.carouselVisible ) {
if ( ! opts._carouselResizeThrottle ) {
// fluid container AND fluid slides; slides need to be resized to fit container
this.fluidSlides( opts );
}
}
else if ( opts.carouselVisible && opts.carouselSlideDimension ) {
dim = visCount * opts.carouselSlideDimension;
opts.container[ vert ? 'height' : 'width' ]( dim );
}
else if ( opts.carouselVisible ) {
dim = visCount * $(opts.slides[0])[vert ? 'outerHeight' : 'outerWidth'](true);
opts.container[ vert ? 'height' : 'width' ]( dim );
}
// else {
// // fluid; don't size the container
// }
offset = ( opts.carouselOffset || 0 );
if ( opts.allowWrap !== false ) {
if ( opts.carouselSlideDimension ) {
offset -= ( (opts.slideCount + opts.currSlide) * opts.carouselSlideDimension );
}
else {
// calculate offset based on actual slide dimensions
tmp = opts._carouselWrap.children();
for (var j=0; j < (opts.slideCount + opts.currSlide); j++) {
offset -= $(tmp[j])[vert?'outerHeight':'outerWidth'](true);
}
}
}
opts._carouselWrap.css( vert ? 'top' : 'left', offset );
},
fluidSlides: function( opts ) {
var timeout;
var slide = opts.slides.eq(0);
var adjustment = slide.outerWidth() - slide.width();
var prepareDimensions = this.prepareDimensions;
// throttle resize event
$(window).on( 'resize', resizeThrottle);
opts._carouselResizeThrottle = resizeThrottle;
onResize();
function resizeThrottle() {
clearTimeout( timeout );
timeout = setTimeout( onResize, 20 );
}
function onResize() {
opts._carouselWrap.stop( false, true );
var slideWidth = opts.container.width() / opts.carouselVisible;
slideWidth = Math.ceil( slideWidth - adjustment );
opts._carouselWrap.children().width( slideWidth );
if ( opts._sentinel )
opts._sentinel.width( slideWidth );
prepareDimensions( opts );
}
},
// transition API impl
transition: function( opts, curr, next, fwd, callback ) {
var moveBy, props = {};
var hops = opts.nextSlide - opts.currSlide;
var vert = opts.carouselVertical;
var speed = opts.speed;
// handle all the edge cases for wrapping & non-wrapping
if ( opts.allowWrap === false ) {
fwd = hops > 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);