document.addEventListener("DOMContentLoaded", function () { /* ------------------------- MAIN MENU ACTIVE LINK BASED ON PAGE URL -------------------------- */ const mainMenuLinks = document.querySelectorAll("#menu-c07d47f .menu-item a"); const currentUrl = window.location.href;
mainMenuLinks.forEach(link => { link.classList.remove("active-link");
// Normalize href to full URL const linkHref = new URL(link.getAttribute("href"), window.location.origin).href;
if (linkHref === currentUrl) { link.classList.add("active-link"); } });
/* ------------------------- INNER MENU: SMOOTH SCROLL & ACTIVE LINK ON SCROLL -------------------------- */ const header = document.querySelector('header'); // Change if your header selector differs
function getHeaderHeight() { return header ? header.offsetHeight : 100; // fallback to 100px }
function initInnerMenu() { const innerMenuLinks = document.querySelectorAll("#advancedmenu .raven-adnav-menu a");
if (!innerMenuLinks.length) { setTimeout(initInnerMenu, 200); // retry if menu not ready return; }
// Remove all active classes and set on clicked link function setActiveLink(link) { innerMenuLinks.forEach(l => l.classList.remove("active-link")); link.classList.add("active-link"); }
// Smooth scroll accounting for sticky header function scrollToTarget(targetId) { const targetElement = document.querySelector(targetId); const headerOffset = getHeaderHeight();
if (targetElement) { const elementPosition = targetElement.getBoundingClientRect().top + window.pageYOffset; const offsetPosition = elementPosition - headerOffset;
window.scrollTo({ top: offsetPosition, behavior: "smooth" }); } }
// Click event on inner menu links for smooth scroll + active class innerMenuLinks.forEach(link => { link.addEventListener("click", function (event) { const targetId = this.getAttribute("href"); if (targetId && targetId.startsWith("#")) { event.preventDefault(); setActiveLink(this); scrollToTarget(targetId); history.pushState(null, null, targetId); } }); });
// On scroll, highlight menu link based on section in viewport function highlightMenuOnScroll() { const scrollPosition = window.scrollY + getHeaderHeight() + 10;
document.querySelectorAll(".elementor-element[id]").forEach(section => { const sectionTop = section.offsetTop; const sectionHeight = section.offsetHeight;
if (scrollPosition >= sectionTop && scrollPosition < sectionTop + sectionHeight) { const id = section.getAttribute("id"); const activeLink = document.querySelector(`#advancedmenu .raven-adnav-menu a[href="#${id}"]`); if (activeLink) { setActiveLink(activeLink); } } }); } window.addEventListener("scroll", highlightMenuOnScroll); // On page load, if URL has hash, scroll to it and set active if (window.location.hash) { scrollToTarget(window.location.hash); const activeLink = document.querySelector(`#advancedmenu .raven-adnav-menu a[href="${window.location.hash}"]`); if (activeLink) { setActiveLink(activeLink); } } else if (innerMenuLinks.length) { setActiveLink(innerMenuLinks[0]); // Default active first link } } initInnerMenu(); });
