-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu-animation.js
176 lines (112 loc) · 4.58 KB
/
menu-animation.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
const menuBtn = document.querySelector('#menuBtn');
const menu = document.querySelector('.menu ul');
const selectAllAnchors = document.querySelectorAll('ul li a[href^="#"]');
// smooth scroll animation when navigating
selectAllAnchors.forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
document.querySelector(this.getAttribute("href")).scrollIntoView({
behavior: "smooth"
});
});
});
// set event to HTML document for 'active' class toggle on anchors.
// set anchor to a number and check if the clicked anchor === number ? toggle : remove.
document.addEventListener("DOMContentLoaded", () => {
const home = document.querySelector('.home');
home.classList.toggle('active');
if (selectAllAnchors) {
selectAllAnchors.forEach((anchor, key) => {
anchor.addEventListener('click', () => {
home.classList.remove('active');
anchor.classList.toggle("active");
selectAllAnchors.forEach((anchors, number) => {
if (key !== number) {
anchors.classList.remove('active');
}
});
});
});
}
});
// hamburger menu.
menuBtn.addEventListener('click', () => {
menuBtn.classList.toggle('active');
menu.classList.toggle('active');
});
// hide menu after clicking on a link or/ clicking outside the menu
window.document.addEventListener('mouseover', (e) => {
if (e.target !== menuBtn && e.target !== menu) {
menuBtn.classList.remove('active');
menu.classList.remove('active');
}
});
// sticky navbar when scrolling.
window.addEventListener('scroll', () => {
let menuSticky = document.querySelector('nav');
menuSticky.classList.toggle('sticky', window.scrollY > 0);
});
const loadingg = document.querySelector('.loading');
const services = document.querySelector('#services');
services.innerHTML += `<article><header><h2>Services not available at this moment. Please revisit 2024 when im done with my education!
</h2></header></article> `;
// menuA.forEach(a => {
// a.addEventListener('click', (e) => {
// e.stopPropagation();
// const active = document.querySelector('active');
// if (active) {
// e.currentTarget.classList.remove('active');
// }
// e.currentTarget.classList.add('active');
// });
// });
// NOT POSSIBLE WITH UNDER PAGES??
// possible to highlight menu a by current location?
// maybe with a loop + checking current location on window?
// const currentLocation = location.pathname;
// const menuHighlight = document.querySelectorAll('.menu a');
// const menuLength = menuHighlight.length;
// console.log(menuLength);
// for (let i = 0; i < menuHighlight.length; i++) {
// //check if menu anchors href match current location href?
// if (menuHighlight[i].pathname === currentLocation) {
// menuHighlight[i].className = 'active';
// }
// }
// add another event listener for scroll
// window.addEventListener('scroll', navHighlight);
// const sections = document.querySelectorAll('section[id]');
// function navHighlight() {
// // current scroll position
// let scrollY = window.pageYOffset;
// //loop thru all sections
// sections.forEach(current => {
// const sectionHeight = current.offsetHeight;
// const sectionTop = current.offsetTop;
// let sectionId = current.getAttribute('id');
// //if current scroll position enter the space where current seciton on screen is
// //add active class else remove it
// if (scrollY > sectionTop && scrollY <= sectionHeight) {
// document.querySelector('.menu li a[href*=' + sectionId + ']').classList.add('active');
// } else {
// document.querySelector('.menu li a[href*=' + sectionId + ']').classList.remove('active');
// }
// });
// }
// let menuItems = document.querySelectorAll('.menu ul li a');
// menuBtn.addEventListener('click', () => {
// menuBtn.classList.toggle('active');
// menu.classList.toggle('active');
// });
// const activePage = window.location.pathname;
// const navLinks = document.querySelectorAll('.menu a').forEach(link => {
// if (link.href.includes(`${activePage}`)) {
// link.classList.add('active');
// }
// })
// document.onclick = (e) => {
// if (e.target.classList !== menuBtn && e.target.classList !== menu) {
// menuBtn.classList.remove('active');
// menu.classList.remove('active');
// }
// }