2012-07-10 54 views
5

下面是我的代码,但我知道它没有什么不对,因为它在您将它从标签中取出时工作正常。也没有重复的Id,我认为这可能会导致问题。无论如何,如果任何人有任何想法或解决方法,将是伟大的!Bootstrap Scrollspy在选项卡内不起作用?

<div id="sampleNavbar" class="navbar navbar-static"> 
    <div class="navbar-inner"> 
     <div class="container" style="width: auto;"> 
      <a class="brand" href="#">Project Name</a> 
      <ul class="nav"> 
       <li><a href="#PA">PA</a></li> 
       <li><a href="#VT">VT</a></li> 
       <li class="dropdown"> 
        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a> 
        <ul class="dropdown-menu"> 
         <li><a href="#uno">uno</a></li> 
         <li><a href="#dos">dos</a></li> 
         <li class="divider"></li> 
         <li><a href="#tres">tres</a></li> 
        </ul> 
       </li> 
      </ul> 
     </div> 
    </div> 
</div> 
<div id="scrollspy-example" data-spy="scroll" data-target="#sampleNavbar" data-offset="300" style="height: 200px; overflow-y: scroll;"> 
    <h4 id="PA">PA</h4> 
    <p>Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui. Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat. 
    </p> 
    <h4 id="VT">VT</h4> 
    <p>Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard. Freegan beard aliqua cupidatat mcsweeney's vero. Cupidatat four loko nisi, ea helvetica nulla carles. Tattooed cosby sweater food truck, mcsweeney's quis non freegan vinyl. Lo-fi wes anderson +1 sartorial. Carles non aesthetic exercitation quis gentrify. Brooklyn adipisicing craft beer vice keytar deserunt. 
    </p> 
    <h4 id="uno">uno</h4> 
    <p>Occaecat commodo aliqua delectus. Fap craft beer deserunt skateboard ea. Lomo bicycle rights adipisicing banh mi, velit ea sunt next level locavore single-origin coffee in magna veniam. High life id vinyl, echo park consequat quis aliquip banh mi pitchfork. Vero VHS est adipisicing. Consectetur nisi DIY minim messenger bag. Cred ex in, sustainable delectus consectetur fanny pack iphone. 
    </p> 
    <h4 id="dos">dos</h4> 
    <p>In incididunt echo park, officia deserunt mcsweeney's proident master cleanse thundercats sapiente veniam. Excepteur VHS elit, proident shoreditch +1 biodiesel laborum craft beer. Single-origin coffee wayfarers irure four loko, cupidatat terry richardson master cleanse. Assumenda you probably haven't heard of them art party fanny pack, tattooed nulla cardigan tempor ad. Proident wolf nesciunt sartorial keffiyeh eu banh mi sustainable. Elit wolf voluptate, lo-fi ea portland before they sold out four loko. Locavore enim nostrud mlkshk brooklyn nesciunt. 
    </p> 
    <h4 id="tres">tres</h4> 
    <p>Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui. Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat. 
    </p> 
    <p>Keytar twee blog, culpa messenger bag marfa whatever delectus food truck. Sapiente synth id assumenda. Locavore sed helvetica cliche irony, thundercats you probably haven't heard of them consequat hoodie gluten-free lo-fi fap aliquip. Labore elit placeat before they sold out, terry richardson proident brunch nesciunt quis cosby sweater pariatur keffiyeh ut helvetica artisan. Cardigan craft beer seitan readymade velit. VHS chambray laboris tempor veniam. Anim mollit minim commodo ullamco thundercats. 
    </p> 
</div>   

回答

10

这不是Bootstrap中的错误。发生的事情是,当你的页面加载时,带有滚动元素的标签是隐藏的。因此,当滚动插件尝试在您的滚动div中查找每个<h4 id="foo">$.position()时,它们会调用返回{top: 0, left: 0}。这就是为什么当你滚动时你会看到闪烁的原因;该插件认为您的所有滚动目标位于相同的位置。

如果你看看documentation for the scrollspy plugin,你会发现它提到,如果你要添加或删除DOM中的元素,你需要调用.scrollspy('refresh')函数,这样插件可以重新评估各个滚动目标的位置。

也就是说,你需要做的就是等待带有滚动内容的标签被加载,然后调用刷新函数,就像这样。请注意,我所有单独的点击处理程序也合并成一个处理程序

$(function(){ 
    $('.nav-tabs li a').click(function(e) { 
     e.preventDefault(); 
     $(this).tab('show'); 

     // If we are showing the scrollspy tab, let the 
     // plugin refresh itself so it can function properly 
     if(this.id === 'scrotab') { 
      $(this).on('shown',function(){ 
       $('#scrollspy-example').scrollspy('refresh'); 
      }); 
     } 
    }); 
}); 

此外,而不是使用您的<div id="scrollspy-example">元素上data-offset="300"属性,你需要的元素的position设置为relative与CSS:

#scrollspy-example { 
    position: relative; 
} 

这里有一个演示的jsfiddle的作品:

http://jsfiddle.net/GyMYE/3/

+0

非常感谢。这很棒。它很有趣,但因为事实证明,我认为你必须有淡淡的课程才能工作。谢谢! – dezman 2012-07-11 18:23:08

1

添加:

$('#scrollspy-example').scrollspy('process'); 

后:

$('#scrollspy-example').scrollspy('refresh'); 

可以确保你加载这个特殊标签的正确位置被更新。否则,错误的li元素将通过滚动显示,直到您移动/开始滚动,导致刷新。 '流程'行在制表符加载时执行此操作。

相关问题