Bootstrap scrollspy在标签内不工作? [英] Bootstrap scrollspy doesn't work inside tabs?

查看:182
本文介绍了Bootstrap scrollspy在标签内不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码,但我知道它没有什么错,因为它工作正常,当你把它从选项卡。也没有重复的ID,我认为可能是导致的问题。无论如何,如果任何人有任何想法或解决方法,将是伟大的!

 < div id =sampleNavbarclass =navbar navbar-static> 
< div class =navbar-inner>
< div class =containerstyle =width:auto;>
< a class =brandhref =#>专案名称< / 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-toggledata-toggle =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-exampledata-spy =scrolldata-target =#sampleNavbardata-offset =300style =height:200px; overflow-y:scroll; >
< h4 id =PA> PA< / h4>
< p>广告绑腿keytar,早午餐id艺术党dolor labore。 Pitchfork yr enim lo-fi before they sold out qui。 Tumblr农场对表自行车权利。 Anim keffiyeh carles羊毛衫。 Velit seitan mcsweeney的照片亭3狼月亮irure。 Cosby毛衣lomo牛仔裤短裤,威廉斯堡连帽衫最小化你可能没有听说过他们和开衫信托基金生物柴油wes安德森审美。 Nihil纹身accusamus,可口的讽刺生物柴油keffiyeh工匠乌拉姆科。
< / p>
< h4 id =VT> VT< / h4>
< p> Veniam marfa胡子滑板,adipisicing fugiat velit干草叉胡子。 Freegan胡子aliqua cupidatat mcsweeney的vero。丘比特四个loko nisi,ea helvetica nulla carles。纹身cosby毛衣食物卡车,mcsweeney的quis非freegan乙烯基。 Lo-fi wes安德森+1标准。 Carles非美学运动quis gentrify。布鲁克林adipisicing工艺啤酒副keytar deserunt。
< / p>
< h4 id =uno> uno< / h4>
< p> Occaecat commodo aliqua delectus。 Fap工艺啤酒deserunt滑板ea。 Lomo顾客服务与广告有可能联系我们在线交谈与我们联系乒乓键航海全球性Nasco艺术&高生命乙烯基,回声公园后果quis aliquip banh mi干草叉。 Vero VHS预测。 Consectetur nisi DIY最小信使包。 Cred过去,可持续发展consetetur屁股包iphone。
< / p>
< h4 id =dos> dos< / h4>
< p>在不喜欢的回声公园,官僚deserunt mcsweeney的proident主清洗thundercats sapiente veniam。 Excepteur VHS精华,proident shoreditch +1生物柴油工艺啤酒。单原产咖啡的旅行者irure四萝卜,丘比特特里理查德森主清洗。 Assumenda你可能还没有听说过他们的艺术党屁股包,纹身nulla开衫时间广告。 Proident wolf nesciunt sartorial keffiyeh eu banh mi可持续发展。 Elit狼狼吞虎咽,洛杉矶波特兰之前,他们卖掉了四个loko。布鲁克林nesciunt。
< / p>
< h4 id =tres> tres< / h4>
< p>广告绑腿keytar,早午餐id艺术党dolor labour。 Pitchfork yr enim lo-fi before they sold out qui。 Tumblr农场对表自行车权利。 Anim keffiyeh carles羊毛衫。 Velit seitan mcsweeney的照片亭3狼月亮irure。 Cosby毛衣lomo牛仔裤短裤,威廉斯堡帽衫迷你你可能没有听说过他们和开衫信托基金生物柴油wes安德森审美。 Nihil纹身accusamus,可口的讽刺生物柴油keffiyeh工匠乌拉姆科。
< / p>
< p> Keytar twee blog,culpa messenger bag marfa whatever delectus food truck。 Sapiente合成器假设。 Locavore sed helvetica cliche讽刺,thundercats你可能没有听说过他们的连帽无麸免费lo-fi fap aliquip。 Labore elit placeat之前他们卖完了,terry richardson proident brunch nesciunt quis cosby毛衣pariatur keffiyeh ut helvetica工匠。羊毛衫工艺啤酒seitan准备velit。 VHS chambray laboris tempor veniam。 Anim mollit minim commodo ullamco thundercats。
< / p>
< / div>


解决方案

这不是Bootstrap中的错误。发生了什么事情,当你的页面加载,带有scrollspy元素的选项卡被隐藏。因此,当scrollspy插件尝试找到每个< h4 id =foo> $。position() c $ c>在你的滚动div内,他们调用return {top:0,left:0} 。这就是为什么你看到闪烁,你滚动;



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



这就是说,你需要做的是等待直到带有scrollspy内容的选项卡加载,然后调用刷新函数,如此。注意,我还将所有单独的点击处理程序合并为一个处理程序

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

//如果我们显示scrollspy选项卡,让
//插件刷新本身,以便它可以正常运行
if(this.id ==='scrotab'){
$ (this).on('shown',function(){
$('#scrollspy-example')。scrollspy('refresh');
});
}
});
});此外,与使用 data-offset =300相比,


$ b < div id =scrollspy-example> 元素的code>属性,您需要设置 position 使用CSS的相对

  scrollspy-example {
position:relative;
}

这里有一个jsFiddle演示:



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

Below is my code, but I know there is nothing wrong with it, because it works fine when you take it out of the tab. There are also no duplicate Id's, which I thought might be causing the problem. Anyway, if anyone has any ideas or workarounds that would be great!

<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>        

解决方案

This isn't a bug in Bootstrap. What's happening is that, when your page loads, the tab with the scrollspy elements is hidden. As such, when the scrollspy plugin attempts to find the $.position() of each <h4 id="foo"> within your scrolling div, they call return {top: 0, left: 0}. That's why you see the flickering as you scroll; the plugin thinks all your scroll targets are in the same position.

If you look at the documentation for the scrollspy plugin, you'll see that it mentions that if you are adding or removing elements from the DOM, you need to call the .scrollspy('refresh') function so that the plugin can reassess the positions of the various scroll targets.

That said, what you need to do is wait until the tab with the scrollspy content is loaded, and then call the refresh function, like so. Note that I've also combined all the separate click handlers into one handler

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

Also, rather than using a data-offset="300" attribute on your <div id="scrollspy-example"> element, you need to set the position of the element to relative with CSS:

#scrollspy-example {
    position: relative;
}

Here's a jsFiddle Demo that works:

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

这篇关于Bootstrap scrollspy在标签内不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