0

Currently i want to show my navbar when i am scrolling on my webpage and hide my navbar when i am scrolling back. I search all over on Google, and i could not find something that could help me. I am surprised that this wasn't on the web yet. Maybe i didn't search good enough, however, after several hours of frustrating myself, i decided to get some help from the pro's here. ;-)

Would appreciate any help!

<body>
<nav>
     <ul>
          <li><a href="#">Help</a></li>
     </ul>
</nav>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"</script>
</body>

nav{
    position: fixed;
    padding: 30px 70px;
    width: 100vw;
    height: 10vh;
    background: black;
    transition: .5s ease;
    z-index: 5;
}

nav ul{
    float: right;
    margin-right: 100px;
    padding: 0px 40px;
}


$(window).scroll(function () {

    var scroll = $(window).scrollTop();
    var p = $win.scrollTop() / max;
    var x = window.matchMedia("(max-width: 800px)")

    console.log(scroll);
        if (scroll > 0) {
            $('nav').css({
                "top": "0%",
            });
        } else {
            $('nav').css({
                "top": "-25%",
            });
        };
});

1 Answers1

0

I think you mean something like that https://www.w3schools.com/howto/howto_js_navbar_slide.asp

Or something like this:

let lastScrollTop = 0;
function scrollFunction() {
  scroll = document.body.scrollTop || document.documentElement.scrollTop;
  if (scroll > lastScrollTop) // Scrolling Down
    document.getElementById("navbar").style.top = "0";
  else // Scrolling Up
    document.getElementById("navbar").style.top = "-50px";
  lastScrollTop = scroll;
}

window.onscroll = function() {scrollFunction()};

let lastScrollTop = 0;
function scrollFunction() {
  scroll = document.body.scrollTop || document.documentElement.scrollTop;
  if (scroll > lastScrollTop) // Scrolling Down
    document.getElementById("navbar").style.top = "0";
  else // Scrolling Up
    document.getElementById("navbar").style.top = "-50px";
  lastScrollTop = scroll;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
  margin: 0;
  background-color: #f1f1f1;
  font-family: Arial, Helvetica, sans-serif;
}

#navbar {
  background-color: #333;
  position: fixed;
  top: -50px;
  width: 100%;
  display: block;
  transition: top 0.3s;
}

#navbar a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 15px;
  text-decoration: none;
  font-size: 17px;
}

#navbar a:hover {
  background-color: #ddd;
  color: black;
}
</style>
</head>
<body>

<div id="navbar">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
</div>

<div style="padding:15px 15px 2500px;font-size:30px">
  <p><b>This example demonstrates how to slide down a navbar when the user starts to scroll the page.</b></p>
  <p>Scroll down this frame to see the effect!</p>
  <p>Scroll to the top to hide the navbar.</p>
  <p>Lorem ipsum dolor dummy text sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>

</body>
</html>

And someone already did it in jQuery How can I determine the direction of a jQuery scroll event?

MK.
  • 752
  • 1
  • 8
  • 12