Skip to content

Instantly share code, notes, and snippets.

@mandiwise
Last active November 30, 2015 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mandiwise/3474a53b807f05f8b429 to your computer and use it in GitHub Desktop.
Save mandiwise/3474a53b807f05f8b429 to your computer and use it in GitHub Desktop.
A simple nav-bar locking jQuery script.
/**
* Navbar Locking
*/
$(function () {
var $window = $(window),
$mainNav = $('.main-navigation'), // nav wrapper element
stickyNavTop = $mainNav.offset().top;
// A helper function to check whether nav should be fixed
var stickyNav = function () {
var scrollTop = $window.scrollTop();
if ( scrollTop > stickyNavTop ) {
if ( $('body').hasClass('logged-in') ) {
$mainNav.addClass('under-admin-bar fixed-nav');
} else {
$mainNav.addClass('fixed-nav');
}
} else {
if ( $('body').hasClass('logged-in') ) {
$mainNav.removeClass('under-admin-bar fixed-nav');
} else {
$mainNav.removeClass('fixed-nav');
}
}
};
// Initialize nav classes...
stickyNav();
// Then re-run on scroll
$window.scroll(function () {
stickyNav();
});
});
.fixed-nav {
position: fixed;
width: 100%;
left: 0;
top: 0;
z-index: 100;
}
@media only screen and (min-width: 600px) {
.fixed-nav.under-admin-bar {
top: 46px;
}
}
@media only screen and (min-width: 768px) {
.fixed-nav.under-admin-bar {
top: 32px;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment