Tim*_*mD1 1 html javascript css jquery bootstrap-4
我最近一直在写一个小博客,并希望可以从手风琴中访问连续的博客文章(下面的示例)。这样,您可以快速浏览帖子标题,选择一个有趣的并阅读。完成后,您可以无缝返回浏览,而无需进行不必要的菜单导航。
我的问题是,一旦你读完一篇文章并点击第二篇文章,我就无法让第二篇文章在顶部打开,标题可见。由于您已经向下滚动阅读了第一篇文章,因此您打开了第二篇文章的 2/3。这迫使用户一直滚动回到他尚未阅读的帖子的顶部。出于某种原因,我似乎什么都做不了;任何指导将不胜感激!
更新: 事实证明,由于我使用的是 jQuery 的精简版,因此我尝试使用的功能不可用。现在我已经克服了这个障碍,一切都可以编译,但我无法强制页面滚动到正确的位置。
我最接近的解决方案是这样,它会card-header在打开新部分时向上滚动到第一个(而我想要card-header点击的部分)。
$(".card-header").click(function (event) {
$('html, body').animate({
scrollTop: $(".card-header").offset().top
}, 300);
});
Run Code Online (Sandbox Code Playgroud)
我正在尝试做的在逻辑上与此等效,但是这个确切的代码根本不滚动(它可以正确编译,并且替换$(this)为$(event.target)或$(event.target).parent()也不起作用)。
$(".card-header").click(function(event) {
$('html, body').animate({
scrollTop: $(this).offset().top-60
}, 300);
Run Code Online (Sandbox Code Playgroud)
这是重现我的挣扎的最小工作示例:
$(".card-header").click(function (event) {
$('html, body').animate({
scrollTop: $(".card-header").offset().top
}, 300);
});
Run Code Online (Sandbox Code Playgroud)
始终滚动到打开折叠元素的偏移顶部。如果关闭折叠高于打开折叠,则从打开折叠的偏移顶部减去关闭折叠的高度。
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
});
});
});
});Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1653 次 |
| 最近记录: |