I'm currently working on the development of a website that includes a feature where a static navbar transforms into a fixed navbar after scrolling down 500px. The transition from "navbar-static-top" to "navbar-fixed-top" is functioning properly. However, I am looking for assistance in implementing a functionality that changes the navbar back to static when the user scrolls back to the top of the page.
I have searched online for solutions and attempted to use the jQuery 'hasClass()' function without success. Do you have any suggestions or ideas on how to achieve this?
This is the current code snippet that I am working with:
$(window).scroll(function() {
$('#navbarMain').each(function(){
var imagePos = $(this).offset().top;
var topOfWindow = $(window).scrollTop();
if (imagePos > topOfWindow-500) {
$(this).removeClass("navbar-static-top");
$(this).addClass("navbar-fixed-top");
}
});
});