Skip to content

Instantly share code, notes, and snippets.

@whitershade
Last active January 28, 2018 19:03
Show Gist options
  • Save whitershade/9dacc4c35e9ffcb995d9 to your computer and use it in GitHub Desktop.
Save whitershade/9dacc4c35e9ffcb995d9 to your computer and use it in GitHub Desktop.
fixed top-menu when you scroll up
HTML
<header class="main-header hidden-main-header"> ... </header> // the same hidden main-header in the bottom of document
CSS
.hidden-main-header {
position: fixed;
display: none;
left: 0;
right: 0;
top: 0;
z-index: 2;
}
.fixed {
display: block;
}
JS
$(document).ready(function () {
var previousScroll = 0;
$(window).scroll(function (event) {
var scroll = $(this).scrollTop();
if (scroll < previousScroll) {
// upscroll code
$(".hidden-main-header").addClass("fixed");
} else {
// downscroll code
$(".hidden-main-header").removeClass("fixed");
}
previousScroll = scroll;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment