Bootstrap 3折叠scrollTop

时间:2013-10-08 01:35:07

标签: jquery twitter-bootstrap accordion twitter-bootstrap-3 scrolltop

我在使用Bootstrap 3的手风琴时遇到了一些麻烦。

我想要做的是为accordion-toggle添加一个类,然后滚动/跳转到新添加的类。添加/删除该类工作正常,我想我只会在同一个块中添加一个scrollTop ...但没有这样的运气。我错过了什么?

以下是它的设置方式:

指向面板的链接:

<ul>
    <li><a data-toggle="collapse" data-parent="#accordion" href="#panel1">Panel1</a>
    </li>
    <li><a data-toggle="collapse" data-parent="#accordion" href="#panel2">Panel2</a>
    </li>
    <li>
        <a data-toggle="collapse" data-parent="#accordion" href="#panel3" ">Panel3</a></li>
</ul>

面板:

<div class="panel-group" id="accordion">
    <div class="panel panel-default">
        <div class="panel-heading">
             <h4 class="panel-title">
    <a class="accordion-toggle aWeight active" data-toggle="collapse" data-parent="#accordion" href="#panel1">
    Panel 1
    </a>
    </h4>

        </div>
        <div id="panel1" class="panel-collapse collapse in">
            <div class="panel-body">Anim pariatur...</div>
        </div>
    </div>
    <div class="panel panel-default">
        <div class="panel-heading">
             <h4 class="panel-title">
    <a class="accordion-toggle aFitness" data-toggle="collapse" data-parent="#accordion" href="#panel2">
    Panel 2
    </a>
    </h4>

        </div>
        <div id="panel2" class="panel-collapse collapse">
            <div class="panel-body">Anim pariatur...</div>
        </div>
    </div>
    <div class="panel panel-default">
        <div class="panel-heading">
             <h4 class="panel-title">
    <a class="accordion-toggle aFitness" data-toggle="collapse" data-parent="#accordion" href="#panel3">
    Panel 3
    </a>
    </h4>

        </div>
        <div id="panel3" class="panel-collapse collapse">
            <div class="panel-body">Anim pariatur...</div>
        </div>
    </div>
</div>

最后我正在使用此脚本来切换活动类:

$(function () {
    $('.accordion').on('show', function (e) {
        $(e.target).prev('.accordion-heading').find('.accordion-toggle').addClass('active');
    });
    $('.accordion').on('hide', function (e) {
        $(this).find('.accordion-toggle').not($(e.target)).removeClass('active');
    });
});

2 个答案:

答案 0 :(得分:4)

在使用Bootstrap 2事件时,请参阅Bootstrap 3,请参阅:http://getbootstrap.com/javascript/#collapse。 崩溃插件已经添加了一个类(.in)。

shown.bs.collapse触发你的&#34;新&#34;小组有&#34; .in&#34;类和当hide.bs.collapse触发&#34; old&#34;小组仍然有&#34; .in&#34;类。

尝试:

$(function () {
    $('#accordion').on('shown.bs.collapse', function (e) {
        var offset = $('.panel.panel-default > .panel-collapse.in').offset();
            if(offset)$('html,body').scrollTop(offset.top);
    });
});

`

答案 1 :(得分:1)

$(".accordion-body").on("shown", function () {
var id = $(this).attr('id');
$('html, body').animate({scrollTop: $('#'+id).offset().top + -50}, 1000);
});
});