Bootstrap选项卡 - 下一个和上一个

时间:2016-06-17 07:53:37

标签: javascript jquery twitter-bootstrap

我的Bootstrap标签有问题。

我在下一个按钮上定义了scrollTop功能。滚动工作正常但标签从1跳到3.

我的JS代码:

<script>

$('.btnNext').click(function(){

     $('html, body').animate({
        scrollTop: jQuery(".expect").offset().top
    }, 800,

    function () {
          $('.nav-tabs > .active').next('li').find('a').trigger('click');
    }
);

});

  $('.btnPrevious').click(function(){
  $('.nav-tabs > .active').prev('li').find('a').trigger('click');
});

</script>

HTML代码

<div class="step-tabs">
    <ul class="nav nav-tabs" role="tablist">
        <li class="nav-item active" style="padding:3px;">
            <a class="nav-link" data-toggle="tab" href="#tab1" role="tab">STAP 01</a>
        </li>
        <li class="nav-item" style="padding:3px;">
            <a class="nav-link" data-toggle="tab" href="#tab2" role="tab">STAP 02</a>
        </li>
        <li class="nav-item" style="padding:3px;">
            <a class="nav-link" data-toggle="tab" href="#tab3" role="tab">STAP 03</a>
        </li>
        <li class="nav-item" style="padding:3px;">
            <a class="nav-link" data-toggle="tab" href="#tab5" role="tab">STAP 03</a>
        </li>
        <li class="nav-item" style="padding:3px;">
            <a class="nav-link" data-toggle="tab" href="#tab5" role="tab">STAP 03</a>
        </li>
        <li class="nav-item" style="padding:3px;">
            <a class="nav-link" data-toggle="tab" href="#tab6" role="tab">STAP 03</a>
        </li>
    </ul>
</div>
<div class="step-panes">
    <!-- Tab panes -->
    <div class="tab-content">
        <div class="tab-pane active" id="tab1" role="tabpanel">
            <p>CONTENT TABS 01<br><br><br><br><br><br><br><br><br><br>XXXXXX<br><br><br><br><br><br><br>XXXXXXX<br><br><br><br><br><br><br></p><a class="btn btn-primary btnNext">Next</a>
        </div>
        <div class="tab-pane" id="tab2" role="tabpanel">
            <p>CONTENT TABS 01<br><br><br><br><br><br><br><br><br><br>XXXXXX<br><br><br><br><br><br><br></p><a class="btn btn-primary btnNext">Next</a> <a class="btn btn-primary btnPrevious">Previous</a>
        </div>
        <div class="tab-pane" id="tab3" role="tabpanel">
            <p>CONTENT TABS 03</p><a class="btn btn-primary btnNext">Next</a> <a class="btn btn-primary btnPrevious">Previous</a>
        </div>
        <div class="tab-pane" id="tab4" role="tabpanel">
            <p>CONTENT TABS 03</p><a class="btn btn-primary btnNext">Next</a> <a class="btn btn-primary btnPrevious">Previous</a>
        </div>
        <div class="tab-pane" id="tab5" role="tabpanel">
            <p>CONTENT TABS 03</p><a class="btn btn-primary btnNext">Next</a> <a class="btn btn-primary btnPrevious">Previous</a>
        </div>
        <div class="tab-pane" id="tab6" role="tabpanel">
            <p>CONTENT TABS 03</p><a class="btn btn-primary btnNext">Next</a> <a class="btn btn-primary btnPrevious">Previous</a>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:3)

此特定代码导致了问题(采用Skelly的方法)

 $('html, body').animate({
        scrollTop: jQuery(".expect").offset().top
    }, 800,

我无法找到上述代码导致跳过跳转的原因。但我确实有解决方案可以解决这个问题。

<强> JS

$(document).ready(function() {
    $('.step-tabs').on('click', function() {
        $('html,body').animate({
            scrollTop: $(this).offset().top
        }, 800);
    });
});


$('.btnNext').click(function() {
    $('.nav-tabs > .active').next('li').find('a').trigger('click');
});


$('.btnPrevious').click(function() {
    $('.nav-tabs > .active').prev('li').find('a').trigger('click');
});
P.S - 如果有人能解释原因为什么他的代码不起作用,我就能够和平地死去。

WORKING BOOTPLY