我曾经问过关于Bootstrap ScrollSpy的以前的问题,这是我试图让这个工作的最后一个尝试.
我想要实现的是在该部分滚动时更改导航栏的颜色.我已经尝试了很多方法,甚至有一个Jsfiddle工作,但即使在我的应用程序设置不起作用
这就是我所拥有的
JS
$('#spyOnThis').scrollspy();
身体
<body data-spy="scroll" data-target="#spyOnThis">
视图
<div class="container"> <div class="row show-grid clear-both"> <div id="left-sidebar" class="span3 sidebar"> <div class="side-nav sidebar-block"> <h3 class="resultTitle fontSize13">Release Dates</h2> <ul class="date"> <% @response.each_pair do |date,movie| %> <li><i class="icon-chevron-right"></i><%= link_to date_format(date),"#d_#{date}",:id=> '#d_#{date}' %></li> <% end %> </ul> </div> </div> <div class="span9"> <div id="dateNav"> <ul class="nav"> <li><% @response.each_pair do |date,movie| %><%= link_to date_format(date),"#d_#{date}" %><% end %></li> </ul> </div> <div id="spyOnThis"> <% @response.each_pair do |date,movie| %> <h3 class="resultTitle fontSize13" id="d_<%= date %>">Available on <%= date_format(date) %></h3> <% movie.each do |m| %> <div class="thumbnail clearfix"> <img class="pull-left" src=<% if m.image_link %> <%= m.image_link %> <% else %> "/assets/noimage.jpg" <% end %>> <div class="caption pull-right"> <%= link_to m.name,m.title_id,:class => 'resultTitle fontSize11' %> <p class="bio"><%= m.bio %></p> <p class="resultTitle">Cast</p> <p class="bio"><%= m.cast.join(",") unless m.cast.empty? %></p> <%= link_to "Remind me",reminders_path(:title_id => m.title_id),:method => :post,:class => 'links button' %> </div> </div> <% end %> <% end %> </div> </div><!--span9--> </div><!--Row--> </div><!--/container-->
CSS
#dateNav{ position: fixed; top: 0; left: 20px; width: 100%; background:none; text-align:center; } #spyOnThis { height:100%; overflow:auto; } .nav > li > a { display:block; } .nav > li.active > a { display:block; color: red; text-decoration: underline; }
我知道100%的高度可能会导致一个问题,但是我的滚动条的高度将取决于内容,它可以改变.
我现在在拉我的头发,我已经看到,这个螺旋线是相当bug,但肯定它必须工作,他们的网站演示
编辑
$('#dateNav').scrollspy(); <body data-spy="scroll" data-target="#dateNav">
HTML
ul class="nav"> <li> <a href="#d_2013-01-09">9th Jan 2013</a> <a href="#d_2013-01-11">11th Jan 2013</a> <a href="#d_2013-01-18">18th Jan 2013</a> <a href="#d_2013-01-23">23rd Jan 2013</a> <a href="#d_2013-01-25">25th Jan 2013</a> <a href="#d_2013-01-30">30th Jan 2013</a> </li>
<div id="spyOnThis"> <h3 id="d_2013-01-09" class="resultTitle fontSize13">Available on 9th Jan 2013</h3> <div class="thumbnail clearfix"> <h3 id="d_2013-01-11" class="resultTitle fontSize13">Available on 11th Jan 2013</h3> <div class="thumbnail clearfix"> <div class="thumbnail clearfix"> <div class="thumbnail clearfix"> <div class="thumbnail clearfix"> <h3 id="d_2013-01-18" class="resultTitle fontSize13">Available on 18th Jan 2013</h3> <div class="thumbnail clearfix"> <div class="thumbnail clearfix"> <h3 id="d_2013-01-23" class="resultTitle fontSize13">Available on 23rd Jan 2013</h3> <div class="thumbnail clearfix"> <h3 id="d_2013-01-25" class="resultTitle fontSize13">Available on 25th Jan 2013</h3> </div>
这将所有的链接变成红色,所以它们现在都是活跃的?
我的身体也是100%的粘性页脚,不知道是否有所作为
帮助真的很感激
解决方法
您需要从主体移动data-spy和data-target属性:
<body data-spy="scroll" data-target="#dateNav">
并将它们移动到div“spyOnThis”:
<div id="spyOnThis">
应该:
<div id="spyOnThis" data-spy="scroll" data-target="#dateNav">
“要轻松地将scrollspy行为添加到您的顶栏导航中,只需将data-spy =”scroll“添加到要监视的元素(通常这将是body)和data-target =”.navbar“来选择哪个导航要使用,您需要使用scrollspy与.nav组件.“
基于您的HTML,您还需要修复几件事情:
<ul class="nav"> <li> <a href="#d_2013-01-09">9th Jan 2013</a> <a href="#d_2013-01-11">11th Jan 2013</a> <a href="#d_2013-01-18">18th Jan 2013</a> <a href="#d_2013-01-23">23rd Jan 2013</a> <a href="#d_2013-01-25">25th Jan 2013</a> <a href="#d_2013-01-30">30th Jan 2013</a> </li> </ul>
应该:
<ul class="nav dateNav"> <li><a href="#d_2013-01-09">9th Jan 2013</a></li> <li><a href="#d_2013-01-11">11th Jan 2013</a></li> <li><a href="#d_2013-01-18">18th Jan 2013</a></li> <li><a href="#d_2013-01-23">23rd Jan 2013</a></li> <li><a href="#d_2013-01-25">25th Jan 2013</a></li> <li><a href="#d_2013-01-30">30th Jan 2013</a></li> </ul>
您的div也没有正确关闭:
<div id="spyOnThis"> <h3 id="d_2013-01-09" class="resultTitle fontSize13">Available on 9th Jan 2013</h3> <div class="thumbnail clearfix"> <h3 id="d_2013-01-11" class="resultTitle fontSize13">Available on 11th Jan 2013</h3> <div class="thumbnail clearfix"> <div class="thumbnail clearfix"> <div class="thumbnail clearfix"> <div class="thumbnail clearfix"> <h3 id="d_2013-01-18" class="resultTitle fontSize13">Available on 18th Jan 2013</h3> <div class="thumbnail clearfix"> <div class="thumbnail clearfix"> <h3 id="d_2013-01-23" class="resultTitle fontSize13">Available on 23rd Jan 2013</h3> <div class="thumbnail clearfix"> <h3 id="d_2013-01-25" class="resultTitle fontSize13">Available on 25th Jan 2013</h3> </div>
应该是(只显示几个):
<div class="thumbnail clearfix"> <h3 id="d_2013-01-23" class="resultTitle fontSize13">Available on 23rd Jan 2013</h3> </div> <div class="thumbnail clearfix"> <h3 id="d_2013-01-25" class="resultTitle fontSize13">Available on 25th Jan 2013</h3> </div>