Bootstrap scrollspy在标签内不起作用?

下面是我的代码,但我知道它没有任何问题,因为当它从标签中取出时它可以正常工作。 也没有重复的Id,我认为可能导致问题。 无论如何,如果有人有任何想法或变通办法那将是伟大的!

 

PA

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.

VT

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.

uno

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.

dos

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.

tres

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.

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.

这不是Bootstrap中的错误。 发生的事情是,当您的页面加载时,隐藏了包含scrollspy元素的选项卡。 因此,当scrollspy插件尝试在滚动div中找到每个

$.position()时,它们会调用return {top: 0, left: 0} 。 这就是你滚动时看到闪烁的原因; 该插件认为所有滚动目标都处于相同位置。

如果您查看scrollspy插件的文档 ,您会看到它提到如果您要从DOM添加或删除元素,则需要调用.scrollspy('refresh')函数,以便插件可以重新评估各种滚动目标的位置。

也就是说,您需要做的是等到加载了scrollspy内容的选项卡,然后调用刷新函数,就像这样。 请注意,我还将所有单独的单击处理程序组合到一个处理程序中

 $(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'); }); } }); }); 

另外,不要在

元素上使用data-offset="300"属性,而是需要使用CSS将元素的position设置为relative

 #scrollspy-example { position: relative; } 

这是一个有效的jsFiddle演示:

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

添加:

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

之后:

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

确保在加载此特定选项卡时更新了正确的位置。 否则,错误的li元素将由scrollspy突出显示,直到您移动/开始滚动,从而导致刷新。 ‘process’行在标签加载上执行此操作。