-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRightJS-Carousel.js
More file actions
286 lines (237 loc) · 6.99 KB
/
RightJS-Carousel.js
File metadata and controls
286 lines (237 loc) · 6.99 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
/**
* Pretty simple Carousel/Slider
*/
var UICarousel = new Class({
// CSS: wrapper class
wrapper: '.slider',
// CSS: movable container
content: 'UL',
// CSS: buttons
css_previous:'.prev',
css_next: '.next',
css_disabled:'disabled',
css_pager: '.slider-paging',
// internal index: Amount of Items available
index: 0,
// current selected index
current: 0,
// intval time
time: 5000,
// id of widget
id: '',
// autoId
autoId: 0,
// itemWidth
itemWidth: 0,
// internal id's
intvalIds: [],
// rotation is disabled by default
autoRotate: false,
// pager is disabled
is_pager : false,
rotationState: "pause",
// constructor
initialize: function(id) {
this.setId(id);
// check if items available, otherwise return false
if($(this.id).find(this.content + '>li').length===0) {
return false;
}
this.countIndex();
if(this.index>=0) {
this.current=0;
}
this.setWidth(this.getItemWidth());
$(this.id).on("click", this.bubble.bindAsEventListener(this));
// if markup available,... create and insert jump links
if($(this.id).find(this.css_pager).first()) {
var paging_numbers = $(this.id).find(this.css_pager).first().hasClass("paging-numbers");
var pager_items = "";
for(i=0; this.index>=i; i++) {
pager_items += '<span class="jump to-'+i+' ' +((this.current==i) ? "active" : '')+'">'+(paging_numbers ? (i+1) : '')+'</span>';
}
$(this.id).find(this.css_pager).first().insert(pager_items);
this.is_pager = true;
}
if(this.current===0) {
var btn_prev = $(this.id).find(this.css_previous).first();
if(btn_prev) {
btn_prev.addClass(this.css_disabled);
}
}
if($(this.id).hasClass('slider-autoRotate')) {
this.autoRotate = true;
this.rotationState = "play";
this.setRotation();
var btn = $(this.id).find("span.slider-control").first();
if(btn) {
btn.addClass("pause");
}
}
if(this.autoRotate) {
$(window).on("blur", this.rotationWrapper.bindAsEventListener(this, "blur"));
$(window).on("focus", this.rotationWrapper.bindAsEventListener(this, "focus"));
}
},
rotationWrapper: function(that, state) {
if(state == "blur") {
this.removeRotation();
} else if (state == "focus") {
var btn = $(this.id).find('.slider-control').first();
if(btn.hasClass("pause")) {
this.setRotation();
}
}
},
bubble: function(event) {
if(event.target.hasClass('jump')) {
this.pager(event);
} else if(event.target.hasClass('prev')) {
this.previous(event);
} else if(event.target.hasClass('next')) {
this.next(event);
} else if(event.target.hasClass('pause')) {
this.removeRotation(event);
this.toggleButton(event, 'pause');
} else if(event.target.hasClass('play')) {
this.setRotation(event);
this.toggleButton(event, 'play');
}
},
setId: function(id) {
_id = UICarousel.prototype.autoId++;
this.id = 'slider-'+_id;
$(id)._.id=this.id;
},
setRotation: function() {
this.intvalIds[this.id] = setInterval(this.next.bindAsEventListener(this), 3000);
},
removeRotation: function() {
clearInterval(this.intvalIds[this.id]);
},
// IE doesn't support it, therefore we'll need to work around it.
getClassList: function(obj) {
if(typeof obj.classList == "undefined") {
var trimmed = obj.className.replace(/^\s+|\s+$/g, "");
return trimmed.split(/\s+/);
} else {
return obj.classList;
}
},
pager: function(event) {
var classList = this.getClassList(event.target._);
this.current = parseInt( classList[1].substr(3) );
this.jump(this.current);
},
// updates the internal index
countIndex: function() {
this.index = $(this.id).find(this.content + '>li').length-1;
},
// get ItemWidth
getItemWidth: function() {
return $(this.id).find(this.content + '>li').first().dimensions().width;
},
// set internal Item width for calculation purposes
setWidth: function(width) {
this.itemWidth = width;
},
// returns previous & next index
getIndexInfo: function() {
var nextIndex = (this.current<this.index) ? (this.current+1) : this.index;
var prevIndex = (this.current!=0) ? (this.current-1) : 0;
return {next: nextIndex, previous: prevIndex};
},
// does the math to jump to the next slide
next: function() {
var indexInfo = this.getIndexInfo();
var nextPosition = (this.itemWidth * indexInfo.next)*-1;
// if rotation is enabled && and we did reach the last index,
// we need to jump to the first one (or something else)
if(this.autoRotate && this.index==this.current) {
this.current=0;
this.jump(this.current);
} else {
if(indexInfo.next>this.current) {
var btn_prev = $(this.id).find(this.css_previous).first();
if(btn_prev) {
btn_prev.removeClass(this.css_disabled);
}
this.current++;
this.scroll(nextPosition);
}
if(indexInfo.next==this.index) {
var btn_next = $(this.id).find(this.css_next).first();
if(btn_next) {
btn_next.addClass(this.css_disabled);
}
}
}
},
// does the math to jump to the previous slide
previous: function() {
var indexInfo = this.getIndexInfo();
var nextPosition = (this.itemWidth * indexInfo.previous)*-1;
if(indexInfo.previous<this.current) {
$(this.id).find(this.css_next).first().removeClass(this.css_disabled);
this.current--;
this.scroll(nextPosition);
}
if(indexInfo.previous==0) {
$(this.id).find(this.css_previous).first().addClass(this.css_disabled);
}
},
toggleButton: function(event, state) {
if(state == 'pause') {
this.rotationState = "pause";
event.target.removeClass("pause").addClass("play");
} else {
this.rotationState = "play";
event.target.removeClass("play").addClass("pause");
}
},
// unused:
setIntervalTime: function(timeName) {
switch(timeName) {
case 'x-slow':
this.time = 10000;
break;
case 'slow':
this.time = 7500;
break;
case 'medium':
this.time = 5000;
break;
case 'fast':
this.time = 2500;
break;
case 'x-fast':
this.time = 1500;
break;
}
},
// to write
jump: function(targetIndex) {
var indexInfo = this.getIndexInfo();
var nextPosition = (this.itemWidth * targetIndex)*-1;
this.scroll(nextPosition);
},
// fx
scroll: function(amount) {
// if autoRotate is enabled, it's a good thing to delay the execution
if(this.autoRotate) {
// but only if the rotation is not paused ;)
if($(this.id).find("span.play").length===0) {
this.removeRotation();
this.setRotation();
}
}
if(this.is_pager) {
$(this.id).find(this.css_pager + '>.active').first().removeClass('active');
$(this.id).find(this.css_pager + '>.to-'+this.current).first().addClass('active');
}
var container = $(this.id).find(this.content).first();
new Fx.Morph(container).start({
left: amount+'px'
});
}
});