Created
June 9, 2014 02:37
-
-
Save dangtrinhnt/fd01671a40a523939c51 to your computer and use it in GitHub Desktop.
Floating navigation menu
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
$(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