-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-gallery.js
50 lines (39 loc) · 1.11 KB
/
01-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
import { galleryItems } from './gallery-items.js';
// Change code below this line
const galleryContainer = document.querySelector('.gallery');
const galleryMarkup = createGalleryMarkup(galleryItems);
let instance;
galleryContainer.insertAdjacentHTML('beforeend', galleryMarkup);
function createGalleryMarkup(gallery){
const markup = gallery.map(({preview, original, description}) => {
return `<div class="gallery__item">
<a class="gallery__link" href="${original}">
<img
class="gallery__image"
src="${preview}"
data-source="${original}"
alt="${description}"
/>
</a>
</div>`
}).join('')
return markup;
}
galleryContainer.addEventListener('click', onOpenModal);
function onOpenModal(evt){
if(evt.target.classList.contains('gallery')){
return
};
evt.preventDefault();
instance = basicLightbox.create(`
<img src="${evt.target.dataset.source}" height="600">
`)
instance.show();
window.addEventListener('keydown', onEscKeyPress);
}
function onEscKeyPress(evt){
if(evt.code === 'Escape'){
window.removeEventListener('keydown', onEscKeyPress);
instance.close();
}
}