Skip to content

Commit

Permalink
Updated navbar.js
Browse files Browse the repository at this point in the history
added the debounce function and then wrapped the scroll event handler with it.
  • Loading branch information
harshshukla06 authored Nov 9, 2024
1 parent 9150e60 commit c8894bb
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,18 @@ overlay.addEventListener("click", function() {
overlay.style.visibility = "hidden";
});

window.addEventListener("scroll", function() {
// Debounce function
function debounce(func, wait = 20) {
let timeout;
return function() {
const context = this, args = arguments;
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(context, args), wait);
};
}

// Scroll event with debounce
window.addEventListener("scroll", debounce(function() {
const navbar = document.querySelector(".navbar-collapse");
const navLinks = document.querySelectorAll(".navbar-nav .nav-item a");

Expand All @@ -53,14 +64,13 @@ window.addEventListener("scroll", function() {
link.style.color = "#edf2f4";
});
}
});
}, 20)); // 20ms delay for debounce

function toggleDropdown(show) {
const dropdownMenu = document.querySelector('.dropdown-menu');
if (show) {
dropdownMenu.style.display = 'block';
} else {
dropdownMenu.style.display = 'none';
}
}


}

0 comments on commit c8894bb

Please sign in to comment.