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

270
Visualizações
Script for hiding navbar...not on top, neither on bottom of scroll

Im a graphic designer with a little knowledge of code... but this is driving me nuts...

I got a classic navigation bar thats hidden unless user scrolls a little bit down. I got this action with javascript. OK.

But I want this happens (the action of showing navigation bar) only on the middle of every page. Like 100px away from top and 100px away from bottom of the scroll. The reason is I got breadcrumbs on top and footer on bottom of the page, I dont need the navbar there.

I tried my best to mix what I got (the navbar) with some scroll detectors, or reading bars that get scroll position... but was impossible to get something work...

¿any help please?

My code is this right now, only the navbar (improvements welcome too...heheh):

    var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
  var currentScrollPos = window.pageYOffset;
  if (prevScrollpos > currentScrollPos) {
    document.getElementById("navbar").style.bottom = "0";
  } else {
    document.getElementById("navbar").style.bottom = "-100px";
  }
  prevScrollpos = currentScrollPos;
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If I understood you correctly, I believe you want something like this:

    window.onscroll = () => { 
        const scrollPos = window.scrollY; 
        const offset = 100; const targetEl = document.body; //Adjust to preference 
        if(scrollPos > offset && (targetEl.scrollHeight - window.innerHeight - scrollY) > offset )
            console.log('shownavbar')
        else
            console.log('hidenavbar')
    }
about 4 years ago · Juan Pablo Isaza Relatório

0

Got it thanks to @NourAshraf. I did some changes with the code I got before and yours. Now works perfect!

Just if anyone need it finally is something like this. To check it easier I put to change the body colour to green (when the navigation bar should show) and red (when should not appear).

var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
  var currentScrollPos = window.pageYOffset;
  var offset = 500; const targetEl = document.body; //Adjust to preference 

  if (prevScrollpos > currentScrollPos && currentScrollPos > offset && (targetEl.scrollHeight - window.innerHeight - scrollY) > offset) {
    document.body.style.backgroundColor = "green";
  } else {
    document.body.style.backgroundColor = "red";
  }
  prevScrollpos = currentScrollPos;
}
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