-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
429 lines (342 loc) · 13.8 KB
/
script.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
const isSrcsetSupported = 'srcset' in new Image();
const swipingThreshold = 5;
let $lightbox;
let images = [];
let currentIndex = 0;
let wasSwiping = false;
$(() => {
initGallery();
createLightbox();
});
function initGallery() {
const $galleryItems = $('.gallery-item');
const $galleryThumbs = $galleryItems.find('.thumb');
const loadThumbnail = target => {
// get the src and srcset from the dataset of the gallery thumb
const src = target.dataset.src;
const srcset = target.dataset.srcset;
// create a temporary image
const tempImage = new Image();
// set the src or srcset of the temp img to preload the actual image file
if (isSrcsetSupported && srcset) {
tempImage.srcset = srcset;
} else if (src) {
tempImage.src = src;
}
// when the temp image is loaded, set the src or srcset to the gallery thumb
tempImage.onload = function () {
if (tempImage.srcset) {
target.srcset = srcset;
} else if (src) {
target.src = src;
}
target.classList.remove('placeholder');
}
};
if ('IntersectionObserver' in window) {
const observerOptions = {
rootMargin: '200px 0px'
}
const handleIntersectionObserver = entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
loadThumbnail(entry.target);
intersectionObserver.unobserve(entry.target);
}
});
};
const intersectionObserver = new IntersectionObserver(handleIntersectionObserver, observerOptions);
$galleryThumbs.each((i, el) => intersectionObserver.observe(el));
} else {
// Fallback for unsupported browsers
$galleryThumbs.each((i, el) => loadThumbnail(el));
}
$galleryItems.on('click', e => {
const $currentTarget = $(e.currentTarget);
const $currentGallery = $currentTarget.closest('.gallery');
const itemIndex = $currentTarget.index();
openLightbox($currentGallery, itemIndex);
initSlides();
addLightboxEventListeners();
});
}
function openLightbox($currentGallery, targetIndex) {
$lightbox.addClass('open');
$lightbox.parent('.lightbox-wrapper').fadeIn('fast');
images = [];
$currentGallery.find('.gallery-item').each((i, element) => {
const $currentImageEl = $(element).find('img');
const currentItem = {
src: $currentImageEl.data('image') || $currentImageEl.data('src'),
srcFallback: $currentImageEl.data('image-fallback'),
srcset: $currentImageEl.data('image-srcset'),
title: $currentImageEl.data('title')
}
images.push(currentItem);
});
currentIndex = targetIndex;
showInitialImage(targetIndex);
updateLightboxHeader(targetIndex);
}
function showInitialImage(index) {
const $prevSlide = $lightbox.find('.lightbox-slide[data-state="prev"]');
const $currentSlide = $lightbox.find('.lightbox-slide[data-state="current"]');
const $nextSlide = $lightbox.find('.lightbox-slide[data-state="next"]');
const $currentImage = $currentSlide.find('.lightbox-image');
const $spinner = $currentSlide.find('.spinner');
loadImage($currentSlide, index);
$currentImage.hide();
$spinner.show();
$currentImage.on('load.currentImage', e => {
loadImage($prevSlide, index - 1);
loadImage($nextSlide, index + 1);
$currentImage.off('load.currentImage');
});
}
function createLightbox() {
// ------------------------- //
// Create DOM Elements,
// Append Lightbox to Body
// ------------------------- //
const $lightboxWrapper = $('<div class="lightbox-wrapper">');
$lightbox = $('<div class="lightbox">');
// Header
const $lightboxHeader = $('<div class="lightbox-header">');
const $lightboxNumbers = $('<div class="lightbox-numbers"></div>');
const $lightboxTitle = $('<div class="lightbox-title"></div>');
const $lightboxClose = $('<button type="button" class="lightbox-close" aria-label="Close"></button>');
$lightboxHeader.append($lightboxNumbers, $lightboxTitle, $lightboxClose);
$lightbox.append($lightboxHeader);
// Slides Wrapper
const $slidesWrapper = $('<div class="lightbox-slides-wrapper"></div>');
$lightbox.append($slidesWrapper);
// Slides
const $prevSlide = $('<div class="lightbox-slide" data-state="prev"></div>');
const $currentSlide = $('<div class="lightbox-slide" data-state="current"></div>');
const $nextSlide = $('<div class="lightbox-slide" data-state="next"></div>');
$slidesWrapper.append($prevSlide, $currentSlide, $nextSlide);
// Image
const $lightboxImage = $('<img class="lightbox-image" src="" alt="" draggable="false">');
$currentSlide.append($lightboxImage);
$prevSlide.append($lightboxImage.clone());
$nextSlide.append($lightboxImage.clone());
// Loading Spinner
const $spinner = $('<div class="spinner spinner-border" role="status"><span class="sr-only">Loading... </span></div>');
$currentSlide.append($spinner);
$prevSlide.append($spinner.clone());
$nextSlide.append($spinner.clone());
// Arrows
const $lightboxArrowLeft = $('<div class="lightbox-arrow arrow-left"></div>');
const $lightboxArrowRight = $('<div class="lightbox-arrow arrow-right"></div>');
$lightbox.append($lightboxArrowLeft);
$lightbox.append($lightboxArrowRight);
// Footer
const $lightboxFooter = $('<div class="lightbox-footer">');
$lightbox.append($lightboxFooter);
// append lightbox to body
$lightbox.appendTo($lightboxWrapper);
$lightboxWrapper.appendTo($('body'));
}
function addLightboxEventListeners() {
// close lightbox when clicking on background
$lightbox.find('.lightbox-slide').on('click', e => {
if (e.currentTarget == e.target && !wasSwiping) closeLightbox();
});
// close lightbox when clicking on close button
$lightbox.find('.lightbox-close').on('click', e => {
closeLightbox();
});
}
function closeLightbox() {
const $lightboxWrapper = $('.lightbox-wrapper');
const $lightboxImage = $lightbox.find('.lightbox-image');
// close lightbox
$lightboxWrapper.removeClass('open').fadeOut('fast', () => {
$lightboxImage.attr('src', '');
$lightboxImage.attr('srcset', '');
});
// remove lightbox event listeners
$lightbox.find('.lightbox-slide').off();
$lightbox.find('.lightbox-close').off();
$lightbox.find('.lightbox-arrow').off();
$(document).off('keydown.lightbox');
}
// try avoiding jQuery in mouse and touch event handlers to improve performance
function initSlides() {
const transitionDuration = 400;
let distance = 0;
let startPos = 0;
let slideWidth = 0;
let $currentSlide;
let currentSlideEl;
let prevSlideEl;
let nextSlideEl;
const updateSlideVariables = () => {
$currentSlide = $('.lightbox-slide[data-state="current"]');
currentSlideEl = $currentSlide[0];
prevSlideEl = document.querySelector('.lightbox-slide[data-state="prev"]');
nextSlideEl = document.querySelector('.lightbox-slide[data-state="next"]');
}
updateSlideVariables();
const handleSlideMove = event => {
const currentPos = event.type == 'touchmove' ? event.touches[0].clientX : event.clientX;
distance = currentPos - startPos;
if (distance < -swipingThreshold || distance > swipingThreshold) wasSwiping = true;
// move current slide and adjust opacity
currentSlideEl.style.transform = `translateX(${distance}px)`;
currentSlideEl.style.opacity = mapRange(Math.abs(distance), 0, slideWidth, 1, 0);
// TODO: reset slide if (currentPos > slideWidth || currentPos < 0) (not sure if necessary)
if (distance < 0) {
// move next slide and adjust opacity
nextSlideEl.style.transform = `translateX(${slideWidth + distance}px)`;
nextSlideEl.style.opacity = mapRange(Math.abs(distance), 0, slideWidth, 0, 1);
} else {
// move previous slide and adjust opacity
prevSlideEl.style.transform = `translateX(${distance - slideWidth}px)`;
prevSlideEl.style.opacity = mapRange(Math.abs(distance), 0, slideWidth, 0, 1);
}
}
const handleMouseDownOrTouchStart = event => {
startPos = event.type == 'touchstart' ? event.touches[0].clientX : event.clientX;
slideWidth = currentSlideEl.offsetWidth;
wasSwiping = false;
currentSlideEl.style.transitionDuration = '0ms';
$currentSlide.on('mousemove touchmove', handleSlideMove);
}
const addSlideEventListeners = () => {
// mouse & touch event listener
$currentSlide.on('mousedown touchstart', handleMouseDownOrTouchStart);
$currentSlide.on('mouseup touchend touchcancel', handleMouseUpOrTouchEnd);
// keyboard event listener
$(document).on('keydown.lightbox', e => {
if (e.key == 'ArrowLeft') {
showPrevSlide();
updateLightbox('prev');
} else if (e.key == 'ArrowRight') {
showNextSlide();
updateLightbox('next');
} else if (e.key == 'Escape') closeLightbox();
});
// click on left arrow
$lightbox.find('.lightbox-arrow.arrow-left').on('click', e => {
showPrevSlide();
updateLightbox('prev');
});
// click on right arrow
$lightbox.find('.lightbox-arrow.arrow-right').on('click', e => {
showNextSlide();
updateLightbox('next');
});
}
removeSlideEventListeners = () => {
// mouse & touch event listener
$(currentSlideEl).off('mousedown touchstart');
$(currentSlideEl).off('mouseup touchend touchcancel');
// keyboard event listener
$(document).off('keydown.lightbox');
// arrow buttons event listener
$lightbox.find('.lightbox-arrow').off('click');
}
const transformSlide = (element, translateX, opacity) => {
element.style.transform = `translateX(${translateX})`;
element.style.opacity = opacity;
element.style.transitionDuration = `${transitionDuration}ms`;
$(element).off('mousemove touchmove');
distance = 0;
}
const showNextSlide = () => {
transformSlide(prevSlideEl, '100%', 0);
transformSlide(currentSlideEl, '-100%', 0);
transformSlide(nextSlideEl, '0px', 1);
}
const showPrevSlide = () => {
transformSlide(prevSlideEl, '0px', 1);
transformSlide(currentSlideEl, '100%', 0);
transformSlide(nextSlideEl, '-100%', 0);
}
const resetSlide = () => {
transformSlide(prevSlideEl, '-100%', 0);
transformSlide(currentSlideEl, '0px', 1);
transformSlide(nextSlideEl, '100%', 0);
}
const updateLightbox = (newSlide) => {
if (newSlide != 'current') removeSlideEventListeners();
setTimeout(() => {
// reset transition duration
[currentSlideEl, nextSlideEl, prevSlideEl].forEach(element => {
element.style.transitionDuration = '0ms';
});
let index;
if (newSlide == 'next') {
prevSlideEl.dataset.state = 'next';
nextSlideEl.dataset.state = 'current';
currentSlideEl.dataset.state = 'prev';
index = getLoopedIndex(currentIndex + 1);
loadImage($(prevSlideEl), index + 1);
} else if (newSlide == 'prev') {
prevSlideEl.dataset.state = 'current';
currentSlideEl.dataset.state = 'next';
nextSlideEl.dataset.state = 'prev';
index = getLoopedIndex(currentIndex - 1);
loadImage($(nextSlideEl), index - 1);
} else {
return;
}
updateSlideVariables();
addSlideEventListeners();
updateLightboxHeader(index);
currentIndex = index;
}, transitionDuration);
}
const handleMouseUpOrTouchEnd = event => {
const slideChangeThreshold = 150;
if (distance < -slideChangeThreshold) {
showNextSlide();
updateLightbox('next');
} else if (distance > slideChangeThreshold) {
showPrevSlide();
updateLightbox('prev');
} else {
resetSlide();
updateLightbox('current');
}
}
addSlideEventListeners();
}
function updateLightboxHeader(index) {
index = getLoopedIndex(index);
const title = images[index].title;
$lightbox.find('.lightbox-title').text(title);
$lightbox.find('.lightbox-numbers').text(index + 1 + '/' + images.length);
}
function loadImage($targetSlide, index) {
index = getLoopedIndex(index);
const $currentImage = $targetSlide.find('.lightbox-image');
const src = isSrcsetSupported ? images[index].src : images[index].srcFallback;
const srcset = images[index].srcset;
const tempImage = new Image();
if (isSrcsetSupported && srcset) {
tempImage.srcset = srcset;
} else {
tempImage.src = src;
}
$(tempImage).on('load.loadImage', e => {
if (isSrcsetSupported && srcset) {
$currentImage.attr('srcset', srcset);
} else {
$currentImage.attr('src', src);
}
$targetSlide.find('.spinner').hide();
$currentImage.show();
$currentImage.off('load.loadImage');
});
}
function getLoopedIndex(index) {
if (index > images.length - 1) return 0;
if (index < 0) return images.length - 1;
return index;
}
// Re-maps a number from one range to another.
function mapRange(value, fromIn, toIn, fromOut, toOut) {
return fromOut + (toOut - fromOut) * (value - fromIn) / (toIn - fromIn);
}