我正在构建一个长页面,并希望导航栏的选项卡在有人在页面上滚动时变得活跃。由于我使用的是 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>