Home > Blockchain >  Header disappearing on scroll
Header disappearing on scroll

Time:05-08

I'm trying to make my header disappear when scrolling down and only re-appear when scrolling up. I can't get it to work: http://jsfiddle.net/mxj562qt/

Any ideas where I'm going wrong?

HTML:

<div id="header" >
    This is your menu.
</div>
<main>
    This is your body.
</main>
<footer>
    This is your footer.
</footer>

Javascript:

// Hide Header on on scroll down
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = $("#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
        $("#header").addClass('nav-up');
    } else {
        // Scroll Up
        if(st   $(window).height() < $(document).height()) {
            $("#header").removeClass('nav-up');
        }
    }
    
    lastScrollTop = st;
}

CSS:

body {
    padding-top: 40px;
}

#header {
    background: #f5b335;
    height: 50px;
    position: fixed;
    top: 0;
    transition: top 0.2s ease-in-out;
    width: 100%;
}

.nav-up {
    top: -50px;
}

main {
    height: 2000px;
}

footer { background: #ddd;}
* { color: transparent}

It would appear that the CSS class doesn't get added but I'm not sure why. Am I referencing the Div in the wrong way?

CodePudding user response:

So, I can see that the issue stems from this bit of code ...

// Scroll Up
if(st   $(window).height() < $(document).height()) {
    $("#header").removeClass('nav-up');
}

In my tests, the doc height was always > than the st window height.

I did this ...

// Scroll Up
console.log('doc height: ', $(document).height());
console.log('st window height: ', st   $(window).height());
        
if(st   $(window).height() < $(document).height()) {
    $("#header").removeClass('nav-up');
}

// results from scrolling up   down
// doc height:       2058
// st window height: 313
// doc height:       2058
// st window height: 280
// doc height:       2058
// st window height  1614
// doc height:       2058
// st window height: 1580

Changing the aforementioned JS to this seems to get you where you need to be.

$("#header").removeClass('nav-up');

Then your CSS needed some work ...

I noticed that your top element wasn't applying due to the CSS selector priority.

.nav-up {
    top: -50px !important;
}

The result: scrolling down, the nav bar hides, scrolling up, the navbar shows.

I forked your code below; http://jsfiddle.net/itsbjk/aw6qb2mr/16/

CodePudding user response:

The problem here is with your CSS. You have specified position:fixed; in your code and that bit of CSS overrides all the JS you are writing. Fixed will force your header to be visible no matter what you are doing. Instead, you could try this in your CSS:

body {
    padding-top: 40px;
}

#header {
    background: #f5b335;
    height: 50px;
    position: absolute;
    top: 0;
    transition: top 0.2s ease-in-out;
    width: 100%;
}

.nav-up {
    top: -50px;
}

main {
    height: 2000px;
}

footer { background: #ddd;}
* { color: transparent}

The absolute property should make it disappear on scrolling. And also, your referencing of the <div> tag isn't wrong!

  • Related