From e8e73949a3be489860309eac61c01ec0d92f4720 Mon Sep 17 00:00:00 2001 From: JGHartel <155636863+JGHartel@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:47:06 +0200 Subject: [PATCH] changed js --- assets/js/script.js | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/assets/js/script.js b/assets/js/script.js index 6439a82..206a439 100644 --- a/assets/js/script.js +++ b/assets/js/script.js @@ -1,7 +1,5 @@ 'use strict'; - - // element toggle function const elementToggleFunc = function (elem) { elem.classList.toggle("active"); } @@ -135,25 +133,31 @@ for (let i = 0; i < formInputs.length; i++) { } - -// page navigation variables +// Page navigation variables const navigationLinks = document.querySelectorAll("[data-nav-link]"); const pages = document.querySelectorAll("[data-page]"); -// add event to all nav link -for (let i = 0; i < navigationLinks.length; i++) { - navigationLinks[i].addEventListener("click", function () { +// Add event to all nav links +navigationLinks.forEach((link) => { + link.addEventListener("click", function () { + const targetPage = this.textContent.trim().toLowerCase(); + + // Remove 'active' class from all navigation links + navigationLinks.forEach((navLink) => { + navLink.classList.remove("active"); + }); - for (let i = 0; i < pages.length; i++) { - if (this.innerHTML.toLowerCase() === pages[i].dataset.page) { - pages[i].classList.add("active"); - navigationLinks[i].classList.add("active"); + // Add 'active' class to the clicked navigation link + this.classList.add("active"); + + // Show the target page and hide others + pages.forEach((page) => { + if (page.dataset.page === targetPage) { + page.classList.add("active"); window.scrollTo(0, 0); } else { - pages[i].classList.remove("active"); - navigationLinks[i].classList.remove("active"); + page.classList.remove("active"); } - } - + }); }); -} \ No newline at end of file +}); \ No newline at end of file