简体   繁体   English

Bootstrap手风琴滚动到活动面板标题的顶部

[英]Bootstrap accordion scroll to top of active panel heading

I'm looking for a code that scrolls up to the top of the currently active panel heading of my bootstrap 3 html/css accordion. 我正在寻找一个代码,该代码可以滚动到我的bootstrap 3 html / css手风琴的当前活动面板标题的顶部。 The closest solution I've found on stackoverflow is the snippet of js below. 我在stackoverflow上找到的最接近的解决方案是以下js的片段。

This snippet works fairly well, but when a panel heading gets clicked the page scrolls such that the very top of the panel content is flush with the top of the screen. 此代码段工作得相当好,但是当单击面板标题时,页面会滚动,使得面板内容的最顶部与屏幕顶部齐平。 Is there a way to modify this so that the scrolling effect will result in the panel "heading" (as opposed to the top of panel content area) being visible at the top of the screen? 有没有办法修改它,以便滚动效果将导致面板“标题”(而不是面板内容区域的顶部)在屏幕顶部可见?

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

Let me know if I should be sharing the bootstrap accordion html as well. 如果我也应该分享bootstrap手风琴html,请告诉我。

I used this and it works fine you can adjust the -20 after the .offset().top if you need to tweak it up or down a little. 我使用了它并且它工作正常你可以在.offset()之后调整-20。如果你需要向上或向下调整它。

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

This is to target the specific .panel-heading clicked as per James Wilson's comment on the accepted answer. 这是根据James Wilson对已接受答案的评论来定位特定的.panel标题。

$(function () {
    $('#accordion').on('shown.bs.collapse', function (e) {
        var offset = $(this).find('.collapse.in').prev('.panel-heading');
        if(offset) {
            $('html,body').animate({
                scrollTop: $(offset).offset().top -20
            }, 500); 
        }
    }); 
});

All I changed from gigelsmith's accepted answer is 'var offset' and the scrollTop's target. 我从gigelsmith接受的答案中改变的是'var offset'和scrollTop的目标。

I couldn't get the answer above to work, perhaps I'm missing something but I can't see how the scrollTop line above relates to the currently opened accordion item so used the following code instead. 我无法得到上面的答案,也许我错过了一些东西,但我看不出上面的scrollTop线如何与当前打开的手风琴项相关,所以使用了以下代码。 Hope it helps someone else: 希望它可以帮助其他人:

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

Always animate looks a bit too much so this is my version to only do the job when heading is over the visible part. 总是动画看起来有点太多,所以这是我的版本,只有在标题超过可见部分时才能完成工作。 (note that I use a data-accordion-focus to apply the fix) (请注意,我使用data-accordion-focus来应用修复)

 $('[data-accordion-focus]').on('shown.bs.collapse', function (e) { var headingTop = $(e.target).prev('.panel-heading').offset().top - 5; var visibleTop = $(window).scrollTop(); if (headingTop < visibleTop) { $('html,body').animate({ scrollTop: headingTop }, 500); } }); 

By using .panel-default as selector of .on() , you can scroll to the active panel. 通过使用.panel-default作为.on()选择器,您可以滚动到活动面板。

$('#accordion').on('shown.bs.collapse', '.panel-default', function (e) {
    $('html,body').animate({
        scrollTop: $(this).offset().top
    }, 500); 
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM