Created
November 20, 2012 21:10
-
-
Save nicolasbinet/4121151 to your computer and use it in GitHub Desktop.
This file contains 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
// grab the initial top offset of the navigation | |
var $sticky_navigation = $('#site-nav'); | |
var sticky_navigation_offset_top = 102; | |
// our function that decides weather the navigation bar should have "fixed" css position or not. | |
var sticky_navigation = function(){ | |
var scroll_top = $(window).scrollTop(); // our current vertical position from the top | |
// if we've scrolled more than the navigation, change its position to fixed to stick to top, otherwise change it back to relative | |
if ($(window).width() > 600) { | |
if (scroll_top > sticky_navigation_offset_top) { | |
$sticky_navigation.css({ 'position': 'fixed', 'top': 0, 'left': 0 }).addClass('site-nav-fixed'); | |
} else { | |
$sticky_navigation.css({ 'position': 'absolute' }).removeClass('site-nav-fixed'); | |
} | |
} else { | |
$sticky_navigation.css({ 'position': 'static', 'top': 'auto', 'left': 'auto' }); | |
} | |
}; | |
// run our function on load | |
sticky_navigation(); | |
// and run it again every time you scroll | |
$(window).scroll(function() { | |
sticky_navigation(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment