Bootstrap选项卡:打开选项卡并滚动

时间:2015-12-09 11:43:12

标签: jquery twitter-bootstrap

我想链接消息标签,一旦用户点击该链接,就应该打开标签并滚动。

滚动功能正常,但如何将标签设置为活动状态?

$('a.js-link-scroll').on('shown.bs.tab', function (e) {
    var that = this;
    $('html, body').animate({
        scrollTop: $( $(that).attr('href') ).offset().top
    }, 500);
});

jsFiddle

1 个答案:

答案 0 :(得分:1)

$('a.js-link-scroll').on('shown.bs.tab', function (e) {
   $('#myTabs .active').removeClass('active');//remove all active classes
   $('a[href="'+$(this).attr('href')+'"]').parent().addClass('active');//add the active class to the #message tab
    var that = this;
    $('html, body').animate({
        scrollTop: $( $(that).attr('href') ).offset().top
    }, 500);
});

http://jsfiddle.net/jvhofaxz/