forked from olton/metroui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcarousel.js
More file actions
378 lines (329 loc) · 12.1 KB
/
carousel.js
File metadata and controls
378 lines (329 loc) · 12.1 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
* Carousel - jQuery plugin for MetroUiCss framework
*/
(function($) {
var pluginName = 'Carousel',
initAllSelector = '[data-role=carousel], .carousel',
paramKeys = ['Auto', 'Period', 'Duration', 'Effect', 'Direction', 'Markers', 'Arrows', 'Stop'];
$[pluginName] = function(element, options) {
if (!element) {
return $()[pluginName]({initAll: true});
}
// default settings
var defaults = {
// auto slide change
auto: true,
// slide change period
period: 6000,
// animation duration
duration: 1000,
// animation effect (fade, slide, switch, slowdown)
effect: 'slide',
// animation direction (left, right) for some kinds of animation effect
direction: 'left',
// markers below the carousel
markers: 'on',
// prev and next arrows
arrows: 'on',
// stop sliding when cursor over the carousel
stop: 'on'
};
var plugin = this;
// plugin settings
plugin.settings = {};
var $element = $(element); // reference to the jQuery version of DOM element
var slides, // all slides DOM objects
currentSlideIndex, // index of current slide
slideInPosition, // slide start position before it's appear
slideOutPosition, // slide position after it's disappear
parentWidth,
parentHeight,
animationInProgress,
autoSlideTimer,
markers,
stopAutoSlide = false;
// initialization
plugin.init = function () {
plugin.settings = $.extend({}, defaults, options);
slides = $element.find('.slides:first-child > .slide');
// if only one slide
if (slides.length <= 1) {
return;
}
currentSlideIndex = 0;
// parent block dimensions
parentWidth = $element.innerWidth();
parentHeight = $element.innerHeight();
// slides positions, used for some kinds of animation
slideInPosition = getSlideInPosition();
slideOutPosition = getSlideOutPosition();
// prepare slide elements
slides.each(function (index, slide) {
$slide = $(slide);
// each slide must have position:absolute
// if not, set it
if ($slide.css('position') !== 'absolute') {
$slide.css('position', 'absolute');
}
// disappear all slides, except first
if (index !== 0) {
$slide.hide();
}
});
if (plugin.settings.arrows === 'on') {
// prev next buttons handlers
$element.find('span.control.left').on('click', function(){
changeSlide('left');
startAutoSlide();
});
$element.find('span.control.right').on('click', function(){
changeSlide('right');
startAutoSlide();
});
} else {
$element.find('span.control').hide();
}
// markers
if (plugin.settings.markers === 'on') {
insertMarkers();
}
// enable auto slide
if (plugin.settings.auto === true) {
startAutoSlide();
// stop sliding when cursor over the carousel
if (plugin.settings.stop === 'on') {
$element.on('mouseenter', function () {
stopAutoSlide = true;
});
$element.on('mouseleave', function () {
stopAutoSlide = false;
startAutoSlide();
});
}
}
// u can use same code:
// $('#carusel').trigger('changeSlide', [{direction: 'left', effect: 'fade', index: 1}])
// any option not required
$element.on('changeSlide', function(event, options){
options = options || {};
changeSlide(options.direction, options.effect, options.index);
});
};
/**
* returns start position for appearing slide {left: xxx}
*/
var getSlideInPosition = function () {
var pos;
if (plugin.settings.direction === 'left') {
pos = {
'left': parentWidth
}
} else if (plugin.settings.direction === 'right') {
pos = {
'left': -parentWidth
}
}
return pos;
};
/**
* returns end position of disappearing slide {left: xxx}
*/
var getSlideOutPosition = function () {
var pos;
if (plugin.settings.direction === 'left') {
pos = {
'left': -parentWidth
}
} else if (plugin.settings.direction === 'right') {
pos = {
'left': parentWidth
}
}
return pos;
};
/**
* start or restart auto change
*/
var startAutoSlide = function () {
clearInterval(autoSlideTimer);
// start slide changer timer
autoSlideTimer = setInterval(function () {
if (stopAutoSlide) {
return;
}
changeSlide();
}, plugin.settings.period);
};
/**
* inserts markers below the carousel
*/
var insertMarkers = function () {
var div, ul, li, i;
div = $('<div class="markers"></div>');
ul = $('<ul></ul>').appendTo(div);
for (i = 0; i < slides.length; i++) {
li = $('<li><a href="javascript:void(0)" data-num="' + i + '"></a></li>');
if (i === 0) {
li.addClass('active');
}
li.appendTo(ul);
}
markers = ul.find('li');
ul.find('li a').on('click', function () {
var $this = $(this),
index;
// index of appearing slide
index = $this.data('num');
if (index === currentSlideIndex) {
return;
}
changeSlide(undefined, 'switch', index);
startAutoSlide();
});
div.appendTo($element);
};
/**
* changes slide to next
*/
var changeSlide = function(direction, effect, slideIndex) {
var outSlide, // disappearin slide element
inSlide, // appearing slide element
nextSlideIndex,
delta = 1,
slideDirection = 1;
effect = effect || plugin.settings.effect;
// correct slide direction, used for 'slide' and 'slowdown' effects
if ((effect === 'slide' || effect === 'slowdown') && typeof direction !== 'undefined' && direction !== plugin.settings.direction) {
slideDirection = -1;
}
if (direction === 'left') {
delta = -1;
}
outSlide = $(slides[currentSlideIndex]);
nextSlideIndex = (typeof slideIndex !== 'undefined' && slideIndex !== currentSlideIndex) ? slideIndex : currentSlideIndex + delta;
if (nextSlideIndex >= slides.length) {
nextSlideIndex = 0;
}
if (nextSlideIndex < 0) {
nextSlideIndex = slides.length - 1;
}
inSlide = $(slides[nextSlideIndex]);
if (animationInProgress === true) {
return;
}
// switch effect is quickly, no need to wait
if (effect !== 'switch') {
// when animation in progress no other animation occur
animationInProgress = true;
setTimeout(function () {
animationInProgress = false;
}, plugin.settings.duration)
}
// change slide with selected effect
switch (effect) {
case 'switch':
changeSlideSwitch(outSlide, inSlide);
break;
case 'slide':
changeSlideSlide(outSlide, inSlide, slideDirection);
break;
case 'fade':
changeSlideFade(outSlide, inSlide);
break;
case 'slowdown':
changeSlideSlowdown(outSlide, inSlide, slideDirection);
break;
}
currentSlideIndex = nextSlideIndex;
// switch marker
if (plugin.settings.markers === 'on') {
markers.removeClass('active');
$(markers.get(currentSlideIndex)).addClass('active');
}
};
/**
* switch effect
*/
var changeSlideSwitch = function (outSlide, inSlide) {
inSlide.show().css({'left': 0});
outSlide.hide();
};
/**
* slide effect
*/
var changeSlideSlide = function (outSlide, inSlide, slideDirection) {
var unmovedPosition = {'left': 0},
duration = plugin.settings.duration;
if (slideDirection !== -1) {
inSlide.css(slideInPosition);
inSlide.show();
outSlide.animate(slideOutPosition, duration);
inSlide.animate(unmovedPosition, duration);
} else {
inSlide.css(slideOutPosition);
inSlide.show();
outSlide.animate(slideInPosition, duration);
inSlide.animate(unmovedPosition, duration);
}
};
/**
* slowdown slide effect (custom easing 'doubleSqrt')
*/
var changeSlideSlowdown = function (outSlide, inSlide, slideDirection) {
var unmovedPosition = {'left': 0},
options;
options = {
'duration': plugin.settings.duration,
'easing': 'doubleSqrt'
};
if (slideDirection !== -1) {
inSlide.css(slideInPosition);
inSlide.show();
outSlide.animate(slideOutPosition, options);
inSlide.animate(unmovedPosition, options);
} else {
inSlide.css(slideOutPosition);
inSlide.show();
outSlide.animate(slideInPosition, options);
inSlide.animate(unmovedPosition, options);
}
};
/**
* fade effect
*/
var changeSlideFade = function (outSlide, inSlide) {
inSlide.hide();
inSlide.css({
left: 0,
top: 0
});
inSlide.fadeIn(plugin.settings.duration);
outSlide.fadeOut(plugin.settings.duration);
};
plugin.init();
};
// easing effect for jquery animation
$.easing.doubleSqrt = function(t, millisecondsSince, startValue, endValue, totalDuration) {
var res = Math.sqrt(Math.sqrt(t));
return res;
};
$.fn[pluginName] = function(options) {
var elements = options && options.initAll ? $(initAllSelector) : this;
return elements.each(function() {
var that = $(this),
params = {},
plugin;
if (undefined == that.data(pluginName)) {
$.each(paramKeys, function(index, key){
params[key[0].toLowerCase() + key.slice(1)] = that.data('param' + key);
});
plugin = new $[pluginName](this, params);
that.data(pluginName, plugin);
}
});
};
// autoinit
$(function(){
$()[pluginName]({initAll: true});
});
})(jQuery);