-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathslider.js
355 lines (301 loc) · 8.78 KB
/
slider.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
(function ($, Vel) {
'use strict';
let _defaults = {
indicators: true,
height: 400,
duration: 500,
interval: 6000
};
/**
* @class
*
*/
class Slider {
/**
* Construct Slider instance and set up overlay
* @constructor
* @param {Element} el
* @param {Object} options
*/
constructor(el, options) {
// If exists, destroy and reinitialize
if (!!el.M_Slider) {
el.M_Slider.destroy();
}
this.el = el;
this.$el = $(el);
this.el.M_Slider = this;
/**
* Options for the modal
* @member Slider#options
* @prop {Boolean} [indicators=true] - Show indicators
* @prop {Number} [height=400] - height of slider
* @prop {Number} [duration=500] - Length in ms of slide transition
* @prop {Number} [interval=6000] - Length in ms of slide interval
*/
this.options = $.extend({}, Slider.defaults, options);
// setup
this.$slider = this.$el.find('.slides');
this.$slides = this.$slider.children('li');
this.activeIndex = this.$slider.find('.active').index();
if (this.activeIndex != -1) {
this.$active = this.$slides.eq(this.activeIndex);
}
this._setSliderHeight();
// Set initial positions of captions
this.$slides.find('.caption').each((el) => {
this._animateCaptionIn(el, 0);
});
// Move img src into background-image
this.$slides.find('img').each((el) => {
let placeholderBase64 = 'data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
if ($(el).attr('src') !== placeholderBase64) {
$(el).css('background-image', 'url("' + $(el).attr('src') + '")' );
$(el).attr('src', placeholderBase64);
}
});
this._setupIndicators();
// Show active slide
if (this.$active) {
this.$active.css('display', 'block');
}
else {
this.$slides.first().addClass('active');
Vel(
this.$slides.first()[0],
{opacity: 1},
{duration: this.options.duration, queue: false, easing: 'easeOutQuad'}
);
this.activeIndex = 0;
this.$active = this.$slides.eq(this.activeIndex);
// Update indicators
if (this.options.indicators) {
this.$indicators.eq(this.activeIndex).addClass('active');
}
}
// Adjust height to current slide
this.$active.find('img').each((el) => {
Vel(
this.$active.find('.caption')[0],
{opacity: 1, translateX: 0, translateY: 0},
{duration: this.options.duration, queue: false, easing: 'easeOutQuad'}
);
});
this._setupEventHandlers();
// auto scroll
this.start();
}
static get defaults() {
return _defaults;
}
static init($els, options) {
let arr = [];
$els.each(function() {
arr.push(new Slider(this, options));
});
return arr;
}
/**
* Get Instance
*/
static getInstance(el) {
let domElem = !!el.jquery ? el[0] : el;
return domElem.M_Slider;
}
/**
* Teardown component
*/
destroy() {
this.pause();
this._removeIndicators();
this._removeEventHandlers();
this.el.M_Slider = undefined;
}
/**
* Setup Event Handlers
*/
_setupEventHandlers() {
this._handleIntervalBound = this._handleInterval.bind(this);
this._handleIndicatorClickBound = this._handleIndicatorClick.bind(this);
if (this.options.indicators) {
this.$indicators.each((el) => {
el.addEventListener('click', this._handleIndicatorClickBound);
});
}
}
/**
* Remove Event Handlers
*/
_removeEventHandlers() {
if (this.options.indicators) {
this.$indicators.each((el) => {
el.removeEventListener('click', this._handleIndicatorClickBound);
});
}
}
/**
* Handle indicator click
* @param {Event} e
*/
_handleIndicatorClick(e) {
let currIndex = $(e.target).index();
this.set(currIndex);
}
/**
* Handle Interval
*/
_handleInterval() {
let newActiveIndex = this.$slider.find('.active').index();
if (this.$slides.length === newActiveIndex + 1) newActiveIndex = 0; // loop to start
else newActiveIndex += 1;
this.set(newActiveIndex);
}
/**
* Animate in caption
* @param {Element} caption
* @param {Number} duration
*/
_animateCaptionIn(caption, duration) {
let velocityOptions = {
opacity: 0
};
if ($(caption).hasClass('center-align')) {
velocityOptions.translateY = -100;
} else if ($(caption).hasClass('right-align')) {
velocityOptions.translateX = 100;
} else if ($(caption).hasClass('left-align')) {
velocityOptions.translateX = -100;
}
Vel(
caption,
velocityOptions,
{duration: duration, queue: false}
);
}
/**
* Set height of slider
*/
_setSliderHeight() {
// If fullscreen, do nothing
if (!this.$el.hasClass('fullscreen')) {
if (this.options.indicators) {
// Add height if indicators are present
this.$el.css('height', (this.options.height + 40) + 'px');
}
else {
this.$el.css('height', this.options.height + 'px');
}
this.$slider.css('height', this.options.height + 'px');
}
}
/**
* Setup indicators
*/
_setupIndicators() {
if (this.options.indicators) {
this.$indicators = $('<ul class="indicators"></ul>');
this.$slides.each((el, index) => {
let $indicator = $('<li class="indicator-item"></li>');
this.$indicators.append($indicator[0]);
});
this.$el.append(this.$indicators[0]);
this.$indicators = this.$indicators.children('li.indicator-item');
}
}
/**
* Remove indicators
*/
_removeIndicators() {
this.$el.find('ul.indicators').remove();
}
/**
* Cycle to nth item
* @param {Number} index
*/
set(index) {
// Wrap around indices.
if (index >= this.$slides.length) index = 0;
else if (index < 0) index = this.$slides.length -1;
// Only do if index changes
if (this.activeIndex != index) {
this.$active = this.$slides.eq(this.activeIndex);
let $caption = this.$active.find('.caption');
this.$active.removeClass('active');
Vel(
this.$active[0],
{opacity: 0},
{duration: this.options.duration, queue: false, easing: 'easeOutQuad',
complete: (() => {
this.$slides.not('.active').each((el) => {
Vel(
el,
{opacity: 0, translateX: 0, translateY: 0},
{duration: 0, queue: false}
);
});
}).bind(this)
}
);
this._animateCaptionIn($caption[0], this.options.duration);
// Update indicators
if (this.options.indicators) {
this.$indicators.eq(this.activeIndex).removeClass('active');
this.$indicators.eq(index).addClass('active');
}
Vel(
this.$slides.eq(index)[0],
{opacity: 1},
{duration: this.options.duration, queue: false, easing: 'easeOutQuad'}
);
Vel(
this.$slides.eq(index).find('.caption')[0],
{opacity: 1, translateX: 0, translateY: 0},
{duration: this.options.duration, delay: this.options.duration, queue: false, easing: 'easeOutQuad'}
);
this.$slides.eq(index).addClass('active');
this.activeIndex = index;
// Reset interval
this.start();
}
}
/**
* Pause slider interval
*/
pause() {
clearInterval(this.interval);
}
/**
* Start slider interval
*/
start() {
clearInterval(this.interval);
this.interval = setInterval(
this._handleIntervalBound, this.options.duration + this.options.interval
);
}
/**
* Move to next slide
*/
next() {
let newIndex = this.activeIndex + 1;
// Wrap around indices.
if (newIndex >= this.$slides.length) newIndex = 0;
else if (newIndex < 0) newIndex = this.$slides.length -1;
this.set(newIndex);
}
/**
* Move to previous slide
*/
prev() {
let newIndex = this.activeIndex - 1;
// Wrap around indices.
if (newIndex >= this.$slides.length) newIndex = 0;
else if (newIndex < 0) newIndex = this.$slides.length -1;
this.set(newIndex);
}
}
M.Slider = Slider;
if (M.jQueryLoaded) {
M.initializeJqueryWrapper(Slider, 'slider', 'M_Slider');
}
}(cash, M.Vel));