Skip to content

Instantly share code, notes, and snippets.

@vosidiy
Created December 9, 2019 19:32
Show Gist options
  • Save vosidiy/cd14bb7dfbe381ec943933be01088180 to your computer and use it in GitHub Desktop.
Save vosidiy/cd14bb7dfbe381ec943933be01088180 to your computer and use it in GitHub Desktop.
Fixed on scroll
<header>
<div class="bg-warning py-2">
<div class="container">
Some top header info
</div>
</div>
<nav id="navbar_top" class="navbar navbar-expand-lg navbar-dark bg-primary">
<div class="container">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#main_nav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="main_nav">
<ul class="navbar-nav ml-auto">
<li class="nav-item"><a class="nav-link" href="#"> Menu item </a></li>
<li class="nav-item"><a class="nav-link" href="#"> Menu item </a></li>
</ul>
</div> <!-- navbar-collapse.// -->
</div> <!-- container.// -->
</nav>
</header>
///////////////// fixed menu on scroll for desktop
if ($(window).width() > 992) {
$(window).scroll(function(){
if ($(this).scrollTop() > 40) {
$('#navbar_top').addClass("fixed-top");
// add padding top to show content behind navbar
$('body').css('padding-top', $('.navbar').outerHeight() + 'px');
}else{
$('#navbar_top').removeClass("fixed-top");
// remove padding top from body
$('body').css('padding-top', '0');
}
});
} // end if
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment