-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathgallery.js
113 lines (90 loc) · 2.69 KB
/
gallery.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
// Vanilla JS basic gallery - use as you please
// © 2020 Sander De la Marche
const screens = [];
const gallery = document.querySelector('.gallery');
const loader = document.querySelector('.loading');
const focus = document.querySelector('.focus');
const shots = document.querySelectorAll('.shot');
for (let shot of shots) {
let thumbnail = shot.querySelector('img');
let source = shot.querySelector('a').href;
let screenshot = {
'url': source,
'filename': source.split('/').pop(),
'description': thumbnail.alt
}
screens.push(screenshot)
// Add the click event that will open the gallery
shot.addEventListener('click', openGallery)
}
// Close the gallery when we click anywhere that isn't forward/back
gallery.addEventListener('click', function() {
closeGallery();
})
// Back
const leftButton = document.querySelector('.left');
leftButton.addEventListener('click', function(event) {
loadPrevious();
event.stopPropagation();
});
// Forward
const rightButton = document.querySelector('.right');
rightButton.addEventListener('click', function(event) {
loadNext();
event.stopPropagation();
});
// Key events
document.addEventListener('keydown', function(event) {
switch (event.keyCode) {
// left arrow
case 37:
loadPrevious();
break;
// right arrow
case 39:
loadNext();
break;
// escape
case 27:
closeGallery();
break;
}
});
function openGallery(event) {
// Show the gallery
gallery.style.display = 'flex';
// Toggle the loader icon
loader.style.display = 'block'
// Get the URL of the clicked figure
let clicked = event.target.parentNode.href;
// Check the URL to our array of screenshots and load the match
for (let i = 0; i < screens.length; i++) {
if (screens[i].url === clicked) {
loadImage(i);
}
}
// Don't follow the link
event.preventDefault()
}
function loadImage(index) {
let highlight = screens[index];
if (highlight === undefined) {
return false;
}
focus.style.backgroundImage = "url('" + highlight.url + "')";
focus.dataset.current = index;
gallery.querySelector('.description').innerHTML = highlight.description;
gallery.querySelector('.filename').innerHTML = highlight.filename;
loader.style.display = 'none';
}
function loadPrevious() {
const currentIndex = parseInt(focus.dataset.current);
loadImage(currentIndex - 1);
}
function loadNext() {
const currentIndex = parseInt(focus.dataset.current);
loadImage(currentIndex + 1);
}
function closeGallery() {
gallery.style.display = 'none';
}