-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
292 lines (239 loc) · 7.71 KB
/
script.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
'use strict';
window.onbeforeunload = () => {
window.scrollTo(0, 0);
};
const header = document.querySelector('.header');
const nav = document.querySelector('.nav');
const navItems = document.querySelectorAll('.nav__item');
const navLinks = document.querySelector('.nav__links');
const navLinksMobile = document.querySelector('.nav__links-mobile');
const navHeight = header.getBoundingClientRect().height;
const allSections = document.querySelectorAll('.section');
const images = document.querySelectorAll('img[data-src]');
const btnHeader = document.querySelector('.btn--header');
const section1 = document.querySelector('#section--1');
const navLogo = document.querySelector('.nav__logo');
const btnScrollUp = document.querySelector('.btn--scroll');
const modalForm = document.querySelector('.modal__form');
const modalContact = document.querySelector('.modal__contact');
const btnCloseModal = document.querySelectorAll('.btn--close-modal');
const btnOpenModalForm = document.querySelectorAll('.btn--show-modal');
const btnOpenModalContact = document.querySelector('.btn--show-modal-contact');
const overlays = document.querySelectorAll('.overlay');
const hidden = document.querySelector('.hidden');
const tabs = document.querySelectorAll('.values__tab');
const tabsContent = document.querySelectorAll('.values__content');
const tabsContainer = document.querySelector('.values__tab-container');
const slides = document.querySelectorAll('.slide');
const btnRight = document.querySelector('.slider__btn--right');
const btnLeft = document.querySelector('.slider__btn--left');
const footerLinks = document.querySelector('.footer__nav');
const openNav = document.querySelector('.nav__mobile--open');
const closeNav = document.querySelector('.nav__mobile--close');
const navMobile = document.querySelector('.nav__mobile');
// Page Navigation / Scrolling (Button) / Sticky Navigation
// Navigation - mobile
const activeNav = () => {
navMobile.classList.toggle('nav__mobile--active');
};
openNav.addEventListener('click', () => {
activeNav();
closeNav.style.display = 'block';
openNav.style.display = 'none';
btnScrollUp.style.display = 'none';
});
closeNav.addEventListener('click', () => {
activeNav();
openNav.style.display = 'block';
closeNav.style.display = 'none';
});
//Event delegation
navLinks.addEventListener('click', e => {
e.preventDefault();
if (e.target.classList.contains('nav__link')) {
const linkId = e.target.getAttribute('href');
document.querySelector(linkId).scrollIntoView({
behavior: 'smooth',
});
}
});
navLinksMobile.addEventListener('click', e => {
e.preventDefault();
if (e.target.classList.contains('nav__link')) {
const linkId = e.target.getAttribute('href');
document.querySelector(linkId).scrollIntoView({
behavior: 'smooth',
});
activeNav();
openNav.style.display = 'block';
closeNav.style.display = 'none';
}
});
footerLinks.addEventListener('click', e => {
e.preventDefault();
if (e.target.classList.contains('footer__link')) {
const linkId = e.target.getAttribute('href');
if (linkId === '') return;
document.querySelector(linkId).scrollIntoView({
behavior: 'smooth',
});
}
});
// Sticky navigation
const stickyNavigation = entries => {
const [entry] = entries; // same as entries[0]
if (!entry.isIntersecting) nav.classList.add('sticky');
else nav.classList.remove('sticky');
};
const headerObserver = new IntersectionObserver(stickyNavigation, {
root: null,
threshold: 0,
rootMargin: '-1px',
});
headerObserver.observe(header);
// Button scroll
btnHeader.addEventListener('click', e => {
section1.scrollIntoView({ behavior: 'smooth' });
});
// Logo scroll
navLogo.addEventListener('click', e => {
header.scrollIntoView({ behavior: 'smooth' });
});
// Mobile view page navigatioin
window.addEventListener('scroll', () => {
if (navMobile.classList.contains('nav__mobile--active')) {
btnScrollUp.style.display = 'none';
} else {
if (document.documentElement.scrollTop > 200) {
btnScrollUp.style.display = 'block';
} else {
btnScrollUp.style.display = 'none';
}
}
});
// Menu - link accent
navLinks.addEventListener('click', e => {
const current = document.querySelector('.current');
current.classList.remove('current');
e.target.parentNode.classList.add('current');
});
// Button - scroll up
btnScrollUp.addEventListener('click', e => {
header.scrollIntoView({ behavior: 'smooth' });
const current = document.querySelector('.current');
current.classList.remove('current');
e.target.parentNode.classList.add('current');
});
// Reveal Sections
const revalSections = (entries, observer) => {
const [entry] = entries;
const section = entry.target;
if (entry.isIntersecting) {
section.classList.remove('section--hidden');
observer.unobserve(section);
}
};
const sectionObserver = new IntersectionObserver(revalSections, {
root: null,
threshold: 0.1,
});
allSections.forEach(section => {
sectionObserver.observe(section);
section.classList.add('section--hidden');
});
// Lazy loading images
const lazyLoading = (entries, observer) => {
const [entry] = entries;
const img = entry.target;
if (entry.isIntersecting) {
img.src = img.dataset.src;
img.classList.remove('lazy-img');
observer.unobserve(img);
}
};
const imgObserver = new IntersectionObserver(lazyLoading, {
root: null,
threshold: 0,
rootMargin: '200px',
});
images.forEach(img => imgObserver.observe(img));
// Cookies
const cookies = document.createElement('div');
cookies.classList.add('cookie-message');
cookies.innerHTML =
'This website collects cookies to deliver better user experience. <button class="btn btn--close-cookie">OK!</button>';
header.append(cookies);
document.querySelector('.btn--close-cookie').addEventListener('click', () => {
cookies.remove();
});
// Modal Window
const openModal = target => {
target.classList.remove('hidden');
overlays.forEach(o => o.classList.remove('hidden'));
closeNav.style.display = 'none';
openNav.style.display = 'none';
btnScrollUp.style.display = 'none';
};
const closeModal = target => {
target.classList.add('hidden');
openNav.style.display = 'block';
btnScrollUp.style.display = 'block';
overlays.forEach(o => o.classList.add('hidden'));
};
btnOpenModalForm.forEach(btn =>
btn.addEventListener('click', () => {
openModal(modalForm);
})
);
btnOpenModalContact.addEventListener('click', () => {
openModal(modalContact);
});
btnCloseModal.forEach(btn =>
btn.addEventListener('click', () => {
closeModal(modalForm);
closeModal(modalContact);
})
);
overlays.forEach(o =>
o.addEventListener('click', () => {
closeModal(modalForm);
closeModal(modalContact);
})
);
// Tabbed component
tabsContainer.addEventListener('click', e => {
const clickedTab = e.target.closest('.values__tab');
if (clickedTab) {
tabs.forEach(tab => tab.classList.remove('values__tab--active'));
tabsContent.forEach(tabContent =>
tabContent.classList.remove('values__content--active')
);
clickedTab.classList.add('values__tab--active');
document
.querySelector(`.values__content--${clickedTab.dataset.tab}`)
.classList.add('values__content--active');
}
});
// Slider
let currentSlide = 0;
const maxSlide = slides.length - 1;
const switchSlide = slide => {
slides.forEach(
(s, i) => (s.style.transform = `translateX(${100 * (i - slide)}%)`)
);
};
switchSlide(0);
const next = () => {
if (currentSlide === maxSlide) {
currentSlide = 0;
} else currentSlide++;
switchSlide(currentSlide);
};
const prev = () => {
if (currentSlide === 0) {
currentSlide = maxSlide;
} else currentSlide--;
switchSlide(currentSlide);
};
btnRight.addEventListener('click', next);
btnLeft.addEventListener('click', prev);