简体   繁体   English

引导侧边栏导航菜单滚动到活动面板标题的顶部

[英]bootstrap sidebar nav menu scroll to top of active panel heading

What i am trying to do is, i have a sidebar menu (accordion type) and i am trying to scrolls up to the top of the currently active panel heading of my我想要做的是,我有一个侧边栏菜单(手风琴类型),我试图向上滚动到我的当前活动面板标题的顶部

<ul class="nav flex-column flex-nowrap overflow-hidden gph-group">
<li class="slm-panel nav-item">
    <a class="nav-link collapsed"  data-toggle="collapse" data-target="#sub1">
    <span>
    Agriculture Diggers                                      
    </span></a>
    <div class="gph-panel collapse" id="sub1" aria-expanded="false">
    <ul class="flex-column pl-2 nav">
            <li class="nav-item">
                <a class="nav-link py-0" href="">
                    <span>sub 1</span>
                </a>
            </li>
    </ul> 
</li>                                   
<li class="slm-panel nav-item">
    <a class="nav-link text-truncate" href="">
        <span>
        ABC                                              
        </span>
    </a>
</li>
<li class="slm-panel nav-item">
    <a class="nav-link text-truncate" href="">
        <span>
        DEF                                              
        </span>
    </a>
</li> 

The problem is when i click on a menu is it moving up and i want to scroll to the top of the menu which is active.问题是当我单击菜单时它是否向上移动并且我想滚动到处于活动状态的菜单顶部。 i am using the below js我正在使用下面的js

$('.gph-panel').on('show.bs.collapse', function (e) {
    var $panel = $(this).closest('.slm-panel');
    console.log($panel.offset());
     $('html,body').animate({
            scrollTop: $panel.offset().top - 20
      }, 500);
    $('.gph-group .in').collapse('hide');
});

when i click the menu it is actually going down but not to the top of the active menu.当我单击菜单时,它实际上正在下降,但没有到达活动菜单的顶部。 Can you please help me?你能帮我么? i found useful reference here here but it is not working as expect.我在这里找到了有用的参考,但它没有按预期工作。

UPDATED更新

My jsfiddle: https://jsfiddle.net/37yuL2d9/1/我的jsfiddle: https://jsfiddle.net/37yuL2d9/1/

in my jsfiddle, for example: first click menu "ABC" then When i click the menu "XYZ" it should it should scroll to top of XYZ heading.例如,在我的 jsfiddle 中:首先单击菜单“ABC”,然后当我单击菜单“XYZ”时,它应该滚动到 XYZ 标题的顶部。 but it is not working as expected.但它没有按预期工作。

$('.gph-panel').on('show.bs.collapse', function (e) {
    var $panel = $(this).closest('.slm-panel');
    console.log($panel.offset());
     $('body').animate({                 // html is deleted
            scrollTop: $panel.offset().top - 20
      }, 500);
    $('.gph-group .in').collapse('hide');
});

Just delete the "html " part in the animate method.只需删除 animate 方法中的“html”部分即可。 I tried in your fiddle and works well.我试过你的小提琴并且效果很好。 Good Luck!祝你好运!

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

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