向下滚动时淡出导航栏

时间:2017-11-26 18:25:48

标签: css html5

滚动时如何让导航栏淡出。我可以使用HTML,CSS和Javascript。从我的搜索中我看到了一些关于如何执行此操作的示例,但导航栏上有div元素,我不知道如何在我的代码中实现这些功能。

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;
}

2 个答案:

答案 0 :(得分:0)

请检查此答案。我在你的nav标签中添加了id,一些虚拟内容用于显示示例和JS代码。

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

答案 1 :(得分:0)

解决方案非常简单,需要简单的脚本功能和少量的CSS。看一下这个例子,身体上的填充对于避免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>