Javascript function added to keep navigation bar sticked to the top when scrolling

This commit is contained in:
Lucas Patenaude
2024-04-07 04:59:09 -06:00
parent 2ad2b6706e
commit db748a38f9
3 changed files with 47 additions and 0 deletions

View File

@@ -3,3 +3,10 @@
box-shadow: 0px 3px 4px rgba(0, 0, 0, 0.4);
}
.sticky {
position: fixed;
top: 0;
width: 100%;
z-index: 1000; /* Adjust as needed */
}

View File

@@ -0,0 +1,37 @@
// navbar-sticky.js
// Function to add sticky behavior to the navbar
function makeNavbarSticky() {
// Get the navigation bar element
var navbar = document.getElementById("navigation-bar-container");
// Get the initial offset of the navbar from the top of the page
var navbarOffset = navbar.offsetTop;
// Function to add sticky behavior when scrolling
function stickyNavbar() {
// Check if the current scroll position is greater than or equal to the initial offset of the navbar
if (window.pageYOffset >= navbarOffset) {
// Add the 'fixed-top' class to make the navbar sticky
navbar.classList.add("fixed-top");
// Add padding to the body to prevent content from jumping when the navbar becomes sticky
document.body.style.paddingTop = navbar.offsetHeight + "px";
} else {
// Remove the 'fixed-top' class to make the navbar non-sticky
navbar.classList.remove("fixed-top");
// Reset the padding of the body
document.body.style.paddingTop = 0;
}
}
// Call the stickyNavbar function when the page is scrolled
window.onscroll = function() {
stickyNavbar();
};
}
// Call the makeNavbarSticky function when the page loads
window.onload = function() {
makeNavbarSticky();
};

View File

@@ -3,6 +3,9 @@
© 2024 - CSCI 3308 Group 6
</p>
<!-- In your HTML file -->
<script src="js/navigation-bar/navigation-bar-follow.js"></script>
<!-- Include Bootstrap JavaScript -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js"></script>