Skip to content

Instantly share code, notes, and snippets.

@danielgamage
Last active July 5, 2016 00:53
Show Gist options
  • Save danielgamage/8f4100093c3d795ab135 to your computer and use it in GitHub Desktop.
Save danielgamage/8f4100093c3d795ab135 to your computer and use it in GitHub Desktop.
fade a background color on header over course of scrolling with jQuery
var scroller = function() {
var scrollPosition = $(window).scrollTop();
if ( scrollPosition < 90 ) {
var bgColor = 'rgba(0, 0, 0, ' + (scrollPosition / 100) + ')';
$('.navbar').css('background-color', bgColor);
} else {
$('.navbar').css('background-color', 'rgba(0, 0, 0, 0.9)');
}
}
scroller(); // call on load in case someone refreshes halway down the page or hits a hash link
$(window).on('scroll touchmove', function(){ // scroll for mouse/trackpad, touchmove for immediate touch response
scroller();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment