-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.flexisel.js
More file actions
322 lines (276 loc) · 12.9 KB
/
jquery.flexisel.js
File metadata and controls
322 lines (276 loc) · 12.9 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
(function ($) {
$.fn.flexisel = function(options) {
var defaults = $.extend({
visibleItems : 4,
animationSpeed : 200,
autoPlay : false,
autoPlaySpeed : 3000,
pauseOnHover : true,
setMaxWidthAndHeight : false,
enableResponsiveBreakpoints : true,
clone : true,
responsiveBreakpoints : {
portrait: {
changePoint:480,
visibleItems: 1
},
landscape: {
changePoint:640,
visibleItems: 2
},
tablet: {
changePoint:768,
visibleItems: 3
}
}
}, options);
/******************************
Private Variables
*******************************/
var object = $(this);
var settings = $.extend(defaults, options);
var itemsWidth; // Declare the global width of each item in carousel
var canNavigate = true;
var itemsVisible = settings.visibleItems; // Get visible items
var totalItems = object.children().length; // Get number of elements
var responsivePoints = [];
/******************************
Public Methods
*******************************/
var methods = {
init : function() {
return this.each(function() {
methods.appendHTML();
methods.setEventHandlers();
methods.initializeItems();
});
},
/******************************
Initialize Items
Fully initialize everything. Plugin is loaded and ready after finishing execution
*******************************/
initializeItems : function() {
var listParent = object.parent();
var innerHeight = listParent.height();
var childSet = object.children();
methods.sortResponsiveObject(settings.responsiveBreakpoints);
var innerWidth = listParent.width(); // Set widths
itemsWidth = (innerWidth) / itemsVisible;
childSet.width(itemsWidth);
if (settings.clone) {
childSet.last().insertBefore(childSet.first());
childSet.last().insertBefore(childSet.first());
object.css({
'left' : -itemsWidth
});
}
object.fadeIn();
$(window).trigger("resize"); // needed to position arrows correctly
},
/******************************
Append HTML
Add additional markup needed by plugin to the DOM
*******************************/
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.setMaxWidthAndHeight) {
var baseWidth = $(".nbs-flexisel-item img").width();
var baseHeight = $(".nbs-flexisel-item img").height();
$(".nbs-flexisel-item img").css("max-width", baseWidth);
$(".nbs-flexisel-item img").css("max-height", baseHeight);
}
$("<div class='nbs-flexisel-nav-left'></div><div class='nbs-flexisel-nav-right'></div>").insertAfter(object);
if (settings.clone) {
var cloneContent = object.children().clone();
object.append(cloneContent);
}
},
/******************************
Set Event Handlers
Set events: click, resize, etc
*******************************/
setEventHandlers : function() {
var listParent = object.parent();
var childSet = object.children();
var leftArrow = listParent.find($(".nbs-flexisel-nav-left"));
var rightArrow = listParent.find($(".nbs-flexisel-nav-right"));
$(window).on("resize", function(event) {
methods.setResponsiveEvents();
var innerWidth = $(listParent).width();
var innerHeight = $(listParent).height();
itemsWidth = (innerWidth) / itemsVisible;
childSet.width(itemsWidth);
if (settings.clone) {
object.css({
'left' : -itemsWidth
});
}else {
object.css({
'left' : 0
});
}
var halfArrowHeight = (leftArrow.height()) / 2;
var arrowMargin = (innerHeight / 2) - halfArrowHeight;
leftArrow.css("top", arrowMargin + "px");
rightArrow.css("top", arrowMargin + "px");
});
$(leftArrow).on("click", function(event) {
methods.scrollLeft();
});
$(rightArrow).on("click", function(event) {
methods.scrollRight();
});
if (settings.pauseOnHover == true) {
$(".nbs-flexisel-item").on({
mouseenter : function() {
canNavigate = false;
},
mouseleave : function() {
canNavigate = true;
}
});
}
if (settings.autoPlay == true) {
setInterval(function() {
if (canNavigate == true)
methods.scrollRight();
}, settings.autoPlaySpeed);
}
},
/******************************
Set Responsive Events
Set breakpoints depending on responsiveBreakpoints
*******************************/
setResponsiveEvents: function() {
var contentWidth = $('html').width();
if(settings.enableResponsiveBreakpoints) {
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;
break;
}
else { // determine custom responsiveBreakpoint to use
if(contentWidth < responsivePoints[i].changePoint) {
itemsVisible = responsivePoints[i].visibleItems;
break;
}
else
continue;
}
}
}
},
/******************************
Sort Responsive Object
Gets all the settings in resposiveBreakpoints and sorts them into an array
*******************************/
sortResponsiveObject: function(obj) {
var responsiveObjects = [];
for(var i in obj) {
responsiveObjects.push(obj[i]);
}
responsiveObjects.sort(function(a, b) {
return a.changePoint - b.changePoint;
});
responsivePoints = responsiveObjects;
},
/******************************
Scroll Left
*******************************/
scrollLeft : function() {
if (object.position().left < 0) {
if (canNavigate == true) {
canNavigate = false;
var listParent = object.parent();
var innerWidth = listParent.width();
itemsWidth = (innerWidth) / itemsVisible;
var childSet = object.children();
object.animate({
'left' : "+=" + itemsWidth
}, {
queue : false,
duration : settings.animationSpeed,
easing : "linear",
complete : function() {
if (settings.clone) {
childSet.last().insertBefore(
childSet.first()); // Get the first list item and put it after the last list item (that's how the infinite effects is made)
}
methods.adjustScroll();
canNavigate = true;
}
});
}
}
},
/******************************
Scroll Right
*******************************/
scrollRight : function() {
var listParent = object.parent();
var innerWidth = listParent.width();
itemsWidth = (innerWidth) / itemsVisible;
var difObject = (itemsWidth - innerWidth);
var objPosition = (object.position().left + ((totalItems-itemsVisible)*itemsWidth)-innerWidth);
if((difObject < Math.ceil(objPosition)) && (!settings.clone)){
if (canNavigate == true) {
canNavigate = false;
object.animate({
'left' : "-=" + itemsWidth
}, {
queue : false,
duration : settings.animationSpeed,
easing : "linear",
complete : function() {
methods.adjustScroll();
canNavigate = true;
}
});
}
} else if(settings.clone){
if (canNavigate == true) {
canNavigate = false;
var childSet = object.children();
object.animate({
'left' : "-=" + itemsWidth
}, {
queue : false,
duration : settings.animationSpeed,
easing : "linear",
complete : function() {
childSet.first().insertAfter(childSet.last()); // Get the first list item and put it after the last list item (that's how the infinite effects is made)
methods.adjustScroll();
canNavigate = true;
}
});
}
};
},
/******************************
Adjust Scroll
*******************************/
adjustScroll : function() {
var listParent = object.parent();
var childSet = object.children();
var innerWidth = listParent.width();
itemsWidth = (innerWidth) / itemsVisible;
childSet.width(itemsWidth);
if (settings.clone) {
object.css({
'left' : -itemsWidth
});
}
}
};
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);