0

I want my navbar to be transparant on the top and bottom of my page but i want it to not be transparant in the middle. When i have my webpage on full screen this works:

$(window).on("scroll", function () {
    if ($(window).scrollTop() > 720 && $(window).scrollTop() < 1450 ) {
        $(".nav").addClass("active");
    } else {
        $(".nav").removeClass("active");
    }
})

But when it gets resized this wont work anymore because the sizes change. Is there a way to do this with % instead of just normal numbers so it will be responsive?

ADreNaLiNe-DJ
  • 4,787
  • 3
  • 26
  • 35
FluffyMe
  • 105
  • 8
  • this thread may help https://stackoverflow.com/questions/3464876/javascript-get-window-x-y-position-for-scroll – t3__rry Feb 15 '18 at 13:23

2 Answers2

0

It occur because you hardcoded your height values. Check the whole site height, divide it on three and incorporate this variables to your if statement. Every time you resize browser window it will recalculate your new position.

window.addEventListener('resize', function() {
  //one third and two third of website
  oneThird = window.scrollHeight / 3;
  twoThird = onethird * 2;

  if ( $(window).scrollTop() > oneThird && $(window).scrollTop() < twoThird ) {
      $(".nav").addClass("active");
  } else {
      $(".nav").removeClass("active");
  }
}
David C.
  • 201
  • 2
  • 11
-1

You can use Media Queries with JS too, so you can do certain things on your desired window size, this might help https://www.w3schools.com/howto/howto_js_media_queries.asp

Osama Xäwãñz
  • 437
  • 2
  • 8
  • 21