JSFiddle - React, Tailwind, and code Playground
HTML
<div style="height: 1000px">
<div class="site-header">
Hej
</div>
<div class="entry-header h1">
Hej
</div>
</div>
CSS
.nav-down {
position: fixed;
width: 100%;
display: block;
}
.nav-up {
top: -176px;
}
.filter-down {
position: fixed;
margin-left: auto;
margin-right: auto;
width: 72%;
z-index: 1000;
}
.filter-up {
width: 100%;
}
.site-header {
height: 176px;
background-color: #CCC;
z-index: 10;
}
.entry-header {
height: 200px;
width: 100%;
z-index: 1;
background-color: #222;
}
body {
background-color: #555;
}
JavaScript
// Hide Header on on scroll down
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = $('.site-header').outerHeight();
var filterHeight = $('.entry-header').outerHeight();
$(window).scroll(function(event){
didScroll = true;
});
setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 250);
function hasScrolled() {
var st = $(this).scrollTop();
// Make sure they scroll more than delta
if(Math.abs(lastScrollTop - st) <= delta)
return;
// If they scrolled down and are past the navbar, add class .nav-up.
// This is necessary so you never see what is "behind" the navbar.
if (st > lastScrollTop && st > navbarHeight){
// Scroll Down
$('.site-header').removeClass('nav-down').addClass('nav-up');
} else {
// Scroll Up
if(st + $(window).height() < $(document).height()) {
$('.site-header').removeClass('nav-up').addClass('nav-down');
}
}
if (st > lastScrollTop && st > filterHeight){
// Scroll Down
$('.entry-header').removeClass('filter-down').addClass('filter-up');
} else {
// Scroll Up
if(st + $(window).height() < $(document).height()) {
$('.entry-header').removeClass('filter-up').addClass('filter-down');
}
}
lastScrollTop = st;
}