2013-12-20 56 views
0

我想在引導中的導航菜單上創建滾動效果,頁面頂部有一個傳送帶,導航菜單位於傳送帶的底部。所以當有人向下滾動時,在他通過導航菜單後,導航菜單將具有navbar-fixed-top類,並且觀看者將看到菜單。 我今天在這個問題上工作,但我找不到解決方案來解決這個問題。 這裏是我的HTML代碼:如何在bootstrap中滾動後創建一個滾動的navbar-fixed-top?

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Bootstrap 101 Template</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <!-- Bootstrap --> 
    <link href="css/bootstrap.min.css" rel="stylesheet"> 
    <link href="css/style.css" rel="stylesheet"> 
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,700' rel='stylesheet' type='text/css'> 
    <script src="https://code.jquery.com/jquery.js"></script> 

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --> 
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> 
    <!--[if lt IE 9]> 
     <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> 
     <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script> 
    <![endif]--> 
    </head> 
    <body class="blog"> 

     <div id="carousel-example-generic" class="carousel slide" data-ride="carousel"> 
      <!-- Indicators --> 
      <ol class="carousel-indicators"> 
      <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li> 
      <li data-target="#carousel-example-generic" data-slide-to="1"></li> 
      <li data-target="#carousel-example-generic" data-slide-to="2"></li> 
      </ol> 

      <!-- Wrapper for slides --> 
      <div class="carousel-inner"> 
      <div class="item active"> 
       <img src="images/3.jpg" alt="slide1" style="width:100%"> 
       <div class="carousel-caption"> 
       <h3>What therefore god has joined together, Let no man seperate.</h3> 
       <p>&ndash; The Bible : Mark 10:9 &ndash;</p> 
       </div> 
      </div> 
      <div class="item"> 
       <img src="images/4.jpg" alt="slide2" style="width:100%"> 
       <div class="carousel-caption"> 
       <h3>Lorem Ipsum Header Dolor Sit Amet</h3> 
       <p>&ndash; John Doe &ndash;</p> 
       </div> 
      </div> 
      <div class="item"> 
       <img src="images/5.jpg" alt="slide3" style="width:100%"> 
       <div class="carousel-caption"> 
       <h3>Header Slider 3 Caption Text</h3> 
       <p>&ndash; Holy Themeforest : Wordpress Category &ndash;</p> 
       </div> 
      </div> 
      </div> 

      <!-- Controls --> 
      <a class="left carousel-control" href="#carousel-example-generic" data-slide="prev"> 
      <span class="glyphicon glyphicon-chevron-left"></span> 
      </a> 
      <a class="right carousel-control" href="#carousel-example-generic" data-slide="next"> 
      <span class="glyphicon glyphicon-chevron-right"></span> 
      </a> 
     </div> 

<header> 

    <nav class="navbar navbar-default" role="navigation"> 
      <!-- Brand and toggle get grouped for better mobile display --> 
      <div class="navbar-header"> 
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> 
       <span class="sr-only">Toggle navigation</span> 
       <span class="icon-bar"></span> 
       <span class="icon-bar"></span> 
       <span class="icon-bar"></span> 
      </button> 
      <a class="navbar-brand" href="#">Brand</a> 
      </div> 

      <!-- Collect the nav links, forms, and other content for toggling --> 
      <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> 
      <ul class="nav navbar-nav navbar-right"> 
       <li class="active"><a href="#">Our Story</a></li> 
       <li><a href="#">Time</a></li> 
       <li><a href="#">Events</a></li> 
       <li><a href="#">Photos</a></li> 
       <li><a href="#">Gifts</a></li> 
       <li><a href="#">From the Blog</a></li> 
       <li><a href="#">RSVP</a></li> 
      </ul> 
      </div><!-- /.navbar-collapse --> 
     </nav> 

    </header> 

<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p><p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 
<p>Lorem ipsum dolor sit amet</p> 


    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 

    <script src="js/functions.js"></script> 
    <!-- Include all compiled plugins (below), or include individual files as needed --> 
    <script src="js/bootstrap.min.js"></script> 
    </body> 
</html> 

和functions.js:

// =============== Menu setup =============== // 

    // checks if menu should be fixed 
     function setup_menu(){ 
      if(!jQuery('body').hasClass('blog') && !jQuery('body').hasClass('single') && !jQuery('body').hasClass('archive') && !jQuery('body').hasClass('search')){ 
       if(!$menu) $menu = jQuery(".navbar"); 
       if (jQuery(window).scrollTop() > 200 && !$menu.hasClass("navbar-fixed-top")){ 
        $menu.addClass("navbar-fixed-top"); 
       } else if(jQuery(window).scrollTop() <= 200 && $menu.hasClass("navbar-fixed-top")) { 
        $menu.removeClass("navbar-fixed-top"); 
       } 

     } 

// =============== Menu setup END =============== // 

另外,我想創建一個滾動的部分,像這樣一個scrollspy效果:http://bit.ly/1hrjDZB我應該怎麼辦這個?

回答

2

1)的ID添加到您的導航元素

<nav id="yourID" class="navbar navbar-default" role="navigation"> 

2)以下腳本添加到您的網站。

<script> 
    function stickyNav() { 

     if (window.pageYOffset > 20) { 
      var x = document.getElementById('yourID'); 
      x.className = "navbar navbar-default navbar-fixed-top"; 
      } 
     else if (window.pageYOffset <= 20) { 
      var x = document.getElementById('navigate'); 
      x.className = "navbar navbar-default"; 
      } 
     } 
    window.onload = stickyNav; 
</script> 

3)用正文元素的onscroll事件調用該函數。

<body onscroll="stickyNav(this)"> 

4)根據需要調整window.pageYOffset的值。

*注意:這不考慮您的其他函數的if/then語句。根據您的需要調整上述腳本。

相關問題