0

How can I make my navigation bar fade when scrolling down.I can use HTML, CSS and Javascript. From my search I've seen some examples on how to do this but the navigation bar was with the div element and I don't know how to implement those functions in my code.

HTML:

        <li><a href="#" id="i">Hiking</a></li>
        <li><a href="">Surfing</a></li>
        <li><a href="">Scuba Diving</a></li>
        <li><a href="">Camping</a></li>
        <li style="float:right;  margin:10px;">
            <form method="GET" action="https://maps.google.com?" target="_blank">
                <input type="submit" value="Search location">

            </form>
        </li>
    </ul>
</nav>

CSS:

nav {
margin: 0 0 0 0;
font-family: sans-serif;
font-size: 100%;
width: 100%;
overflow: hidden;
}

nav ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #333333;
    text-align: center;
    vertical-align: top;
    margin-top: 130px;
}

* {
margin: 0;
padding: 0;
line-height: 1.5;
position: sticky;
top: -130px;
}

#nav li {
float: left;
border-right: 1px solid;
}

#nav a {
display: block;
color: white;
text-align: center;
width: 100px;
padding: 10px;
text-decoration: none;
}
inoi
  • 119
  • 1
  • 9
  • 2
    Did you try this? https://stackoverflow.com/questions/36448800/how-to-show-or-hide-a-menu-when-i-scroll-down-or-up – Andrii Pryimak Nov 26 '17 at 18:29
  • http://jsfiddle.net/vp7chr47/2/ I found this from an answer but I don't know how to write it in my code. – inoi Nov 26 '17 at 18:37

2 Answers2

0

Please check this answer. I added id to your nav tag, some dummy content to show the example and JS code.

var lastScrollTop = 0;

window.addEventListener("scroll", function(){  
   var st = window.pageYOffset || document.documentElement.scrollTop;  
   if (st > lastScrollTop){
       document.getElementById("bottommenu").style.top = "-100%";
   } else {
      document.getElementById("bottommenu").style.top = "0";
   }
   lastScrollTop = st;
}, false);
nav {
margin: 0 0 0 0;
font-family: sans-serif;
font-size: 100%;
top:0;
 position: fixed;
  width: 100%;
  height: auto; 
  -webkit-transition: top 2s;
  transition: top 2s;
}

nav ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #333333;
    text-align: center;
    vertical-align: top;
}

* {
margin: 0;
padding: 0;
line-height: 1.5;
position: sticky;
top: -130px;
}

#nav li {
float: left;
border-right: 1px solid;
}

#nav a {
display: block;
color: white;
text-align: center;
width: 100px;
padding: 10px;
text-decoration: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav id="bottommenu">
        <ul>
        <li><a href="#" id="i">Hiking</a></li>
        <li><a href="">Surfing</a></li>
        <li><a href="">Scuba Diving</a></li>
        <li><a href="">Camping</a></li>
        <li style="float:right;  margin:10px;">
            <form method="GET" action="https://maps.google.com?" target="_blank">
                <input type="submit" value="Search location">

            </form>
        </li>
    </ul>
</nav>
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
<br>awd
Andrii Pryimak
  • 797
  • 2
  • 10
  • 33
0

The solution is very easy, need a simple script function and few css. Look the example, the padding on body is important to avoid a flick when fadeOut.

$(document).ready(function() {

    $(window).scroll(function() {
        var scroll = $(window).scrollTop();

        if (scroll > 50) {            
            $('#nav').fadeOut();
        } else {
            $('#nav').fadeIn();
        }
    });

});
body{
  padding-top: 50px; /* Same height use it in js to toggle fade */
}

#nav{
  position: fixed;
  top: 0;
  left: 0;
  background-color: black;
  color: white;
  width: 100%;
  padding: 5px 10px;
}

#nav > a {
  color inherit;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<nav id="nav">
<a>Item 1</a>
<a>Item 2</a>
<a>Item 3</a>
</nav>

<div>

What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).


</div>
dave008
  • 402
  • 3
  • 9