简体   繁体   English

打开时滚动到引导手风琴的顶部

[英]Scroll to the top of bootstrap accordion when opened

I've recently been working on a small blog, and would like consecutive blog posts to be accessible from within an accordion (example below). 我最近一直在从事一个小型博客的工作,希望在手风琴中可以访问连续的博客文章(以下示例)。 This way, you can quickly skim over post titles, select one which is interesting, and read it. 这样,您可以快速浏览帖子标题,选择一个有趣的标题并阅读。 When finished, you can seamlessly go back to skimming without unnecessary menu navigation. 完成后,您可以无缝返回浏览,而无需不必要的菜单导航。

My problem is that once you're done reading a post and click on a second one, I can't get the second post to open at the top, with the title visible. 我的问题是,一旦您读完一篇文章并单击第二篇文章,我将无法在顶部打开第二篇文章(标题可见)。 Since you've already scrolled so far down to read the first post, you open 2/3 of the way through the second post. 由于您已经向下滚动了以阅读第一篇文章,因此您可以在第二篇文章的2/3处打开。 This forces the user to scroll all the way back up to the top of a post which he hasn't read yet. 这会迫使用户一直滚动到尚未读过的帖子的顶部。 For some reason I can't seem to get anything to work; 由于某种原因,我似乎什么也无法工作。 any guidance would be much appreciated! 任何指导将不胜感激!

UPDATE: It turns out that since I was using the slim version of jQuery, the functions I was trying to use weren't available. 更新:事实证明,由于我使用的是jQuery的苗条版本,因此我尝试使用的功能不可用。 Now that I'm over that hurdle, everything compiles but I can't force the page to scroll to the right place. 现在,我已经克服了这个障碍,所有内容都可以编译,但是我不能强迫页面滚动到正确的位置。

My closest solution is this, which will scroll back up to the first card-header when a new section is opened (whereas I want the card-header which was clicked). 我最接近的解决方案是此方法,当打开新部分时,它将向上滚动到第一个card-header (而我要单击该card-header )。

$(".card-header").click(function (event) {                                      
    $('html, body').animate({                                                   
        scrollTop: $(".card-header").offset().top                               
    }, 300);                                                                    
}); 

What I'm trying to do is logically equivalent to this, but this exact code doesn't scroll at all (it compiles correctly, and replacing $(this) with $(event.target) or $(event.target).parent() doesn't work either). 我想做的事在逻辑上等效于此,但是此确切的代码根本无法滚动(它正确编译,并用$(event.target)$(event.target).parent()替换$(this) $(event.target).parent()也不起作用)。

$(".card-header").click(function(event) {                                                                           
    $('html, body').animate({                                                   
        scrollTop: $(this).offset().top-60                     
    }, 300);  

Here's a minimal working example to reproduce my struggles: 这是一个重现我的奋斗的最小工作示例:

 <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/> <div class="container" id="container"> <div class="card"> <a class="card-header" data-toggle="collapse" href="#sec1">Title1</a> <div id="sec1" class="collapse" data-parent="#container"> <div class="card-body"> lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text </div> </div> </div> <div class="card"> <a class="card-header" data-toggle="collapse" href="#sec2">Title2</a> <div id="sec2" class="collapse" data-parent="#container"> <div class="card-body"> lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text </div> </div> </div> </div> 

Always scroll to the offset-top of the opening collapse element. 始终滚动到打开折叠元素的偏移顶部。 If the closing collapse if above the opening collapse then subtract the height of the closing collapse from the offset-top of the opening collapse. 如果闭合塌陷位于开口塌陷上方,则从开口塌陷的偏移顶部减去闭合塌陷的高度。

 jQuery(function($){ $('.card-header').each(function(){ let $card_header = $(this); let $collapse_element = $card_header.next(); $collapse_element.on('show.bs.collapse', function () { let $card_header_top = $card_header.offset().top; let $visible_collapse = $('.collapse.show'); if($visible_collapse.length){ let $visible_collapse_top = $visible_collapse.offset().top; if($visible_collapse_top < $card_header_top){ $card_header_top -= $visible_collapse.height(); } } $([document.documentElement, document.body]).animate({ scrollTop: $card_header_top }); }); }); }); 
 <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/> <div style="height:30px"></div> <div class="container" id="container"> <div class="card"> <a class="card-header" data-toggle="collapse" href="#sec1">Title1</a> <div id="sec1" class="collapse" data-parent="#container"> <div class="card-body"> lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text </div> </div> </div> <div class="card"> <a class="card-header" data-toggle="collapse" href="#sec2">Title2</a> <div id="sec2" class="collapse" data-parent="#container"> <div class="card-body"> lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text lots and lots of text </div> </div> </div> </div> 

I added the id to the div tag from <div class="card"> to <div class="card" id="div2"> and add javascrip: 我将id从<div class="card"><div class="card" id="div2">添加到div标记中,并添加了Javascrip:

$(".card-header").click(function () {
  $('html, body').animate({
    scrollTop: $("#div2").offset().top
  }, 2000);
});

It work for me. 它对我有用。 You don't need to override everything. 您无需覆盖所有内容。

Have use in event 在事件中使用

  $(document).ready(function () { $("html, body").animate({ scrollTop: 0 }, 600); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 

Have fun 玩得开心

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

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