-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgallery.js
35 lines (31 loc) · 1.05 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
function showGallery(img) {
document.body.classList.add('gallery-open');
document.querySelector('#gallery .caption').innerHTML = img.getAttribute(
'data-gallery-caption'
);
document
.querySelector('#gallery img')
.setAttribute('srcset', img.getAttribute('data-gallery-source'));
document
.getElementById('gallery')
.setAttribute('style', 'display: initial');
}
function hideGallery() {
document.body.classList.remove('gallery-open');
document.querySelector('#gallery img').setAttribute('srcset', '');
document.getElementById('gallery').setAttribute('style', '');
}
// Open gallery when clicking image
const images = document.querySelectorAll('img[data-gallery-source]');
images.forEach(node =>
node.addEventListener('click', () => showGallery(node))
);
// Close gallery when clicking background
const gallery = document.getElementById('gallery');
gallery.addEventListener('click', () => hideGallery());
// Close gallery when pressing 'esc'
document.onkeydown = event => {
if (event.key === 'Escape') {
hideGallery();
}
};