-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgallery.js
124 lines (96 loc) · 3.23 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
114
115
116
117
118
119
120
121
122
123
124
const nextBtn = document.querySelector('.modal-next-icon')
const prevBtn = document.querySelector('.modal-prev-icon')
const crossIcon = document.querySelector('.modal-cross-icon');
const modal = document.querySelector('.modal');
const galleryCardContainers = document.querySelectorAll('.gallery-card-containers')
const galleryImages = document.querySelectorAll('.gallery-images');
const imagesContainers = document.querySelectorAll('.img-container');
const infoBtn = document.getElementById('info-btn-container');
const infoBox = document.getElementById('info-box');
infoBox.classList.add('close-info-box');
infoBtn.addEventListener('click',() =>{
if(infoBox.classList.contains('close-info-box')){
infoBox.classList.remove('close-info-box');
infoBox.classList.add('open-info-box');
}
else if(infoBox.classList.contains('open-info-box')){
infoBox.classList.remove('open-info-box');
infoBox.classList.add('close-info-box');
}
})
crossIcon.addEventListener('click',() =>{
modal.classList.remove('open-modal')
modal.classList.add('close-modal')
})
imagesContainers.forEach((container,index) =>{
let slide = document.createElement('div');
slide.className = 'slides';
let modalImageContainer = document.createElement('div');
modalImageContainer.className = 'modal-img-container';
let modalTitleContainer = document.createElement('div');
modalTitleContainer.className = 'modal-title-container';
let modalImage = document.createElement('img');
modalImage.className = 'modal-img';
modalImage.src = container.firstChild.src;
modalImage.alt = `image_${index}`;
let modalTitle = document.createElement('h2');
modalTitle.className = 'modal-title';
modalTitle.innerHTML = container.getAttribute('value');
slide.appendChild(modalImageContainer);
slide.appendChild(modalTitleContainer);
modalImageContainer.appendChild(modalImage);
modalTitleContainer.appendChild(modalTitle);
modal.appendChild(slide);
})
const slides = document.querySelectorAll('.slides')
let startSlide = '';
galleryImages.forEach((image,i) =>{
image.addEventListener('click',() =>{
startSlide = i;
getCurrentSlide(i);
})
})
nextBtn.addEventListener('click',() =>{
plusSlide(1);
});
prevBtn.addEventListener('click',() =>{
plusSlide(-1)
});
document.addEventListener('keydown',(e) =>{
if(e.keyCode === 39 || e.keyCode === 38){
plusSlide(1);
}
else if(e.keyCode === 37 || e.keyCode === 40){
plusSlide(-1)
}
})
function getCurrentSlide(i){
slides.forEach((slide,j) =>{
if(i != j){
slide.classList.remove('slide');
slide.classList.add('no-slide');
}
})
slides[i].classList.remove('no-slide');
slides[i].classList.add('slide');
modal.classList.remove('close-modal');
modal.classList.add('.open-modal');
}
function plusSlide(i){
startSlide = startSlide+i;
slides.forEach((slide,j) =>{
if(startSlide != j){
slide.classList.remove('slide');
slide.classList.add('no-slide');
}
})
if(startSlide === slides.length){
startSlide = 0;
}
else if(startSlide === -1){
startSlide = slides.length-1;
}
slides[startSlide].classList.remove('no-slide');
slides[startSlide].classList.add('slide');
modal.classList.add('.open-modal');
}