2017-11-26 6 views
0

スクロールダウンするときにナビゲーションバーがフェードするのを防ぐにはどうすればいいですか?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

あなたはこれを試してみましたか? https://stackoverflow.com/questions/36448800/how-to-show-or-hide-a-menu-when-i-scroll-down-or-up –

+0

http://jsfiddle.net/vp7chr47/2/私は答えからこれを見つけましたが、私のコードにそれを書く方法はわかりません。 – inoi

答えて

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

+0

動作しません – inoi

+0

どういう意味ですか?これは動作例です:) –

+0

このコードスニペットを実行します。あなたのコードでもCSSを更新してください。 –

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>

関連する問題