forked from thomhines/square1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquare1.js
461 lines (363 loc) · 14.7 KB
/
square1.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
* Square1 Slider
* Copyright (c) 2016 Thom Hines
* Licensed under MIT.
* @author Thom Hines
* https://github.com/thomhines/square1
* @version 0.5.0
*/
filter_gallery = '';
(function($){
$.fn.square1 = function(options) {
var $this = this;
var _this = $(this)[0];
// _this.settings = null;
// _this.square1_interval = null;
_this.reset_position_timeout = null;
var load_image_timeout = null;
var slideshow_hover = false;
// Stop plugin execution if selected elements aren't present on page
if($this.length < 1) return;
// Initialize settings and defaults
if(!_this.settings) {
_this.settings = $.extend({
width: '', // options: any specific measurement. Blank values will default to whatever is set in CSS.
height: '',
fill_mode: 'cover', // options: 'contain' or 'cover'
scale_from: 'center center', // options: all values that work for CSS background-position property
background: 'none',
lazy_load: false,
auto_start: true,
start_delay: 0,
slide_duration: 4000,
transition_time: 500,
pause_on_hover: false,
keyboard: true,
gestures: true,
theme: 'dark',
prev_next_nav: 'inside', // options: 'inside', 'outside', 'hover', 'none'
dots_nav: 'inside', // options: 'inside', 'outside', 'hover', 'none'
caption: 'outside', // options: 'inside', 'outside', 'hover', 'none'
onLoad: function() {},
onPlay: function() {},
onStop: function() {},
onChange: function() {}
}, options);
}
// Handle slideshow argument commands (eg. $('.slideshow').square1('play');)
if(options == 'play') {
run_slideshow();
return;
}
if(options == 'stop') {
_this.settings['auto_start'] = false;
stop_slideshow();
return;
}
if(options == 'next') {
next_image();
if(_this.settings['auto_start']) reset_interval();
return;
}
if(options == 'prev') {
prev_image();
if(_this.settings['auto_start']) reset_interval();
return;
}
// If argument value is an integer, go to that image
if(Math.floor(options) === options && +options === options) {
jump_to_image(options-1);
if(_this.settings['auto_start']) reset_interval();
return;
}
// Style slideshow elements
$($this).css({
position: 'relative',
width: _this.settings['width'],
height: _this.settings['height'],
background: _this.settings['background'],
});
// Create slideshow elements
$this.addClass('square1 loading');
if(_this.settings['keyboard'] && !$this.attr('tabindex')) $this.attr('tabindex', 0);
$this.wrapInner('<div class="slides_container" />')
$this.find('.slides_container').children().wrap('<div class="image_wrapper" />'); // Surround all direct decendents with <div> (so that this can work with images or other elements, such as <a> or <ul>)
$('.image_wrapper', $this).each(function() {
$img = $(this).find('img');
$(this).attr('scale-from', $img.attr('scale-from'))
.attr('scale-from-mobile', $img.attr('scale-from-mobile'))
.attr('space', $img.attr('space'));
});
// load slider images one at at time, starting with first
loadImage($('.image_wrapper:first-child', $this))
// Hide all but first image, then fade them in one at a time.
$('.image_wrapper:first', $this).addClass('current_slide');
$('.image_wrapper:not(.current_slide)', $this).hide();
$($this).append('<div class="square1_caption"></div>');
// Add slideshow navigation controls (if there is more than 1 image)
if($($this).find('.image_wrapper').length > 1) $($this).append('<div class="square1_controls"><span class="square1_prev_image">Previous</span><span class="square1_next_image">Next</span><div class="square1_dots"></div></div>');
// Add loading
$($this).append('<div class="square1_spinner"></div>');
// For each img, add dot to dot nav
var x = 0;
$('.image_wrapper', $this).each(function() {
$('.square1_dots', $this).append('<span data-image-num="' + x + '"></span>');
x++;
});
$('.square1_dots span:first-child', $this).addClass('current')
if(_this.settings['animation'] == 'slide') {
$this.addClass('slide_animation');
$('.image_wrapper, .wrap_placeholder', $this).show();
var x = 0;
$('.image_wrapper, .wrap_placeholder', $this).each(function() {
$(this).css({left: x * 100 + "%", transitionDuration: _this.settings['transition_time'] + 'ms'});
x++;
});
// Create wrap placeholders
$('.image_wrapper', $this).last().clone().removeClass('image_wrapper').addClass('wrap_placeholder').css({left: '-100%'}).appendTo($('.slides_container', $this))
$('.image_wrapper', $this).first().clone().removeClass('image_wrapper').addClass('wrap_placeholder').css({left: ($('.image_wrapper', $this).length * 100) +'%'}).appendTo($('.slides_container', $this))
}
// Customize behavior styles
$($this).addClass('fill_mode-' + _this.settings['fill_mode']);
$($this).addClass('theme-' + _this.settings['theme']);
$($this).addClass('prev_next_nav-' + _this.settings['prev_next_nav']);
$($this).addClass('dots_nav-' + _this.settings['dots_nav']);
$($this).addClass('caption-' + _this.settings['caption']);
// Events
$('.square1_prev_image', $this).click(function() {
prev_image();
});
$('.square1_next_image', $this).click(function() {
next_image();
});
$('.square1_dots span', $this).click(function() {
jump_to_image($(this).data('image-num'));
});
$($this).keydown(function(e) {
if(e.keyCode == 37 || e.keyCode == 38) prev_image();
else if(e.keyCode == 39 || e.keyCode == 40) next_image();
else return;
e.preventDefault();
e.stopPropagation();
});
if(_this.settings['gestures']) {
var touchstartX, touchstartY, touchstartTime, touchMove;
$($this).on('touchstart', function(e) {
touchstartX = e.touches[0].screenX;
touchstartY = e.touches[0].screenY;
touchstartTime = new Date();
});
$($this).on('touchmove', function(e) {
touchMove = e;
});
$($this).on('touchend', function(e) {
deltaX = touchMove.touches[0].screenX - touchstartX;
deltaY = touchMove.touches[0].screenY - touchstartY;
touchDuration = new Date() - touchstartTime;
if(Math.abs(deltaX / deltaY) < 2) return;
if(Math.abs(deltaX) < 30) return;
if(touchDuration > 800) return;
if(deltaX > 0) prev_image();
else next_image();
});
}
// Pause on hover
$($this).on('mouseenter', function() {
if(_this.settings['pause_on_hover']) stop_slideshow();
});
$($this).on('mouseleave', function() {
if(_this.settings['pause_on_hover'] && _this.settings['auto_start']) run_slideshow();
});
// Start slideshow (with pause at the beginning)
if(_this.settings['auto_start']) setTimeout(run_slideshow, _this.settings['start_delay']);
update_caption();
// HELPER FUNCTIONS
// Load BG images one at a time
function loadImage($wrapper) {
// skip if all images are loaded
if($($this).find('.image_wrapper:not(.image_loaded), .wrap_placeholder:not(.image_loaded)').length < 1) return
$img = $wrapper.find('img');
if($img.attr('data-src')) $img.attr('src', $img.attr('data-src')).attr('srcset', $img.attr('data-srcset'));
clearTimeout(load_image_timeout);
if($img.prop('currentSrc')) var img_url = $img.prop('currentSrc');
else var img_url = $img.attr('src');
if($img.prop('currentSrc')) var img_url = $img.prop('currentSrc');
else var img_url = $img.attr('src');
$wrapper.css('background-image', "url(" + img_url + ")");
// Check to see when images are finished loading
img = new Image();
img.onload = function() {
$(this).css('display', ''); // Show image as background instead
if(_this.settings['lazy_load']) do_nothing = 1;
else if($wrapper.next().hasClass('image_wrapper')) {
load_image_timeout = setTimeout(function() {
loadImage($wrapper.next());
}, 1000);
}
else if($wrapper.closest('.square1').find('.image_wrapper').length) {
load_image_timeout = setTimeout(function() {
loadImage($wrapper.closest('.square1').find('.image_wrapper').first());
}, 1000);
}
$wrapper.addClass('image_loaded')
if($wrapper.closest('.square1').find('.image_wrapper:not(.image_loaded)').length < 1) {
$this.attr('images_loaded', '').removeClass('loading');
_this.settings['onLoad']();
}
};
img.src = img_url;
if (img.complete) {
img.onload();
}
$('.wrap_placeholder[background-image="'+img_url+'"]', $this).css('background-image', "url(" + img_url + ")");
}
function run_slideshow() {
_this.square1_interval = 1;
reset_interval();
update_dots_nav();
_this.settings['onPlay']();
}
show_trace = 0
function stop_slideshow() {
clearInterval(_this.square1_interval);
_this.square1_interval = null;
_this.settings['onStop']();
show_trace = 1
}
// Reset timer for slideshow (to prevent weird jumps when changing slides, for instance)
function reset_interval() {
if(!_this.square1_interval) return;
clearInterval(_this.square1_interval);
clearInterval(_this.square1_interval);
_this.square1_interval = setInterval(next_image, _this.settings['slide_duration'] + _this.settings['transition_time']);
// $($this).data('interval', _this.square1_interval);
}
var loop_slider = 0
function next_image() {
var curr_slide_index = $('.current_slide', $this).index();
var next_slide_index = $('.current_slide', $this).next('.image_wrapper').index();
if(filter_gallery) {
next_slide_index = $('.current_slide', $this).nextAll('.image_wrapper[space*="'+filter_gallery+'"]').index()
if(next_slide_index < 0 && $('.image_wrapper', $this).first().is('.image_wrapper[space*="'+filter_gallery+'"]')) next_slide_index = $('.image_wrapper', $this).first().index()
if(next_slide_index < 0) next_slide_index = $('.image_wrapper', $this).first().nextAll('.image_wrapper[space*="'+filter_gallery+'"]').index();
}
// If last slide, jump to first slide
if(next_slide_index < 0) {
loop_slider = 1;
jump_to_image(0);
}
// Otherwise, show next slide and hide the current
else {
jump_to_image(next_slide_index);
}
}
function prev_image() {
var curr_slide_index = $('.current_slide', $this).index();
var prev_slide_index = $('.current_slide', $this).prev('.image_wrapper').index();
if(filter_gallery) {
prev_slide_index = $('.current_slide', $this).prevAll('.image_wrapper[space*="'+filter_gallery+'"]').index()
if(prev_slide_index < 0 && $('.image_wrapper', $this).last().is('.image_wrapper[space*="'+filter_gallery+'"]')) prev_slide_index = $('.image_wrapper', $this).last().index()
if(prev_slide_index < 0) prev_slide_index = $('.image_wrapper', $this).first().prevAll('.image_wrapper[space*="'+filter_gallery+'"]').index();
}
// If first slide, show final slide
if(prev_slide_index < 0) {
loop_slider = 1;
jump_to_image($('.image_wrapper', $this).last().index());
}
// Otherwise, show previous slide
else jump_to_image(prev_slide_index);
}
function jump_to_image(image_num) {
// Check to see if image is loaded. If not, load it and try again
if(!$('.image_wrapper', $this).eq(image_num).hasClass('image_loaded')) {
loadImage($('.image_wrapper', $this).eq(image_num))
setTimeout(function() {
jump_to_image(image_num)
}, 100)
return
}
// Cancel all previous animations
$('.image_wrapper', $this).finish().clearQueue();
// Slide animation
if(_this.settings['animation'] == 'slide') {
reset_slide_position = false;
target_slide = image_num
last_slide = $('.image_wrapper', $this).last().index()
clearTimeout(_this.reset_position_timeout)
$('.image_wrapper, .wrap_placeholder', $this).removeClass('no_transition')
// Wrap animations
if(loop_slider && image_num == 0) {
reset_slide_position = "first"
target_slide = last_slide + 1
}
else if(loop_slider && image_num == last_slide) {
if(!$('.wrap_placeholder', $this).first().hasClass('image_loaded')) loadImage($('.wrap_placeholder', $this).first())
reset_slide_position = "last"
target_slide = -1
}
loop_slider = 0
$('.current_slide', $this).removeClass('current_slide')
$('.image_wrapper', $this).eq(image_num).addClass('current_slide');
translate_target = target_slide * -100;
$('.image_wrapper, .wrap_placeholder', $this).css('transform','translateX('+translate_target+'%)')
if(reset_slide_position == "first") {
_this.reset_position_timeout = setTimeout(function() {
$('.image_wrapper, .wrap_placeholder', $this).addClass('no_transition')
$('.image_wrapper, .wrap_placeholder', $this).css('transform','translateX(0%)');
}, _this.settings['transition_time'])
}
if(reset_slide_position == "last") {
_this.reset_position_timeout = setTimeout(function() {
$('.image_wrapper, .wrap_placeholder', $this).addClass('no_transition')
$('.image_wrapper, .wrap_placeholder', $this).css('transform','translateX(-'+ (last_slide * 100) +'%)');
}, _this.settings['transition_time'])
}
}
// Fade animation
else {
// Different fading necessary for different fill modes. Crossfading in 'cover' mode requires that both elements are visible at the same time
if(_this.settings['fill_mode'] == 'cover') {
// If index of new image is bigger than current, fade in new image and hide current
if(image_num > $('.current_slide', $this).index()) {
$('.current_slide', $this).removeClass('current_slide');
$('.image_wrapper', $this).eq(image_num).addClass('current_slide').fadeIn(_this.settings['transition_time'], function() { $('.image_wrapper:not(.current_slide)', $this).hide(); });
}
// Otherwise, show new image and fade out current
else if(image_num < $('.current_slide', $this).index()) {
$('.current_slide', $this).removeClass('current_slide').fadeOut(_this.settings['transition_time']);
$('.image_wrapper', $this).eq(image_num).addClass('current_slide').show();
}
}
// Whereas 'contain' mode looks weird when different sized images stay on the screen too long
else {
$('.current_slide', $this).removeClass('current_slide').fadeOut(_this.settings['transition_time']);
$('.image_wrapper', $this).eq(image_num).addClass('current_slide').fadeIn(_this.settings['transition_time']);
}
}
reset_interval();
update_caption();
update_dots_nav();
_this.settings['onChange']();
}
function update_caption() {
if(_this.settings['caption'] == 'none') {
$('.square1_caption', $this).remove();
return;
}
$('.square1_caption', $this).fadeOut(_this.settings['transition_time'], function() {
setTimeout(function() { // Add a delay so that new image can load before getting new caption
$('.square1_caption', $this).html($('.current_slide img', $this).attr('alt')).fadeIn(_this.settings['transition_time'] - 200);
}, 10)
});
}
function update_dots_nav() {
if(_this.settings['dots_nav'] == 'none') {
$('.square1_dots', $this).remove();
return;
}
$('.square1_dots span', $this).removeClass('current');
$('.square1_dots span[data-image-num="' + $('.current_slide', $this).index() + '"]', $this).addClass('current');
}
return $this;
}; // $.fn.square1
}(jQuery));