我正在使用Bootstrap制作一个响应式网站,它包含带有大量文本的折叠式页面,当你读到底部并单击下一个折叠式页面时,大量文本会被折叠,我会留在页面底部。
我从Bootstrap accordion scroll to top of active panel heading中找到了这个有用的代码,但是它滚动到所有 accordion 的顶部,而不是打开的特定 accordion 。
JS
$(function () {
$('#accordion').on('shown.bs.collapse', function (e) {
var offset = $('.panel.panel-default > .panel-collapse.in').offset();
if(offset) {
$('html,body').animate({
scrollTop: $('.panel-heading').offset().top -20
}, 500);
}
});
});
如何修改此代码以滚动到当前活动 accordion 的顶部?
HTML
<div class="panel-group custom-padding no-sides" id="accordion">
<div class="panel panel-default">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse1">
<div class="panel-heading custom-padding">
<h4 class="panel-title text-uppercase">Short synopsis <i class="fa fa-chevron-down pull-right"></i></h4>
</div>
</a>
<div id="collapse1" class="panel-collapse collapse">
<div class="panel-body">
<p id="game-deck"></p>
</div>
</div>
</div>
<div class="panel panel-default">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse2">
<div class="panel-heading custom-padding">
<h4 class="panel-title text-uppercase">Concepts <i class="fa fa-chevron-down pull-right"></i></h4>
</div>
</a>
<div id="collapse2" class="panel-collapse collapse">
<div class="panel-body">
<ul class="whatever" id="game-concepts"></ul>
</div>
</div>
</div>
<div class="panel panel-default">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse3">
<div class="panel-heading custom-padding">
<h4 class="panel-title text-uppercase">Themes <i class="fa fa-chevron-down pull-right"></i></h4>
</div>
</a>
<div id="collapse3" class="panel-collapse collapse">
<div class="panel-body" id="game-themes"></div>
</div>
</div>
<div class="panel panel-default">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse4">
<div class="panel-heading custom-padding">
<h4 class="panel-title text-uppercase">Notable locations<i class="fa fa-chevron-down pull-right"></i></h4>
</div>
</a>
<div id="collapse4" class="panel-collapse collapse">
<div class="panel-body" id="game-locations"></div>
</div>
</div>
<div class="panel panel-default">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse5">
<div class="panel-heading custom-padding">
<h4 class="panel-title text-uppercase">Notable characters<i class="fa fa-chevron-down pull-right"></i></h4>
</div>
</a>
<div id="collapse5" class="panel-collapse collapse">
<div class="panel-body" id="game-characters"></div>
</div>
</div>
<div class="panel panel-default">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse6">
<div class="panel-heading custom-padding">
<h4 class="panel-title text-uppercase">Full description <i class="fa fa-chevron-down pull-right"></i></h4>
</div>
</a>
<div id="collapse6" class="panel-collapse collapse">
<div class="panel-body" id="game-description"></div>
</div>
</div>
</div>
5条答案
按热度按时间zyfwsgd61#
您可以通过获取"目标元素"的顶部,然后在主体上调用animate来设置滚动的动画。
把它修改成这样会帮助你实现你所追求的
来源:http://www.abeautifulsite.net/smoothly-scroll-to-an-element-without-a-jquery-plugin-2/
辅助小提琴:https://jsfiddle.net/hjugdwbp/
Bootstrap 4示例
使用 accordion 示例:www.example.com我已经修改了代码以支持卡。https://getbootstrap.com/docs/4.0/components/collapse/#accordion-example I have modified the code to support cards.
小提琴:https://jsfiddle.net/agpkc4v2/1/
我让它更漂亮...
引导3
https://jsfiddle.net/erutfgvn/
引导4
https://jsfiddle.net/9p7f0hut/
引导5
https://jsfiddle.net/hLzg0n2y/2/
js81xvg62#
2021年更新
从Bootstrap 5开始,jQuery不再是必需的。下面是一个普通的JavaScript解决方案,可以滚动到打开的 accordion 项的顶部...
Demo
z4iuyo4d3#
我正在做一个需要相同功能的项目。产生了下面的代码。我添加了变量以进行说明:
ahy6op9u4#
当用户点击折叠箭头按钮时,上面的代码将执行以下步骤:
2.accordion[0].scrollHeight -查找打开的 accordion 的滚动高度。
3.使用animate选项和scrollTop,页面将移动到折叠式屏幕的滚动高度。
4.为了确保第2步和第3步是在 accordion 打开后完成的,setTimeout函数用于计算 accordion 打开后的代码时间。如果 accordion 主体较长,则可以相应地更改该值。在上面的代码中,当前值为100。
ou6hu8tu5#
这里有香草Javascript。
为具有accordion-collapse类的元素给予唯一ID(collapse-{{uniqueId}})后: