gpt4 book ai didi

jquery - Bootstrap 3 折叠滚动顶部

转载 作者:行者123 更新时间:2023-12-01 02:56:52 26 4
gpt4 key购买 nike

我在使用 Bootstrap 3 的 Accordion 时遇到了一些问题。

我想要做的是将一个类添加到 Accordion 切换中,然后滚动/跳转到新添加的类。添加/删除类效果很好,我想我只需在同一个 block 中添加一个scrollTop...但没有这样的运气。我错过了什么?

设置方式如下:

面板链接:

<ul>
<li><a data-toggle="collapse" data-parent="#accordion" href="#panel1">Panel1</a>
</li>
<li><a data-toggle="collapse" data-parent="#accordion" href="#panel2">Panel2</a>
</li>
<li>
<a data-toggle="collapse" data-parent="#accordion" href="#panel3" ">Panel3</a></li>
</ul>

面板:

<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle aWeight active" data-toggle="collapse" data-parent="#accordion" href="#panel1">
Panel 1
</a>
</h4>

</div>
<div id="panel1" class="panel-collapse collapse in">
<div class="panel-body">Anim pariatur...</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle aFitness" data-toggle="collapse" data-parent="#accordion" href="#panel2">
Panel 2
</a>
</h4>

</div>
<div id="panel2" class="panel-collapse collapse">
<div class="panel-body">Anim pariatur...</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle aFitness" data-toggle="collapse" data-parent="#accordion" href="#panel3">
Panel 3
</a>
</h4>

</div>
<div id="panel3" class="panel-collapse collapse">
<div class="panel-body">Anim pariatur...</div>
</div>
</div>
</div>

最后,我使用此脚本来切换事件类:

$(function () {
$('.accordion').on('show', function (e) {
$(e.target).prev('.accordion-heading').find('.accordion-toggle').addClass('active');
});
$('.accordion').on('hide', function (e) {
$(this).find('.accordion-toggle').not($(e.target)).removeClass('active');
});
});

最佳答案

您在使用 Bootstrap 2 事件时引用了 Bootstrap 3,请参阅:http://getbootstrap.com/javascript/#collapse 。折叠插件已经添加了一个类(.in)。

shown.bs.collapse 触发时,您的"new"面板具有“.in”类,而当 hide.bs.collapse 触发“旧”面板时面板仍然具有“.in”类。

尝试:

$(function () {
$('#accordion').on('shown.bs.collapse', function (e) {
var offset = $('.panel.panel-default > .panel-collapse.in').offset();
if(offset)$('html,body').scrollTop(offset.top);
});
});

`

关于jquery - Bootstrap 3 折叠滚动顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19237641/

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