A Pen by Stan Williams on CodePen.
Created
July 10, 2025 06:17
-
-
Save stanwmusic/cabbc5a6140a0ee4b343df47ab181c16 to your computer and use it in GitHub Desktop.
Navbar - Bootstrap 4 | sticky Navbar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--top header--> | |
<header > | |
<!--most top info --> | |
<div style="height: 40px; background: #f2f9ee"></div> | |
<!-- End most top info --> | |
<nav class="navbar navbar-expand-lg navbar-light top-navbar" data-toggle="sticky-onscroll"> | |
<div class="container"> | |
<a class="navbar-brand" href="#">Navbar</a> | |
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> | |
<span class="navbar-toggler-icon"></span> | |
</button> | |
<div class="collapse navbar-collapse justify-content-end" id="navbarSupportedContent"> | |
<ul class="navbar-nav pull-right"> | |
<li class="nav-item"> | |
<a class="nav-link active" href="#">Home</a> | |
</li> | |
<li class="nav-item"> | |
<a class="nav-link" href="#">Services</a> | |
</li> | |
<li class="nav-item"> | |
<a class="nav-link" href="#">Our Work</a> | |
</li> | |
<li class="nav-item"> | |
<a class="nav-link" href="#">Pricing</a> | |
</li> | |
<li class="nav-item"> | |
<a class="nav-link" href="#">About</a> | |
</li> | |
<li class="nav-item"> | |
<a class="nav-link" href="#">Contact</a> | |
</li> | |
<li class="nav-item"> | |
<a class="btn btn-primary" href="#">GET A QUOTE</a> | |
</li> | |
</ul> | |
</div> | |
</div> | |
</nav> | |
</header> | |
<!-- End top header--> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sticky navbar | |
// ========================= | |
$(document).ready(function () { | |
// Custom function which toggles between sticky class (is-sticky) | |
var stickyToggle = function (sticky, stickyWrapper, scrollElement) { | |
var stickyHeight = sticky.outerHeight(); | |
var stickyTop = stickyWrapper.offset().top; | |
if (scrollElement.scrollTop() >= stickyTop) { | |
stickyWrapper.height(stickyHeight); | |
sticky.addClass("is-sticky"); | |
} | |
else { | |
sticky.removeClass("is-sticky"); | |
stickyWrapper.height('auto'); | |
} | |
}; | |
// Find all data-toggle="sticky-onscroll" elements | |
$('[data-toggle="sticky-onscroll"]').each(function () { | |
var sticky = $(this); | |
var stickyWrapper = $('<div>').addClass('sticky-wrapper'); // insert hidden element to maintain actual top offset on page | |
sticky.before(stickyWrapper); | |
sticky.addClass('sticky'); | |
// Scroll & resize events | |
$(window).on('scroll.sticky-onscroll resize.sticky-onscroll', function () { | |
stickyToggle(sticky, stickyWrapper, $(this)); | |
}); | |
// On page load | |
stickyToggle(sticky, stickyWrapper, $(window)); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/js/bootstrap.min.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*sticky header position*/ | |
.sticky.is-sticky { | |
position: fixed; | |
left: 0; | |
right: 0; | |
top: 0; | |
z-index: 1000; | |
width: 100%; | |
} | |
body { | |
min-height: 1200px; | |
} | |
nav { | |
background: #eaebec; | |
min-height: 85px; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment