-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
212 lines (160 loc) · 5.96 KB
/
index.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
let listItems = document.querySelectorAll('li');
let listItemsEmbedded = document.querySelectorAll('.embedded li');
const listImagePath = '<img class="list-img" src="pictures/icons/petit.png"/> '
function addListImage() {
// remove if after refactor 'embedded' will not be used
for(let j = 0; j < listItemsEmbedded.length; j++) {
listItemsEmbedded[j].innerHTML = listImagePath + listItemsEmbedded[j].innerHTML;
}
for(let i = 0; i < listItems.length; i++) {
listItems[i].innerHTML = listImagePath + listItems[i].innerHTML;
}
}
addListImage();
let navbar = document.querySelector('#navbar');
let navHeight = getNumberOfPx(navbar, 'height');
let navPadding = getNumberOfPx(navbar, 'padding-bottom');
const viewportWidth = Number(window.visualViewport.width);
function getNumberOfPx(element, property) {
let dimension = window.getComputedStyle(element).getPropertyValue(property).slice(0,-2);
return Number(dimension);
}
function setHeaderMarginTop() {
if(Number(viewportWidth) > 550) {
let paddingHeight = navHeight + navPadding + 'px';
document.querySelector('#header').style.paddingTop = paddingHeight;
document.querySelector('h1').style.marginTop = paddingHeight;
}
}
if (document.querySelector('#header') != null) {
setHeaderMarginTop();
}
// ON/OFF navbar menu on mobile
const navIcon = document.querySelector('.nav-icon');
const navbarMobile = document.querySelector('#navbar');
const navLinkMobile = document.querySelectorAll('#navbar a');
const navArrow = document.querySelector('.nav-arrow');
// open menu
navIcon.addEventListener('click', () => {
navIcon.style.display = 'none';
navbarMobile.classList.add('navbar-mobile');
navArrow.classList.add('nav-arrow-mobile');
for(let i = 0; i < navLinkMobile.length; i++) {
navLinkMobile[i].classList.add('nav-mobile');
}
});
// close menu by arrow
navArrow.addEventListener('click', () => {
hideNavMenu();
});
// close menu after click on a link
// if statement for prevent display nav-icon on big screen
if(Number(viewportWidth) < 550) {
for(let i = 0; i < navLinkMobile.length; i++) {
navLinkMobile[i].addEventListener('click', () => {
hideNavMenu();
});
}
}
function hideNavMenu() {
for(let i = 0; i < navLinkMobile.length; i++) {
navLinkMobile[i].classList.remove('nav-mobile');
}
navArrow.classList.remove('nav-arrow-mobile');
navbarMobile.classList.remove('navbar-mobile');
navIcon.style.display = 'inline-block';
}
/* When the user scrolls down, hide the navbar. When the user scrolls up, show the navbar */
var prevScrollPos = window.pageYOffset;
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
if (prevScrollPos > currentScrollPos) {
document.getElementById("navbar").style.top = "0";
} else {
document.getElementById("navbar").style.top = "-" + navHeight + "px";
}
prevScrollPos = currentScrollPos;
}
// add images placeholder
const galleryPhotos = document.querySelectorAll('.gallery-photo');
let photosContainer = document.querySelector('#gallery div.c-flex');
let photosModuloBy2 = galleryPhotos.length % 2;
let photosModuloBy4 = galleryPhotos.length % 4;
if ((viewportWidth > 800) && (photosModuloBy4 != 0)) {
for(let i = 0; i < (4 - photosModuloBy4); i++) {
photosContainer.innerHTML += '<img class="gallery-photo" src="" alt="" style= "visibility: hidden;">';
}
};
if ((viewportWidth < 800) && (viewportWidth > 550) && photosModuloBy2 != 0) {
photosContainer.innerHTML += '<img class="gallery-photo" src="" alt="" style= "visibility: hidden;">';
};
// zoom photo after click
let popUpContainer = document.querySelector('.pop-up');
let zoomImage = document.querySelector('.zoom');
document.getElementById('gallery-box').addEventListener('click', (e) => {
if (e.target && e.target.matches('.gallery-photo')) {
popUpContainer.style.display = 'inline-block';
zoomImage.src = e.target.src.replace('file:///C:/js/refreshspace/', '');
zoomImage.alt = e.target.alt;
setScrollArrowsStyleTop();
scrollPhoto(e.target, zoomImage);
document.querySelector('.exit').addEventListener('click', () => {
popUpContainer.style.display = 'none';
zoomImage.innerHTML.src = '';
zoomImage.innerHTML.alt = '';
});
}
})
// set scroll arrows top position
let photosList = document.querySelectorAll('.gallery-photo');
let scrollArrows = document.querySelectorAll('.scroll');
function setScrollArrowsStyleTop() {
let popUpHeight = getNumberOfPx(popUpContainer, 'height');
scrollArrows.forEach(arrow => {
arrow.style.top = Math.floor(popUpHeight * 0.4) + 'px';
});
}
function scrollPhoto(photo, zoomImg) {
let photoIndex = findPhotoIndex(photosList, photo)
scrollArrows.forEach(e => {
e.addEventListener('click', (arrow) => {
photoIndex = incrementIndex(arrow, photoIndex)
zoomImage.src = photosList[photoIndex].src;
zoomImage.alt = photosList[photoIndex].alt;
});
});
};
function findPhotoIndex(list, photo) {
return [].indexOf.call(list, photo);
};
function incrementIndex(arrow, photoIndex) {
if (arrow.target.classList[1] == 'left') {
photoIndex = scrollToLeft(photoIndex);
} else {
photoIndex = scrollToRight(photoIndex);
};
return photoIndex;
};
function scrollToLeft(photoIndex) {
photoIndex -= 1
if (photoIndex > photosList.length - 1) {
photoIndex = 0;
};
if (photoIndex < 0) {
photoIndex = photosList.length - 1;
};
while (photosList[photoIndex].style.visibility == 'hidden') {
photoIndex -= 1;
};
return photoIndex;
};
function scrollToRight(photoIndex) {
photoIndex += 1
if (photoIndex > photosList.length - 1) {
photoIndex = 0;
};
if (photosList[photoIndex].style.visibility == 'hidden') {
photoIndex = 0;
};
return photoIndex;
};