Skip to content

Instantly share code, notes, and snippets.

@dangtrinhnt
Created June 9, 2014 02:37
Show Gist options
  • Save dangtrinhnt/fd01671a40a523939c51 to your computer and use it in GitHub Desktop.
Save dangtrinhnt/fd01671a40a523939c51 to your computer and use it in GitHub Desktop.
Floating navigation menu
$(function() {
// Stick the #nav to the top of the window
var nav = $('#nav');
var navHomeY = nav.offset().top;
var isFixed = false;
var $w = $(window);
$w.scroll(function() {
var scrollTop = $w.scrollTop();
var shouldBeFixed = scrollTop > navHomeY;
if (shouldBeFixed && !isFixed) {
nav.css({
position: 'fixed',
top: 0,
left: nav.offset().left,
width: nav.width()
});
isFixed = true;
}
else if (!shouldBeFixed && isFixed)
{
nav.css({
position: 'static'
});
isFixed = false;
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment