-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathjquery.flexisel.js
416 lines (339 loc) · 16.7 KB
/
jquery.flexisel.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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
* File: jquery.flexisel.js
* Version: 2.2.2
* Description: Responsive carousel jQuery plugin
* Author: 9bit Studios
* Copyright 2016, 9bit Studios
* http://www.9bitstudios.com
* Free to use and abuse under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*/
(function ($) {
$.fn.flexisel = function (options) {
var defaults = $.extend({
visibleItems: 4,
itemsToScroll: 3,
animationSpeed: 400,
infinite: true,
navigationTargetSelector: null,
autoPlay: {
enable: false,
interval: 5000,
pauseOnHover: true
},
responsiveBreakpoints: {
portrait: {
changePoint:480,
visibleItems: 1,
itemsToScroll: 1
},
landscape: {
changePoint:640,
visibleItems: 2,
itemsToScroll: 2
},
tablet: {
changePoint:768,
visibleItems: 3,
itemsToScroll: 3
}
},
loaded: function(){ },
before: function(){ },
after: function(){ },
resize: function(){ }
}, options);
/******************************
Private Variables
*******************************/
var object = $(this);
var settings = $.extend(defaults, options);
var itemsWidth;
var canNavigate = true;
var itemCount;
var itemsVisible = settings.visibleItems;
var itemsToScroll = settings.itemsToScroll;
var responsivePoints = [];
var resizeTimeout;
var autoPlayInterval;
/******************************
Public Methods
*******************************/
var methods = {
init: function() {
return this.each(function () {
methods.appendHTML();
methods.setEventHandlers();
methods.initializeItems();
});
},
/******************************
Initialize Items
*******************************/
initializeItems: function() {
var obj = settings.responsiveBreakpoints;
for(var i in obj) { responsivePoints.push(obj[i]); }
responsivePoints.sort(function(a, b) { return a.changePoint - b.changePoint; });
var childSet = object.children();
childSet.first().addClass("index");
itemsWidth = methods.getCurrentItemWidth();
itemCount = childSet.length;
childSet.width(itemsWidth);
if(settings.infinite) {
methods.offsetItemsToBeginning(Math.floor(childSet.length / 2));
object.css({
'left': -itemsWidth * Math.floor(childSet.length / 2)
});
}
$(window).trigger('resize');
object.fadeIn();
settings.loaded.call(this, object);
},
/******************************
Append HTML
*******************************/
appendHTML: function() {
object.addClass("nbs-flexisel-ul");
object.wrap("<div class='nbs-flexisel-container'><div class='nbs-flexisel-inner'></div></div>");
object.find("li").addClass("nbs-flexisel-item");
if(settings.navigationTargetSelector && $(settings.navigationTargetSelector).length > 0) {
$("<div class='nbs-flexisel-nav-left'></div><div class='nbs-flexisel-nav-right'></div>").appendTo(settings.navigationTargetSelector);
} else {
settings.navigationTargetSelector = object.parent();
$("<div class='nbs-flexisel-nav-left'></div><div class='nbs-flexisel-nav-right'></div>").insertAfter(object);
}
if(settings.infinite) {
var childSet = object.children();
var cloneContentBefore = childSet.clone();
var cloneContentAfter = childSet.clone();
object.prepend(cloneContentBefore);
object.append(cloneContentAfter);
}
},
/******************************
Set Event Handlers
*******************************/
setEventHandlers: function() {
var self = this;
var childSet = object.children();
$(window).on("resize", function(event){
canNavigate = false;
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(function(){
canNavigate = true;
methods.calculateDisplay();
itemsWidth = methods.getCurrentItemWidth();
childSet.width(itemsWidth);
if(settings.infinite) {
object.css({
'left': -itemsWidth * Math.floor(childSet.length / 2)
});
} else {
methods.clearDisabled();
$(settings.navigationTargetSelector).find(".nbs-flexisel-nav-left").addClass('disabled');
object.css({
'left': 0
});
}
settings.resize.call(self, object);
}, 100);
});
$(settings.navigationTargetSelector).find(".nbs-flexisel-nav-left").on("click", function (event) {
methods.scroll(true);
});
$(settings.navigationTargetSelector).find(".nbs-flexisel-nav-right").on("click", function (event) {
methods.scroll(false);
});
if(settings.autoPlay.enable) {
methods.setAutoplayInterval();
if (settings.autoPlay.pauseOnHover === true) {
object.on({
mouseenter : function() {
canNavigate = false;
},
mouseleave : function() {
canNavigate = true;
}
});
}
}
object[0].addEventListener('touchstart', methods.touchHandler.handleTouchStart, false);
object[0].addEventListener('touchmove', methods.touchHandler.handleTouchMove, false);
},
/******************************
Calculate Display
*******************************/
calculateDisplay: function() {
var contentWidth = $('html').width();
var largestCustom = responsivePoints[responsivePoints.length-1].changePoint; // sorted array
for(var i in responsivePoints) {
if(contentWidth >= largestCustom) { // set to default if width greater than largest custom responsiveBreakpoint
itemsVisible = settings.visibleItems;
itemsToScroll = settings.itemsToScroll;
break;
}
else { // determine custom responsiveBreakpoint to use
if(contentWidth < responsivePoints[i].changePoint) {
itemsVisible = responsivePoints[i].visibleItems;
itemsToScroll = responsivePoints[i].itemsToScroll;
break;
}
else {
continue;
}
}
}
},
/******************************
Scroll
*******************************/
scroll: function(reverse) {
if(typeof reverse === 'undefined') { reverse = true }
if(canNavigate == true) {
canNavigate = false;
settings.before.call(this, object);
itemsWidth = methods.getCurrentItemWidth();
if(settings.autoPlay.enable) {
clearInterval(autoPlayInterval);
}
if(!settings.infinite) {
var scrollDistance = itemsWidth * itemsToScroll;
if(reverse) {
object.animate({
'left': methods.calculateNonInfiniteLeftScroll(scrollDistance)
}, settings.animationSpeed, function(){
settings.after.call(this, object);
canNavigate = true;
});
} else {
object.animate({
'left': methods.calculateNonInfiniteRightScroll(scrollDistance)
},settings.animationSpeed, function(){
settings.after.call(this, object);
canNavigate = true;
});
}
} else {
object.animate({
'left' : reverse ? "+=" + itemsWidth * itemsToScroll : "-=" + itemsWidth * itemsToScroll
}, settings.animationSpeed, function() {
settings.after.call(this, object);
canNavigate = true;
if(reverse) {
methods.offsetItemsToBeginning(itemsToScroll);
} else {
methods.offsetItemsToEnd(itemsToScroll);
}
methods.offsetSliderPosition(reverse);
});
}
if(settings.autoPlay.enable) {
methods.setAutoplayInterval();
}
}
},
touchHandler: {
xDown: null,
yDown: null,
handleTouchStart: function(evt) {
this.xDown = evt.touches[0].clientX;
this.yDown = evt.touches[0].clientY;
},
handleTouchMove: function (evt) {
if (!this.xDown || !this.yDown) {
return;
}
var xUp = evt.touches[0].clientX;
var yUp = evt.touches[0].clientY;
var xDiff = this.xDown - xUp;
var yDiff = this.yDown - yUp;
// only comparing xDiff
// compare which is greater against yDiff to determine whether left/right or up/down e.g. if (Math.abs( xDiff ) > Math.abs( yDiff ))
if (Math.abs( xDiff ) > 0) {
if ( xDiff > 0 ) {
// swipe left
methods.scroll(false);
} else {
//swipe right
methods.scroll(true);
}
}
/* reset values */
this.xDown = null;
this.yDown = null;
canNavigate = true;
}
},
/******************************
Utility Functions
*******************************/
getCurrentItemWidth: function() {
return (object.parent().width())/itemsVisible;
},
offsetItemsToBeginning: function(number) {
if(typeof number === 'undefined') { number = 1 }
for(var i = 0; i < number; i++) {
object.children().last().insertBefore(object.children().first());
}
},
offsetItemsToEnd: function(number) {
if(typeof number === 'undefined') { number = 1 }
for(var i = 0; i < number; i++) {
object.children().first().insertAfter(object.children().last());
}
},
offsetSliderPosition: function(reverse) {
var left = parseInt(object.css('left').replace('px', ''));
if (reverse) {
left = left - itemsWidth * itemsToScroll;
} else {
left = left + itemsWidth * itemsToScroll;
}
object.css({
'left': left
});
},
getOffsetPosition: function() {
return parseInt(object.css('left').replace('px', ''));
},
calculateNonInfiniteLeftScroll: function(toScroll) {
methods.clearDisabled();
if(methods.getOffsetPosition() + toScroll >= 0) {
$(settings.navigationTargetSelector).find(".nbs-flexisel-nav-left").addClass('disabled');
return 0;
} else {
return methods.getOffsetPosition() + toScroll;
}
},
calculateNonInfiniteRightScroll: function(toScroll){
methods.clearDisabled();
var negativeOffsetLimit = (itemCount * itemsWidth) - (itemsVisible * itemsWidth);
if(methods.getOffsetPosition() - toScroll <= -negativeOffsetLimit) {
$(settings.navigationTargetSelector).find(".nbs-flexisel-nav-right").addClass('disabled');
return -negativeOffsetLimit;
} else {
return methods.getOffsetPosition() - toScroll;
}
},
setAutoplayInterval: function(){
autoPlayInterval = setInterval(function() {
if (canNavigate) {
methods.scroll(false);
}
}, settings.autoPlay.interval);
},
clearDisabled: function() {
var parent = $(settings.navigationTargetSelector);
parent.find(".nbs-flexisel-nav-left").removeClass('disabled');
parent.find(".nbs-flexisel-nav-right").removeClass('disabled');
}
};
if (methods[options]) { // $("#element").pluginName('methodName', 'arg1', 'arg2');
return methods[options].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof options === 'object' || !options) { // $("#element").pluginName({ option: 1, option:2 });
return methods.init.apply(this);
} else {
$.error( 'Method "' + method + '" does not exist in flexisel plugin!');
}
};
})(jQuery);