Created
April 24, 2013 01:56
-
-
Save pongstr/5449006 to your computer and use it in GitHub Desktop.
sticky topbar
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
//Calculate the height of <header> | |
//Use outerHeight() instead of height() if have padding | |
var aboveHeight = $('#topbar').outerHeight(); | |
// on scroll event | |
$(window).scroll(function(){ | |
//if scrolled down more than the header's height | |
if ($(window).scrollTop() > aboveHeight){ | |
$('#topbar').addClass('sticky').fadeIn(); | |
} else { | |
// when scroll up or less than aboveHeight, | |
// remove the "fixed" class, and the padding-top | |
$('.sticky') | |
.css( 'margin-top', aboveHeight + 50 ) | |
.animate({ 'margin-top' : 0 }, 'fast', function(){ | |
$(this).removeAttr('style'); | |
}) | |
.removeClass('sticky'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment