-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
81 lines (61 loc) · 2.67 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
const hamburguerButtonEL = document.querySelector('[data-js="hamburguer-button"]')
const hamburguerMenuCloserEL = document.querySelector('[data-js="hamburguer-menu-closer"]')
const hamburguerMenuEL = document.querySelector('[data-js="hamburguer-menu"]')
const backdropAreaEL = document.querySelector('[data-js="backdrop-area"]')
const carouselImagesELs = Array.from(document.querySelector('[data-js="carousel-container"]').children)
const contentSectionsELs = Array.from(document.querySelector('.carousel-text').children)
const previousImageEL = document.querySelector('[data-js="previous-image"]')
const nextImageEL = document.querySelector('[data-js="next-image"]')
function toggleShowHamburgerMenu() {
const hamburguerMenuIsExposed = hamburguerMenuEL.classList.contains('show-hamburguer-menu--js')
if (hamburguerMenuIsExposed) {
hamburguerMenuEL.classList.remove('show-hamburguer-menu--js')
backdropAreaEL.classList.remove('show-backdrop-area--js')
}
else {
hamburguerMenuEL.classList.add('show-hamburguer-menu--js')
backdropAreaEL.classList.add('show-backdrop-area--js')
}
}
function getVisibleSectionIndex() {
for (let index=0; index < carouselImagesELs.length; index++) {
const currentImage = carouselImagesELs[index]
const theCurrentImageIsVisible = currentImage.classList.contains('show-image--js')
if (theCurrentImageIsVisible) return index
}
}
function removeVisibilityClasses(sectionIndex) {
carouselImagesELs[sectionIndex].classList.remove('show-image--js')
contentSectionsELs[sectionIndex].classList.remove('show-text--js')
}
function addVisibilityClasses(sectionIndex) {
carouselImagesELs[sectionIndex].classList.add('show-image--js')
contentSectionsELs[sectionIndex].classList.add('show-text--js')
}
function prev() {
const visibleSectionIndex = getVisibleSectionIndex()
const carouselImagesLength = carouselImagesELs.length
removeVisibilityClasses(visibleSectionIndex)
if (visibleSectionIndex) {
addVisibilityClasses(visibleSectionIndex-1)
}
else {
addVisibilityClasses(carouselImagesLength-1)
}
}
function next() {
const visibleSectionIndex = getVisibleSectionIndex()
const carouselImagesLength = carouselImagesELs.length
removeVisibilityClasses(visibleSectionIndex)
if (visibleSectionIndex < carouselImagesLength-1) {
addVisibilityClasses(visibleSectionIndex+1)
}
else {
addVisibilityClasses(0)
}
}
hamburguerMenuCloserEL.addEventListener('click', toggleShowHamburgerMenu)
hamburguerButtonEL.addEventListener('click', toggleShowHamburgerMenu)
backdropAreaEL.addEventListener('click', toggleShowHamburgerMenu)
previousImageEL.addEventListener('click', prev)
nextImageEL.addEventListener('click', next)