gpt4 book ai didi

javascript - 打开时滚动到引导 Accordion 的顶部

转载 作者:行者123 更新时间:2023-11-30 09:16:21 26 4
gpt4 key购买 nike

<分区>

我最近一直在开发一个小型博客,并希望可以从 accordion 中访问连续的博客文章。 (下面的例子)。这样,您可以快速浏览帖子标题,选择一个有趣的帖子并阅读。完成后,您可以无缝返回浏览,无需不必要的菜单导航。

我的问题是,一旦您读完一篇文章并点击第二篇文章,我无法让第二篇文章在顶部打开,并且标题可见。由于您已经向下滚动阅读了第一篇文章,因此您打开了第二篇文章的 2/3。这迫使用户一直滚动回到他尚未阅读的帖子的顶部。出于某种原因,我似乎什么都做不了;任何指导将不胜感激!

更新:事实证明,由于我使用的是 jQuery 的精简版,因此我尝试使用的功能不可用。现在我已经克服了这个障碍,一切都可以编译,但我不能强制页面滚动到正确的位置。

我最接近的解决方案是这个,它会在打开新部分时向上滚动到第一个 card-header (而我想要的是 card-header点击)。

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

我正在尝试做的在逻辑上等同于此,但这段确切的代码根本不会滚动(它编译正确,并将 $(this) 替换为 $( event.target)$(event.target).parent() 也不起作用。

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

这里有一个最小的工作示例来重现我的挣扎:

<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>

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com