-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavigation.js
110 lines (74 loc) · 2.78 KB
/
navigation.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
// ------------------------------
// VARIABLES PLACE
// ------------------------------
// 'DOM ELEMENTS'
const
hamburgerButton = document.querySelector('.hamburger-button'),
navigation = document.querySelector('nav'),
navigationListpoints = document.querySelectorAll('nav li')
// 'ANIMATION VARIABLES'
let delayVar = 0 // initial delay value
// ------------------------------
// CALLBACK
// ------------------------------
const animationBringFullWidth = (element, delay) =>{
// keyframe object, timing properties
const
showTheListPoint = [{ width: '100%'}],
showTheListPointTiming = {
duration: 300,
delay: delay,
fill: 'forwards'
}
element.animate(showTheListPoint, showTheListPointTiming)
}
const animationSetWidhtToZero = (element, delay) =>{
// keyframe object, timing properties
const
showTheListPoint = [{ width: '0'}],
showTheListPointTiming = {
duration: 300,
delay: delay,
fill: 'forwards'
}
element.animate(showTheListPoint, showTheListPointTiming)
}
// ------------------------------
// EVENT LISTENER
// ------------------------------
let wantSeeNavigation = false // state if the navigation should be visible
// => handle the hamburger click
hamburgerButton.addEventListener('click', ()=>{
wantSeeNavigation = !wantSeeNavigation
// => change the hamburger button
hamburgerButton.classList.toggle('active')
// => show the nav / hide the nav
// => when the nav should be closed, wait till the last list item shrinks
let timeout = wantSeeNavigation ? 0 : delayVar
setTimeout( ()=>{
navigation.classList.toggle('nav-active')
}, `${timeout}` )
// => if the user want see the navigation ...
if(wantSeeNavigation){
// => show the list points
navigationListpoints.forEach(listPoint =>{
// console.log(delayVar)
delayVar += 200
animationBringFullWidth(listPoint, delayVar)
})
}
// => if the user close the navigation ...
else{
// => shrink the list points
// => the last list point should be the first that shrinks
const reversedNavigationListPoint = Array.from(navigationListpoints).reverse()
reversedNavigationListPoint.forEach( (listPoint, idx) =>{
// => reset the delay variable with the first list item for the shrink animations
idx === 0 && (delayVar = 0)
delayVar += 200
animationSetWidhtToZero(listPoint, delayVar)
// => reset the delay variable with the last list item for the grow animations
idx === reversedNavigationListPoint.length -1 && (delayVar = 0)
})
}
})