Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

269
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!