title: Bootstrap accordion, scroll to top of active accordion on click, how will i implement on nested accordion?
body: <p>I'm making a responsive site using Bootstrap and it contains nested accordions with a large amount of text, when you read to the bottom and click the next accordion, the large amount of text is collapsed and I'm left at the bottom of the page.</p>
  
  <p>I found this useful code from <a href="https://stackoverflow.com/questions/35992900/bootstrap-accordion-scroll-to-top-of-active-open-accordion-on-click">Bootstrap accordion, scroll to top of active (open) accordion on click?</a> but it scrolls only first level.
  When i click on nested accordion it scroll two time and position of scroll is not perfect.
  I have added below script from link which i have mention on above para.</p>
  
  <pre>
  
      $(document).ready(function(){
                      $('.panel-collapse').on('shown.bs.collapse', function(e) {
                        var $panel = $(this).closest('.panel');
                        $('html,body').animate({
                          scrollTop: $panel.offset().top
                        }, 500);
                      });
                  });
  
  </pre>
  
  <p>HTML</p>
  
  <pre><code>&lt;pre&gt;
  
          &lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
                  &lt;div class="bs-example" data-example-id="collapse-accordion"&gt;
                  &lt;div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true"&gt;
                      &lt;div class="panel panel-default"&gt;
                          &lt;div class="panel-heading" role="tab" id="headingOne"&gt;
                              &lt;h4 class="panel-title"&gt;
                                  &lt;a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne" class=""&gt; Collapsible Group Item #1 &lt;/a&gt;
                              &lt;/h4&gt;
                          &lt;/div&gt;
                          &lt;div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne" aria-expanded="true"&gt;
                              &lt;div class="panel-body"&gt;
                                  Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia 
                                  aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred 
                                  nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
                                  &lt;div class="panel-group" id="accordion1" role="tablist" aria-multiselectable="true"&gt;
                                      &lt;div class="panel panel-default"&gt;
                                          &lt;div class="panel-heading" role="tab" id="headingOne1"&gt;
                                              &lt;h4 class="panel-title"&gt;
                                                  &lt;a role="button" data-toggle="collapse" data-parent="#accordion1" href="#collapseOne1" aria-expanded="true" aria-controls="collapseOne1" class=""&gt; Collapsible Group Item #1 &lt;/a&gt;
                                              &lt;/h4&gt;
                                          &lt;/div&gt;
                                          &lt;div id="collapseOne1" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne1" aria-expanded="true"&gt;
                                              &lt;div class="panel-body"&gt; Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 
                                                  wolf moon fficia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf  oon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice  omo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
                                              &lt;/div&gt;
                                          &lt;/div&gt;
                                          &lt;div class="panel panel-default"&gt;
                                              &lt;div class="panel-heading" role="tab" id="headingOne2"&gt;
                                                  &lt;h4 class="panel-title"&gt;
                                                      &lt;a role="button" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne2" aria-expanded="true" aria-controls="collapseOne2" class=""&gt; Collapsible Group Item #2 &lt;/a&gt;
                                                  &lt;/h4&gt;
                                              &lt;/div&gt;
                                              &lt;div id="collapseOne2" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne2" aria-expanded="true"&gt;
                                                  &lt;div class="panel-body"&gt; Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon  fficia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher  ice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.&lt;/div&gt;
                                              &lt;/div&gt;
                                              &lt;div class="panel panel-default"&gt;
                                                  &lt;div class="panel-heading" role="tab" id="headingOne3"&gt;
                                                      &lt;h4 class="panel-title"&gt;
                                                          &lt;a role="button" data-toggle="collapse" data-parent="#accordion3" href="#collapseOne3" aria-expanded="true" aria-controls="collapseOne3" class=""&gt; Collapsible Group Item #3 &lt;/a&gt;
                                                      &lt;/h4&gt;
                                                  &lt;/div&gt;
                                                  &lt;div id="collapseOne3" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne3" aria-expanded="true"&gt;
                                                      &lt;div class="panel-body"&gt; Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon fficia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan  xcepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
                                                      &lt;/div&gt;
                                                  &lt;/div&gt;
                                              &lt;/div&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                              &lt;div class="panel panel-default"&gt;
                                  &lt;div class="panel-heading" role="tab" id="headingTwo"&gt;
                                      &lt;h4 class="panel-title"&gt; &lt;a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"&gt; Collapsible Group Item #2 &lt;/a&gt; &lt;/h4&gt;
                                  &lt;/div&gt;
                                  &lt;div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo" aria-expanded="false" style="height: 0px;"&gt;
                                      &lt;div class="panel-body"&gt; Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua
                                          put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table,
                                          raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. 
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                              &lt;div class="panel panel-default"&gt;
                                  &lt;div class="panel-heading" role="tab" id="headingThree"&gt;
                                      &lt;h4 class="panel-title"&gt; &lt;a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="false" aria-controls="collapseThree"&gt; Collapsible Group Item #3 &lt;/a&gt; &lt;/h4&gt;
                                  &lt;/div&gt;
                                  &lt;div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree" aria-expanded="false" style="height: 0px;"&gt;
                                      &lt;div class="panel-body"&gt; Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua
                                          put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table,
                                          raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. 
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
  
      &lt;/pre&gt;
  </code></pre>