2017-08-29 192 views
2

我試圖在向下滾動/向上時隱藏/顯示Bootstrap導航欄,但似乎不起作用。我卡住了,我真的很感謝這裏的一些幫助。誰能告訴我我做錯了什麼?請記住,我正在學習如何使用Bootstrap,並且對jQuery瞭解甚少。 當我用瀏覽器控制檯檢查代碼時,它不顯示任何錯誤。 我也試過fadeInfadeOut以及addClass和removeClass函數,但沒有運氣。滾動時隱藏bootstrap導航欄

$(document).ready(function() { 
 

 
    var banner = $("#navscroll"); 
 
    $(window).scroll(function() { 
 
    var scroll = $(window).scrollTop(); 
 
    if (scroll >= banner.height()) { 
 
     $("#banner").hide(); 
 

 
    } else { 
 
     $("banner").show(); 
 
    } 
 
    }); 
 

 
}); 
 

 
console.log();
body { 
 
    background-color: azure; 
 
} 
 

 
.divi { 
 
    width: 500px; 
 
    height: 500px; 
 
} 
 

 
#divi1 { 
 
    background-color: red; 
 
} 
 

 
#divi2 { 
 
    background-color: greenyellow; 
 
}
<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <!-- Required meta tags --> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 
 

 
    <!-- Bootstrap CSS --> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous"> 
 
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script> 
 
    <!-- Stylesheet for Mysite --> 
 

 

 
    <title>My Site</title> 
 

 
</head> 
 

 
<body> 
 

 
    <!-- Banner --> 
 
    <div id="banner" class="container-fluid"> 
 
    <nav id="navscroll" class="navbar navbar-toggleable-sm navbar-light fixed-top"> 
 
     <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation"> 
 
      <span class="navbar-toggler-icon"></span> 
 
      </button> 
 
     <a class="navbar-brand" href="#">My Site</a> 
 
     <div class="collapse navbar-collapse" id="navbarNavAltMarkup"> 
 
     <div class="navbar-nav"> 
 
      <a class="nav-item nav-link active" href="#">Me<span class="sr-only">(current)</span></a> 
 
      <a class="nav-item nav-link" href="#">What I do</a> 
 
      <a class="nav-item nav-link" href="#">Find Me</a> 
 
     </div> 
 
     </div> 
 
    </nav> 
 
    </div> 
 
    <!-- Banner --> 
 

 
    <!-- Divs --> 
 
    <div class="container-fluid"> 
 
    <div id="divi1" class="divi"></div> 
 
    <div id="divi2" class="divi"></div> 
 
    </div> 
 
    <!-- Divs --> 
 

 

 

 
    <!-- jQuery first, then Tether, then Bootstrap JS. --> 
 

 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script> 
 
</body> 
 

 
</html>

+0

你忘了''#'在$( 「旗幟」)顯示();' –

+0

這將有助於http://jsfiddle.net/nwV2r/699/ –

+0

Sanchit Patiyal感謝這一點。我試圖在我的代碼中實現它,但它不起作用。 – AtlantisNaranja

回答

1

有兩個問題向上滾動時防止你的代碼顯示後面的導航欄:

  1. $("banner").show(); =>有「#」 banner前失蹤。
  2. 隱藏元素時,其高度不會保留。因此,在已經隱藏之後測試nvarbar高度時,會得到意想不到的結果。因此,我建議預先保存高度並將其與保存的變量進行比較。

固定代碼是:

$(document).ready(function() { 
 
    var banner_height = $("#navscroll").height(); 
 
    var lastScrollTop = 0; 
 
    $(window).scroll(function() { 
 
    var scroll = $(window).scrollTop(); 
 
    var currScrollTop = $(this).scrollTop(); 
 
    if (scroll >= banner_height && currScrollTop > lastScrollTop) { 
 
     $("#banner").hide(); 
 
    } else { 
 
     $("#banner").show(); 
 
    } 
 
    lastScrollTop = currScrollTop; 
 

 
    }); 
 

 
});
body { 
 
    background-color: azure; 
 
} 
 

 
.divi { 
 
    width: 500px; 
 
    height: 500px; 
 
} 
 

 
#divi1 { 
 
    background-color: red; 
 
} 
 

 
#divi2 { 
 
    background-color: greenyellow; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <!-- Required meta tags --> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 
 

 
    <!-- Bootstrap CSS --> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous"> 
 
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script> 
 
    <!-- Stylesheet for Mysite --> 
 

 
    <title>My Site</title> 
 

 
