2016-07-24 56 views
1

當我的導航欄被摺疊時,滾動是正常的,當滾動通過頁眉/ jumbotron時,導航欄固定到頁面的頂部。但是,當導航欄展開時(顯示「一」,「二」和「三」),頁面將跳過一些內容並通過導航欄覆蓋它。我怎樣才能使頁面滾動/行爲「正常」,並允許擴展的導航欄修復頁面的頂部而不跳過下面的內容?如何在導航菜單展開時將導航條滾動並修復到頁面頂部?

<!DOCTYPE html> 
<html> 

<head> 
    <title>Example</title> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
    <link href="https://fonts.googleapis.com/css?family=Cabin" rel="stylesheet"> 
    <link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet"> 
    <script type="text/javascript"> 
    </script> 
    <style> 
     .affix { 
      top: 0; 
      width: 100%; 
     } 
     .affix + .container-fluid { 
      padding-top: 70px; 
     } 
     nav { 
      z-index: 9; 
     } 
     .emoji p:hover { 
      text-decoration: none; 
     } 
    </style> 
</head> 

<body> 
    <div class="container-fluid" style="background-color:#f64e09; color:#fff; height:200px;"> 
     <div class="container text-center" style="color: white;"> 
      <div> 
       <h1 style="font-family: 'Lobster', cursive;">Website</h1> </div> 
      <h1 style="font-size: 30px; font-family: 'Cabin', sans-serif;"> 

    </h1> </div> 
    </div> 
    <nav class="navbar navbar-inverse" data-spy="affix" data-offset-top="197"> 
     <div class="container-fluid"> 
      <div class="navbar-static-top"> 
       <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a id="navId" class="navbar-brand" style="color:white;" href="#">Navbar Title</a> </div> 
      <div class="collapse navbar-collapse" id="myNavbar"> 
       <ul class="nav navbar-nav"> 
        <li><a href="#">One</a> </li> 
        <li class="active"><a href="#">Two</a> </li> 
        <li><a href="#">Three</a> </li> 
       </ul> 
      </div> 
     </div> 
    </nav> 
    <!-- Start containter for everything south of navbar --> 
    <div class="container-fluid"> 
     <div class="container" style="padding-top: 20px;"> 
      <h1>Content so the page scrolls</h1> 
      <h1>Content so the page scrolls</h1> 
      <h1>Content so the page scrolls</h1> 
      <h1>Content so the page scrolls</h1> 
      <h1>Content so the page scrolls</h1> 
      <h1>Content so the page scrolls</h1> 
      <h1>Content so the page scrolls</h1> 
      <h1>Content so the page scrolls</h1> 
      <h1>Content so the page scrolls</h1> </div> 
    </div> 
    <div class="panel panel-default" style="font-family: 'Cabin', sans- serif;"> 
     <div class="panel-footer"> 
      <center> 
       <p>This is the footer</p> 
      </center> 
     </div> 
    </div> 
</body> 

</html> 

回答

0

正如你可能知道,爲什麼你的內容是不是「跳」,而導航欄被倒塌的原因是這種風格:

.affix + .container-fluid { 
    padding-top: 70px; 
} 

但是當你擴展導航欄,填充保持不變,但navbar本身變得更高。所以,我們必須以某種方式阻止這些情況。

我們可以添加一些javascript到一個額外的類添加到<nav>元素:

<script type="text/javascript"> 
    $(document).ready(function() { 
     var navBar = $('#myNavbar'); 
     $('.navbar-toggle').click(function(e) { 
      if (!navBar.is(':visible')) { 
       // #myNavbar is about to be shown, add extra class to parent <nav> 
       navBar.parents('nav').addClass('affix-visible-navbar'); 
      } else { 
       // #myNavbar is about to be hidden, remove extra class from parent <nav> 
       navBar.parents('nav').removeClass('affix-visible-navbar'); 
      } 
     }); 
    }); 
</script> 

,並確定新的CSS類:

.affix.affix-visible-navbar + .container-fluid { 
    padding-top: 155px !important; 
} 

因此,完整的工作解決方案將是這樣的:

<!DOCTYPE html> 
<html> 

<head> 
    <title>Example</title> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
    <link href="https://fonts.googleapis.com/css?family=Cabin" rel="stylesheet"> 
    <link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet"> 
    <script type="text/javascript"> 
    </script> 
    <style> 
     .affix { 
      top: 0; 
      width: 100%; 
     } 
     .affix + .container-fluid { 
      padding-top: 70px; 
     } 
     .affix.affix-visible-navbar + .container-fluid { 
      padding-top: 155px !important; 
     } 
     nav { 
      z-index: 9; 
     } 
     .emoji p:hover { 
      text-decoration: none; 
     } 
    </style> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      var navBar = $('#myNavbar'); 
      $('.navbar-toggle').click(function(e) { 
       if (!navBar.is(':visible')) { 
        // #myNavbar is about to be shown, add extra class to parent <nav> 
        navBar.parents('nav').addClass('affix-visible-navbar'); 
       } else { 
        // #myNavbar is about to be hidden, remove extra class from parent <nav> 
        navBar.parents('nav').removeClass('affix-visible-navbar'); 
       } 
      }); 
     }); 
    </script> 
</head> 

<body> 
    <div class="container-fluid" style="background-color:#f64e09; color:#fff; height:200px;"> 
     <div class="container text-center" style="color: white;"> 
      <div> 
       <h1 style="font-family: 'Lobster', cursive;">Website</h1> </div> 
      <h1 style="font-size: 30px; font-family: 'Cabin', sans-serif;"> 

    </h1> </div> 
    </div> 
    <nav class="navbar navbar-inverse" data-spy="affix" data-offset-top="197"> 
     <div class="container-fluid"> 
      <div class="navbar-static-top"> 
       <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a id="navId" class="navbar-brand" style="color:white;" href="#">Navbar Title</a> </div> 
      <div class="collapse navbar-collapse" id="myNavbar"> 
       <ul class="nav navbar-nav"> 
        <li><a href="#">One</a> </li> 
        <li class="active"><a href="#">Two</a> </li> 
        <li><a href="#">Three</a> </li> 
       </ul> 
      </div> 
     </div> 
    </nav> 
    <!-- Start containter for everything south of navbar --> 
    <div class="container-fluid"> 
     <div class="container" style="padding-top: 20px;"> 
      <h1>Content so the page scrolls</h1> 
      <h1>Content so the page scrolls</h1> 
      <h1>Content so the page scrolls</h1> 
      <h1>Content so the page scrolls</h1> 
      <h1>Content so the page scrolls</h1> 
      <h1>Content so the page scrolls</h1> 
      <h1>Content so the page scrolls</h1> 
      <h1>Content so the page scrolls</h1> 
      <h1>Content so the page scrolls</h1> </div> 
    </div> 
    <div class="panel panel-default" style="font-family: 'Cabin', sans- serif;"> 
     <div class="panel-footer"> 
      <center> 
       <p>This is the footer</p> 
      </center> 
     </div> 
    </div> 
</body> 

</html> 

當然,如果你改變#myNavbar元素(例如添加/刪除項目),你必須調整填充.affix.affix-visible-navbar + .container-fluid {}

+0

完美的作品,謝謝。 – user3376899

+0

@ user3376899,比upvote pls呢=) – spirit

+0

我做了,但沒有顯示,因爲我太新的用戶。 – user3376899

相關問題