-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgallery.js
90 lines (68 loc) · 1.82 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
import cards from './gallery-items.js';
import refs from './dom.js';
refs.imageContainer.addEventListener('click', (e) => {
e.preventDefault();
const imageClick = e.target.classList.contains('gallery__image');
if (!imageClick) {
return
};
modalOpen(e);
})
refs.buttonCloseModal.addEventListener('click', onButtonCloseModal);
refs.modalOverlay.addEventListener('click', onButtonCloseModal);
window.addEventListener('keydown', (e) => {
if (e.code !== "Escape") {
return;
}
onButtonCloseModal();
});
function getModalImg(e) {
refs.modalImage.src = e.target.dataset.source;
refs.modalImage.alt = e.target.alt;
refs.modalImage.dataset.index = e.target.dataset.index;
}
function onButtonCloseModal(e) {
const modalIsOpen = refs.modalWindow.classList.contains('is-open');
if (!modalIsOpen) {
return;
}
refs.modalWindow.classList.remove('is-open');
getModalImg(e);
}
function modalOpen(e) {
refs.modalWindow.classList.add('is-open');
addImageInModal(e);
}
function addImageInModal(e) {
getModalImg(e);
}
//----ArrowLeft&Right-----
window.addEventListener('keydown', (e) => {
let index;
if (e.code === "ArrowRight") {
onArrowRight();
}
if (e.code === "ArrowLeft") {
onArrowLeft();
}
});
function onArrowLeft(index) {
index = +refs.modalImage.dataset.index;
if (index === 0) {
setNewSrc(cards.length - 1, 0);
return;
}
setNewSrc(index, -1);
}
function onArrowRight(index) {
index = +refs.modalImage.dataset.index;
if (index === cards.length -1) {
setNewSrc(0, 0);
return;
}
setNewSrc(index, 1);
}
function setNewSrc(index, step) {
refs.modalImage.dataset.index = `${index + step}`;
refs.modalImage.src = cards[index + step].original;
}