I am working on customizing a Bootstrap 4 top-sticky nav to only be visible on mobile devices. Using CSS (d-block d-md-none) can achieve this, but on desktop, I want the menu to fade in as the user scrolls down past a certain point and disappear when scrolling back up. However, my current implementation causes the menu to briefly show on load on desktop.
(function ($) {
$(document).ready(function(){
$(".navbar").hide();
$(function () {
$(window).scroll(function () {
if($(window).width() >= 768) {
if ($(this).scrollTop() > 500) {
$('.navbar').fadeIn();
} else {
$('.navbar').fadeOut();
}
}
});
});
});
}(jQuery));
Another approach is to use classes for adding and removing elements to prevent the flicker effect. However, this method may result in an abrupt transition, and I'm facing challenges with its implementation:
$(function() {
var div = $(".navbar");
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll >= 200) {
div.removeClass('d-block').removeClass("d-md-none");
} else {
div.addClass("d-block").addClass('d-md-none');
}
});
});
Any assistance would be greatly appreciated. Thank you!