I've been experimenting with a navigation bar/header design for my website. I have it set up so that when you scroll 100px past the logo, the navbar becomes fixed at the top of the page.
Everything seems fine with this setup, but on smaller screens, I would like to keep the navbar fixed at the top as well since the logo is hidden. The issue arises when scrolling down on these smaller viewports, causing the script to hide and then reveal the navbar unnecessarily. I tried adding some CSS to prevent this on screens with a max-width of 768px, but it didn't work as expected. Is there a better way to handle this situation?
Feel free to check out the jsfiddle link in mobile view and try scrolling down.
Below is the code snippet I'm currently using:
$(window).scroll(function() {
var nav = $('#custom-bootstrap-menu');
var body = $('body');
var top = 100;
var logo = $('div#navlogo');
if ($(window).scrollTop() >= top) {
nav.addClass('navbar-fixed-top');
body.addClass('padding-fifty');
logo.css('display', 'block');
} else {
nav.removeClass('navbar-fixed-top');
body.removeClass('padding-fifty');
logo.css('display', 'none');
}
});
Appreciate any advice or suggestions you may have!