Skip to content

Commit 179ac14

Browse files
committed
reduce code
1 parent 99bd89c commit 179ac14

File tree

1 file changed

+7
-31
lines changed

1 file changed

+7
-31
lines changed

script/script.js

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,13 @@ navSlide();
141141
// button move to top
142142
const btnTop = document.querySelector("#movetop");
143143

144+
const navLinksAll = document.querySelectorAll(".nav-links li a");
144145
const homeNav = document.getElementById("HOME-NAV");
145146
const aboutNav = document.getElementById("ABOUT-NAV");
146147
const factNav = document.getElementById("FACT-NAV");
147148
const repoNav = document.getElementById("REPO-NAV");
148149
const workNav = document.getElementById("WORK-NAV");
149150
const contactNav = document.getElementById("CONTACT-NAV");
150-
151151
btnTop.addEventListener("click", function () {
152152
document.body.scrollTop = 0;
153153
document.documentElement.scrollTop = 0;
@@ -162,51 +162,27 @@ window.onscroll = function () {
162162
// console.log(positionTop);
163163
if (positionTop > 4144 - 236) {
164164
//4144
165-
homeNav.classList.remove("active");
166-
aboutNav.classList.remove("active");
167-
factNav.classList.remove("active");
168-
repoNav.classList.remove("active");
169-
workNav.classList.remove("active");
165+
navLinksAll.forEach((all) => all.classList.remove("active"));
170166
contactNav.classList.add("active");
171167
} else if (positionTop > 3439 - 236) {
172168
//3439
173-
homeNav.classList.remove("active");
174-
aboutNav.classList.remove("active");
175-
factNav.classList.remove("active");
176-
repoNav.classList.remove("active");
169+
navLinksAll.forEach((all) => all.classList.remove("active"));
177170
workNav.classList.add("active");
178-
contactNav.classList.remove("active");
179171
} else if (positionTop > 3039 - 236) {
180172
//3039
181-
homeNav.classList.remove("active");
182-
aboutNav.classList.remove("active");
183-
factNav.classList.remove("active");
173+
navLinksAll.forEach((all) => all.classList.remove("active"));
184174
repoNav.classList.add("active");
185-
workNav.classList.remove("active");
186-
contactNav.classList.remove("active");
187175
} else if (positionTop > 1950 - 236) {
188176
//1950
189-
homeNav.classList.remove("active");
190-
aboutNav.classList.remove("active");
177+
navLinksAll.forEach((all) => all.classList.remove("active"));
191178
factNav.classList.add("active");
192-
repoNav.classList.remove("active");
193-
workNav.classList.remove("active");
194-
contactNav.classList.remove("active");
195179
} else if (positionTop > 477 - 236) {
196180
//477
197-
homeNav.classList.remove("active");
181+
navLinksAll.forEach((all) => all.classList.remove("active"));
198182
aboutNav.classList.add("active");
199-
factNav.classList.remove("active");
200-
repoNav.classList.remove("active");
201-
workNav.classList.remove("active");
202-
contactNav.classList.remove("active");
203183
} else if (positionTop > 0) {
184+
navLinksAll.forEach((all) => all.classList.remove("active"));
204185
homeNav.classList.add("active");
205-
aboutNav.classList.remove("active");
206-
factNav.classList.remove("active");
207-
repoNav.classList.remove("active");
208-
workNav.classList.remove("active");
209-
contactNav.classList.remove("active");
210186
}
211187

212188
if (

0 commit comments

Comments
 (0)