Utilizing a basic tutorial from here to implement show/hide functionality for a standard BS4 navbar on scroll. Works flawlessly on desktop.
However, facing an issue with the mobile view where the navbar behaves oddly when scrolling down and returning to the top. Upon reaching the top again, the navbar disappears once more.
Suspecting that the problem lies within the scrollTop()
function but unable to pinpoint the exact cause of the issue.
Sharing my JS code below:
if ($('.smart-scroll').length > 0) { // check if element exists
var last_scroll_top = 0;
$(window).on('scroll', function() {
var scroll_top = $(this).scrollTop();
if(scroll_top < last_scroll_top) {
$('.smart-scroll').removeClass('scrolled-down').addClass('scrolled-up');
} else {
$('.smart-scroll').removeClass('scrolled-up').addClass('scrolled-down');
}
last_scroll_top = scroll_top;
/* Tried to catch for scroll_top zero, but doesn't help */
if(scroll_top == 0) $('.smart-scroll').removeClass('scrolled-up');
});
}
Additionally, presenting my CSS styles:
.smart-scroll {
position: fixed !important;
top: 0;
right: 0;
left: 0;
z-index: 1000;
transition: all 0.3s ease-in-out;
}
.scrolled-down {transform:translateY(-100%);}
.scrolled-up {transform:translateY(0);}
Even attempted integrating suggestions from this stackoverflow thread without success.
Any insights on how to resolve this issue for mobile devices?