1

我正在构建一个长页面,并希望导航栏的选项卡在有人在页面上滚动时变得活跃。由于我使用的是 Bootstrap 3,我认为 scrollspy 会起作用。但是,我无法让它工作。下面是我的代码。我将不胜感激任何帮助 :)

导航栏的代码

<header class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
  <div class="navbar-header">
    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
      <span class="sr-only">Toggle navigation</span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </button>
    <%= link_to image_tag("Logomark_Red.png", alt: "") + "  Title", root_url, class: "navbar-brand" %>
  </div>
  <div id="active-nav" class="collapse navbar-collapse">
    <ul class="nav navbar-nav pull-right">
      <li><%= link_to "About", root_url(:anchor => "about") %></li>
      <li><%= link_to "Projects", root_url(:anchor => "past-projects") %></li>
      <li><%= link_to "Team", root_url(:anchor => "team") %></li>
      <li><%= link_to "Contact", root_url(:anchor => "contact") %></li>
    </ul>
  </div><!--/.nav-collapse -->
</div>

application.html.erb 文件的片段

<!DOCTYPE html>
 <html lang="en">
  <head>
    ...head stuff....
  </head>
 <%= render 'layouts/nav' %>
 <body data-target="#active-nav" data-spy="scroll" data-offset="0">

身体的 CSS

body{
  position: relative;
  margin-top: 50px;
}

应用程序.js 文件

//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require turbolinks
//= require colorbox-rails
//= require_tree .

HTML(片段只是为了显示部分有 id 并且导航链接到这些链接)

<section id="about" class="row">
    <div class="container">
        <div class="row">
            <h1>title</h1>
            <div class="col-md-6">
                <p>content.</p>
            </div>
            <div class="col-md-6">
                <%= image_tag("lego.jpg", width: "100%", alt: "") %>
            </div>
            <%= link_to "About Magnum Group", about_url, class: "btn btn-primary" %>
        </div>
    </div>
</section>
4

1 回答 1

0

我无法让它工作,但我创建了一个有效的 JavaScript 代码。我希望它对将来的人有所帮助。

    /* active on scroll */
marginTop = 50;
about = 849 - marginTop;
projects = 1388 - marginTop;
team = 1701 - marginTop;
contact = 2249 - marginTop;

$(window).scroll(function(){
    //Retrieve scroll distance from top
    var top = $(window).scrollTop();
    //Check if scrolled past div
    if(top >= about && top <= projects) {
        console.log('you are on about');
        $( ".navbar-nav li" ).removeClass( "active" );
        $( ".navbar-nav li" ).first().addClass( "active" );
    } else if (top >= projects && top <= team) {
        console.log('you are on projects');
        $( ".navbar-nav li" ).removeClass( "active" );
        $( ".navbar-nav li:nth-child(2)" ).addClass( "active" );
    } else if (top >= team && top <= contact) {
        console.log('you are on team');
        $( ".navbar-nav li" ).removeClass( "active" );
        $( ".navbar-nav li:nth-child(3)" ).addClass( "active" );
    } else if ( top >= contact) {
        console.log('you are on contact');
        $( ".navbar-nav li" ).removeClass( "active" );
        $( ".navbar-nav li" ).last().addClass( "active" );
    }
    else {
        console.log('tabs should not be active');
        $( ".navbar-nav li" ).removeClass( "active" );
    }        
}); 
于 2014-10-27T00:11:51.537 回答