-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
384 lines (313 loc) · 11.3 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
'use strict';
///////////////////////////////////////
// Global selectors
const nav = document.querySelector('.nav');
const header = document.querySelector('.header');
const navHeight = nav.getBoundingClientRect().height;
const hamburger = document.querySelector('[data-nav-hamburguer]');
const navLinksWrapper = document.querySelector('[data-nav-links]');
const footer = document.querySelector('.footer');
const windowWidth = window.innerWidth;
const mobileBreakpoint = 767;
const tabletBreakpoint = 768;
///////////////////////////////////////
// Add copy year dynamically
const copyYear = document.querySelector('[data-year]');
copyYear.textContent = new Date().getFullYear();
///////////////////////////////////////
// Cookie Message
const cookieMessage = document.createElement('div');
cookieMessage.classList.add('cookie-message');
cookieMessage.innerHTML =
'We use cookies for improved functionality and analytics. <button class="btn" data-close-cookie>Got it!</button>';
footer.after(cookieMessage);
const closeCookie = document.querySelector('[data-close-cookie]');
closeCookie.addEventListener('click', function () {
cookieMessage.remove();
});
///////////////////////////////////////
// Mobile Menu
hamburger.addEventListener('click', mobileMenu);
function mobileMenu() {
hamburger.classList.toggle('active');
navLinksWrapper.classList.toggle('active');
}
///////////////////////////////////////
// Scrolling to first section
const btnScrollTo = document.querySelector('[data-scroll-to]');
const section1 = document.querySelector('#section--1');
btnScrollTo.addEventListener('click', function () {
section1.scrollIntoView({ behavior: 'smooth' });
});
///////////////////////////////////////
// Navigation
// Scrolling with event delegation
navLinksWrapper.addEventListener('click', function (e) {
e.preventDefault();
if (e.target.hasAttribute('data-nav-link')) {
const id = e.target.getAttribute('href');
document.querySelector(id).scrollIntoView({ behavior: 'smooth' });
}
});
// Menu fade animation
const handleHover = function (e) {
if (e.target.classList.contains('nav__link')) {
const link = e.target;
const siblings = link.closest('.nav').querySelectorAll('.nav__link');
const logo = link.closest('.nav').querySelector('img');
siblings.forEach(el => {
if (el !== link) el.style.opacity = this;
});
logo.style.opacity = this;
}
};
//Setting "this" manually through "bind" to pass the opacity as an "argument" into the handler function
nav.addEventListener('mouseover', handleHover.bind(0.5));
nav.addEventListener('mouseout', handleHover.bind(1));
//Sticky Navigation with Intersection Observer API
/* The header element is being observed by the API since it becomes its "target" and as soon as it is no longer visible
on the viewport (root: 0, threshold: 0), which means not being intersectioned anymore, the sticky class is added to it */
const stickyNavCallback = function (entries) {
/* Entries represents the threshold options.
Since there is only one option this time, the entry is just destructured from entries
*/
const [entry] = entries;
if (!entry.isIntersecting) {
nav.classList.add('sticky');
} else {
nav.classList.remove('sticky');
}
};
const stickyOptions = {
root: null, //by using null, the viewport is defined as the root
threshold: 0, //by using 0, the threshold is defined as soon as the header element is no longer visible
rootMargin: windowWidth > tabletBreakpoint ? `-${navHeight}px` : '300px', //that makes the sticky navigation appears before the section starts to prevent content overlapping
};
const headerObserver = new IntersectionObserver(
stickyNavCallback,
stickyOptions
);
headerObserver.observe(header);
///////////////////////////////////////
// Reveal sections with Intersection Observer API
const allSections = document.querySelectorAll('section');
const revealSection = function (entries, observer) {
const [entry] = entries; //get the threshold option
if (!entry.isIntersecting) return;
entry.target.classList.remove('section--hidden');
observer.unobserve(entry.target);
};
const sectionObserver = new IntersectionObserver(revealSection, {
root: null, //consider viewport as the root
threshold: 0.15, // the section will be revealed when it is 15% visible
});
allSections.forEach(function (section) {
sectionObserver.observe(section);
// Consider the page top position to hide the section in case the user
// refresh the page while in a botton one.
var topPosition = document.body.getBoundingClientRect().top;
if (topPosition > -100) {
section.classList.add('section--hidden');
}
});
///////////////////////////////////////
// Lazy loading images with Intersection Observer API
const imgTargets = document.querySelectorAll('img[data-src]');
const loadImg = function (entries, observer) {
const [entry] = entries;
if (!entry.isIntersecting) return;
//Replace src with data-src
entry.target.src = entry.target.dataset.src;
entry.target.addEventListener('load', function () {
entry.target.classList.remove('lazy-img');
});
observer.unobserve(entry.target);
};
const imgObserver = new IntersectionObserver(loadImg, {
root: null,
threshold: 0,
rootMargin: '-100px', //Make images load before the thresold is reached
});
imgTargets.forEach(img => {
imgObserver.observe(img);
});
///////////////////////////////////////
// Tabbed component
const tabTrigger = document.querySelectorAll('[data-tab]');
const tabContainer = document.querySelector('[data-tab-container]');
const tabContent = document.querySelectorAll('[data-tab-content]');
tabContainer.addEventListener('click', function (e) {
const clickedBtn = e.target.closest('[data-tab]');
//Guard clause
if (!clickedBtn) return;
//Activate tab
tabTrigger.forEach(tab => tab.classList.remove('operations__tab--active'));
clickedBtn.classList.add('operations__tab--active');
//Activate content area
tabContent.forEach(tab =>
tab.classList.remove('operations__content--active')
);
document
.querySelector(`.operations__content--${clickedBtn.dataset.tab}`)
.classList.add('operations__content--active');
});
///////////////////////////////////////
// Slider
const slider = function () {
const sliderContainer = document.querySelector('.slider');
const slides = document.querySelectorAll('.slide');
let currentSlide = 0;
const maxSlide = slides.length;
const btnLeft = document.querySelector('[data-slider-left]');
const btnRight = document.querySelector('[data-slider-right]');
const dotContainer = document.querySelector('[data-dots-container]');
const createDots = function () {
slides.forEach((_, i) => {
const btnDot = `<button class="dots__dot" data-slide="${i}"></button>`;
dotContainer.insertAdjacentHTML('beforeend', btnDot);
});
};
const goToSlide = function (slide) {
slides.forEach((s, i) => {
s.style.transform = `translateX(${100 * (i - slide)}%)`;
});
activeDot(slide);
btnRight.classList.remove('disabled');
btnLeft.classList.remove('disabled');
if (slide === 0) {
btnLeft.classList.add('disabled');
}
if (slide === maxSlide - 1) {
btnRight.classList.add('disabled');
}
};
const activeDot = function (slide) {
//Remove all active dot styling first
document
.querySelectorAll('.dots__dot')
.forEach(dot => dot.classList.remove('dots__dot--active'));
document
.querySelector(`.dots__dot[data-slide="${slide}"]`)
.classList.add('dots__dot--active');
};
dotContainer.addEventListener('click', function (e) {
if (e.target.classList.contains('dots__dot')) {
const slide = e.target.dataset.slide;
goToSlide(Number(slide));
}
});
//Next slide
const nextSlide = function () {
currentSlide++;
goToSlide(currentSlide);
};
// Previous slide
const prevSlide = function () {
currentSlide--;
goToSlide(currentSlide);
};
//Initiate always on slide 0
const initSlider = function () {
createDots();
goToSlide(0);
};
initSlider();
//Event handlers
btnRight.addEventListener('click', nextSlide);
btnLeft.addEventListener('click', prevSlide);
document.addEventListener('keydown', function (e) {
e.key === 'ArrowLeft' && prevSlide();
e.key === 'ArrowRight' && nextSlide();
});
const sliderTouch = function () {
let isDragging = false,
startPos = 0,
currentTranslate = 0,
prevTranslate = 0,
currentIndex = 0;
slides.forEach((slide, index) => {
slide.addEventListener('dragstart', e => e.preventDefault());
// touch events
slide.addEventListener('touchstart', touchStart(index));
slide.addEventListener('touchend', touchEnd);
slide.addEventListener('touchmove', touchMove);
// mouse events
slide.addEventListener('mousedown', touchStart(index));
slide.addEventListener('mouseup', touchEnd);
slide.addEventListener('mousemove', touchMove);
slide.addEventListener('mouseleave', touchEnd);
});
// make responsive to viewport changes
window.addEventListener('resize', setPositionByIndex);
// prevent menu popup on long press
window.oncontextmenu = function (event) {
event.preventDefault();
event.stopPropagation();
return false;
};
function getPositionX(event) {
return event.type.includes('mouse')
? event.pageX
: event.touches[0].clientX;
}
function touchStart(index) {
return function (event) {
currentIndex = index;
startPos = getPositionX(event);
isDragging = true;
sliderContainer.classList.add('grabbing');
};
}
function touchMove(event) {
if (isDragging) {
const currentPosition = getPositionX(event);
currentTranslate = prevTranslate + currentPosition - startPos;
}
}
function touchEnd() {
isDragging = false;
const movedBy = currentTranslate - prevTranslate;
// if moved enough negative then snap to next slide if there is one
if (movedBy < -100 && currentIndex < slides.length - 1) {
currentIndex++;
goToSlide(currentIndex);
}
// if moved enough positive then snap to previous slide if there is one
if (movedBy > 100 && currentIndex > 0) {
currentIndex--;
goToSlide(currentIndex);
}
setPositionByIndex();
sliderContainer.classList.remove('grabbing');
}
function setPositionByIndex() {
currentTranslate = currentIndex * -window.innerWidth;
prevTranslate = currentTranslate;
}
};
sliderTouch();
};
slider();
///////////////////////////////////////
// Modal window
const modal = document.querySelector('[data-modal]');
const overlay = document.querySelector('[data-overlay]');
const btnCloseModal = document.querySelector('[data-modal-close]');
const btnsOpenModal = document.querySelectorAll('[data-modal-open]');
const openModal = function (e) {
e.preventDefault();
modal.classList.remove('hidden');
overlay.classList.remove('hidden');
};
const closeModal = function () {
modal.classList.add('hidden');
overlay.classList.add('hidden');
};
btnsOpenModal.forEach(btn => btn.addEventListener('click', openModal));
btnCloseModal.addEventListener('click', closeModal);
overlay.addEventListener('click', closeModal);
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape' && !modal.classList.contains('hidden')) {
closeModal();
}
});