</head> 
 

 
<body> 
 

 
    <!-- Banner --> 
 
    <div id="banner" class="container-fluid"> 
 
    <nav id="navscroll" class="navbar navbar-toggleable-sm navbar-light fixed-top"> 
 
     <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation"> 
 
      <span class="navbar-toggler-icon"></span> 
 
      </button> 
 
     <a class="navbar-brand" href="#">My Site</a> 
 
     <div class="collapse navbar-collapse" id="navbarNavAltMarkup"> 
 
     <div class="navbar-nav"> 
 
      <a class="nav-item nav-link active" href="#">Me<span class="sr-only">(current)</span></a> 
 
      <a class="nav-item nav-link" href="#">What I do</a> 
 
      <a class="nav-item nav-link" href="#">Find Me</a> 
 
     </div> 
 
     </div> 
 
    </nav> 
 
    </div> 
 
    <!-- Banner --> 
 

 
    <!-- Divs --> 
 
    <div class="container-fluid"> 
 
    <div id="divi1" class="divi"></div> 
 
    <div id="divi2" class="divi"></div> 
 
    </div> 
 
    <!-- Divs --> 
 

 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script> 
 
</body> 
 

 
</html>

+0

非常感謝。它像一個魅力!我不知道爲什麼我以前看不到這個。 – AtlantisNaranja

+0

很高興能幫到你:) – Tomer

+0

對不起@Tomer,還有一件事。當我向上滾動時,無論我的滾動位置如何,我的導航欄都不顯示。它只顯示我是否在頁面的頂部。我怎樣才能解決這個問題?謝謝 – AtlantisNaranja

0

嘗試定義旗幟的高度滾動功能外,你也忘了#,你的腳本看起來像這樣

$(document).ready(function(){ 

       var banner = $("#navscroll"); 
       var bannerHeight = banner.height(); 
       $(window).scroll(function(){ 
        var scroll = $(window).scrollTop(); 
        if (scroll >= bannerHeight){ 
         $("#banner").hide(); 
        } else { 
         $("#banner").show(); 
        } 
       }); 

     }); 
0

我有固定的代碼爲你添加了一些評論, 嘗試打開控制檯並滾動查看發生了什麼。

基本上,您將滾動高度與從40更改爲-16時的橫幅高度進行比較,因此您必須先保存它。

 $(document).ready(function(){ 
      var banner = $("#navscroll"); 
      var bannerStartHeight = $("#navscroll").height(); 
      $(window).scroll(function(){ 
       var scroll = $(window).scrollTop(); 
       console.log("SCROLL: "+scroll) 
       console.log("BANNER HEIGHT:"+banner.height()) 
       console.log("BANNER START HEIGHT: "+bannerStartHeight) 
       if (scroll >= bannerStartHeight){ 
        $("#banner").hide(); 
       } else { 
        $("#banner").show(); //here u forgot a # 
       } 
      }); 
    }); 

希望它可以幫助

0

Banner heightscroll函數之外進行計算。

$(document).ready(function() { 
 

 
    var banner = $("#navscroll"); 
 
    var bannerHgt = banner.height(); 
 
    $(window).scroll(function() { 
 
    var scroll = $(window).scrollTop(); 
 
    if (scroll >= bannerHgt) { 
 
     $("#banner").hide(); 
 
    } else { 
 
     $("#banner").show(); 
 
    } 
 
    }); 
 

 
}); 
 

 
console.log();
body{ 
 
    
 
      background-color: azure; 
 
     } 
 

 
     .divi{ 
 

 
      width: 500px; 
 
      height: 500px; 
 
     } 
 

 
     #divi1{ 
 

 
      background-color: red; 
 
     } 
 

 
     #divi2{ 
 

 
      background-color: greenyellow; 
 
     }
<!DOCTYPE html> 
 
<html lang="en"> 
 
    <head> 
 
    <!-- Required meta tags --> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 
 

 
    <!-- Bootstrap CSS --> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous"> 
 
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script> 
 
    <!-- Stylesheet for Mysite --> 
 
     
 
     
 
    <title>My Site</title> 
 
      
 
    </head> 
 
      
 
    <body> 
 
     
 
    <!-- Banner --> 
 
    <div id="banner" class="container-fluid"> 
 
     <nav id="navscroll" class="navbar navbar-toggleable-sm navbar-light fixed-top"> 
 
      <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation"> 
 
      <span class="navbar-toggler-icon"></span> 
 
      </button> 
 
      <a class="navbar-brand" href="#">My Site</a> 
 
      <div class="collapse navbar-collapse" id="navbarNavAltMarkup"> 
 
      <div class="navbar-nav"> 
 
       <a class="nav-item nav-link active" href="#">Me<span class="sr-only">(current)</span></a> 
 
       <a class="nav-item nav-link" href="#">What I do</a> 
 
       <a class="nav-item nav-link" href="#">Find Me</a> 
 
      </div> 
 
      </div> 
 
     </nav> 
 
    </div> 
 
    <!-- Banner --> 
 
    
 
    <!-- Divs --> 
 
    <div class="container-fluid"> 
 
     <div id="divi1" class="divi"></div> 
 
     <div id="divi2" class="divi"></div> 
 
    </div> 
 
    <!-- Divs --> 
 
     
 
    
 

 
    <!-- jQuery first, then Tether, then Bootstrap JS. --> 
 
    
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script> 
 
    </body> 
 
</html>