-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgallery-items.js
161 lines (144 loc) · 4.58 KB
/
gallery-items.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
const flowers = [
{
preview:
'https://cdn.pixabay.com/photo/2019/05/14/16/43/himilayan-blue-poppy-4202825__340.jpg',
original:
'https://cdn.pixabay.com/photo/2019/05/14/16/43/himilayan-blue-poppy-4202825_1280.jpg',
description: 'Hokkaido Flower',
},
{
preview:
'https://cdn.pixabay.com/photo/2019/05/14/22/05/container-4203677__340.jpg',
original:
'https://cdn.pixabay.com/photo/2019/05/14/22/05/container-4203677_1280.jpg',
description: 'Container Haulage Freight',
},
{
preview:
'https://cdn.pixabay.com/photo/2019/05/16/09/47/beach-4206785__340.jpg',
original:
'https://cdn.pixabay.com/photo/2019/05/16/09/47/beach-4206785_1280.jpg',
description: 'Aerial Beach View',
},
{
preview:
'https://cdn.pixabay.com/photo/2016/11/18/16/19/flowers-1835619__340.jpg',
original:
'https://cdn.pixabay.com/photo/2016/11/18/16/19/flowers-1835619_1280.jpg',
description: 'Flower Blooms',
},
{
preview:
'https://cdn.pixabay.com/photo/2018/09/13/10/36/mountains-3674334__340.jpg',
original:
'https://cdn.pixabay.com/photo/2018/09/13/10/36/mountains-3674334_1280.jpg',
description: 'Alpine Mountains',
},
{
preview:
'https://cdn.pixabay.com/photo/2019/05/16/23/04/landscape-4208571__340.jpg',
original:
'https://cdn.pixabay.com/photo/2019/05/16/23/04/landscape-4208571_1280.jpg',
description: 'Mountain Lake Sailing',
},
{
preview:
'https://cdn.pixabay.com/photo/2019/05/17/09/27/the-alps-4209272__340.jpg',
original:
'https://cdn.pixabay.com/photo/2019/05/17/09/27/the-alps-4209272_1280.jpg',
description: 'Alpine Spring Meadows',
},
{
preview:
'https://cdn.pixabay.com/photo/2019/05/16/21/10/landscape-4208255__340.jpg',
original:
'https://cdn.pixabay.com/photo/2019/05/16/21/10/landscape-4208255_1280.jpg',
description: 'Nature Landscape',
},
{
preview:
'https://cdn.pixabay.com/photo/2019/05/17/04/35/lighthouse-4208843__340.jpg',
original:
'https://cdn.pixabay.com/photo/2019/05/17/04/35/lighthouse-4208843_1280.jpg',
description: 'Lighthouse Coast Sea',
},
];
const flowersArr = flowers.map(flower => flower.original);
const gallery = document.querySelector('.js-gallery');
const modalOpenEl = document.querySelector('.lightbox');
const modalImgEl = document.querySelector('.lightbox__image');
const galleryMarkup = createPicturesCardsMarkup(flowers);
gallery.insertAdjacentHTML('beforeend', galleryMarkup);
gallery.addEventListener('click', onGalleryClick);
modalOpenEl.addEventListener('click', onModalClose);
function createPicturesCardsMarkup() {
return flowers
.map(({ preview, original, description }) => {
return `<li class="gallery__item">
<a
class="gallery__link"
href="${preview}"
>
<img
class="gallery__image"
src="${preview}"
data-source="${original}"
alt="${description}"
/>
</a>
</li>`;
})
.join('');
}
function onGalleryClick(ev) {
ev.preventDefault();
const isGalleryPicEl = ev.target.classList.contains('gallery__image');
if (!isGalleryPicEl) {
return;
}
const currentPicUrl = ev.target.dataset.source;
const currentPicAlt = ev.target.alt;
modalOpenEl.classList.add('is-open');
modalImgEl.src = currentPicUrl;
modalImgEl.alt = currentPicAlt;
window.addEventListener('keydown', onModalClose);
}
function onModalClose(ev) {
if (
!(onModalCloseOverlay(ev) || onModalCloseBtn(ev) || onModalCloseEsc(ev))
) {
return;
}
modalOpenEl.classList.remove('is-open');
modalImgEl.src = '#';
modalImgEl.alt = '';
window.removeEventListener('keydown', onModalClose);
}
function onModalCloseOverlay(ev) {
return ev.target.classList.contains('lightbox__overlay');
}
function onModalCloseBtn(ev) {
return ev.target.classList.contains('lightbox__button');
}
function onModalCloseEsc(ev) {
onChangePic(ev);
return ev.key === 'Escape';
}
function onChangePic(ev) {
const currentPic = flowersArr.indexOf(`${modalImgEl.src}`);
const tmp = flowersArr.length-1;
if (ev.key === 'ArrowRight') {
if ((currentPic === flowersArr.length-1)) {
modalImgEl.src = flowersArr[0];
} else {
modalImgEl.src = flowersArr[currentPic + 1];
}
}
if (ev.key === 'ArrowLeft') {
if ((currentPic === 0)) {
modalImgEl.src = flowersArr[tmp];
} else {
modalImgEl.src = flowersArr[currentPic - 1];
}
}
}