Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
Header disappearing on scroll

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" class="custom-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?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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/

about 4 years ago · Juan Pablo Isaza Relatório

0

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!

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda