Skip to content

Instantly share code, notes, and snippets.

@amboy00
Created May 15, 2015 14:30
Show Gist options
  • Save amboy00/ec65c2d2ee7d11757b13 to your computer and use it in GitHub Desktop.
Save amboy00/ec65c2d2ee7d11757b13 to your computer and use it in GitHub Desktop.
Change the css class after a scrolled threshold. CSS has a background change.
nav#main {
position: fixed;
z-index: 200;
top: 0;
left: 0;
width: 100%;
background: rgba(255, 255, 255, 0.01);
transition: all 0.3s ease;
&.opaque {
background: rgba(255, 255, 255, 0.8);
}
}
// Navigation Scrollllllling
var nav = $('#main'),
threshold = 500,
scrollPos = 0,
opaqueState = false;
$(document).on('scroll', function() {
scrollPos = $(this).scrollTop();
if(scrollPos > threshold) {
opaque(true);
} else {
opaque(false);
}
});
function opaque(state) {
if ( state && !opaqueState ) {
nav.addClass('opaque');
opaqueState = true;
}
if ( !state && opaqueState ) {
nav.removeClass('opaque');
opaqueState = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment