Skip to content

Instantly share code, notes, and snippets.

@debonx
Created November 19, 2019 20:01
Show Gist options
  • Save debonx/e3d5e76a35cc410be3d119f1b797fb04 to your computer and use it in GitHub Desktop.
Save debonx/e3d5e76a35cc410be3d119f1b797fb04 to your computer and use it in GitHub Desktop.
Sticky navigation bar in jQuery.
$(function(){
var position;
var width = $(window).width();
var position = $(this).scrollTop();
$(document).scroll(function() {
var currentScroll = $(this).scrollTop();
//Detecting if it's scrolling down
if( currentScroll > position ){
//Instructions for breakpoints after certain amount of pixels
if (currentScroll > 400) {
$('#top-bar').slideUp(0);
}
//Detecting if it's scrolling up
} else {
//Specify other actions after scrolling up
if(!$('#top-bar').is(':visible')){
$('#top-bar').slideDown(150).delay(1000);
}
}
position = currentScroll;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment