I'm working on a site which requires a sticky nav bar, which should 'pop out' of the header when the user scrolls down the page. It should then return to its original positioning within the header when the user scrolls back to the top.
My problem is that my .sticky
class is not being removed when the user scrolls back to the top of the page. I have already researched other questions regarding jQuery's removeClass()
not working, but none of the proposed solutions to these questions have worked for my case.
I considered whether my 'if' statement condition was causing the problem but when tracing out the scrollTop()
numerical values, everything seems to be correct. The 'else' condition definitely runs (I verified this with more console logs), but the jQuery removeClass()
does not.
var stickyHeaderOffsetValue = $('#sticky-container').offset().top;
var currentOffsetPosition = $(window).scrollTop();
if (currentOffsetPosition >= stickyHeaderOffsetValue) {
$('#sticky-container').addClass('sticky');
} else {
$('#sticky_navigation').removeClass(); // this didn't work
}