HTML
xxxxxxxxxx
1
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
2
3
<div class='bg-secondary w-100' style='height:750px;'>
4
</div>
5
6
<div class="accordion" id="accordionExampleY">
7
<div class="accordion-item">
8
<h2 class="accordion-header" id="headingOneY">
9
<button class="accordion-button" type="button" data-mdb-toggle="collapse"
10
data-mdb-target="#collapseOneY" aria-expanded="true" aria-controls="collapseOneY">
11
<i class="fas fa-question-circle fa-sm me-2 opacity-70"></i>Accordion Item #1
12
</button>
13
</h2>
14
<div id="collapseOneY" class="accordion-collapse collapse show" aria-labelledby="headingOneY"
15
data-mdb-parent="#accordionExampleY">
16
<div class="accordion-body">
17
<strong>This is the first item's accordion body.</strong> It is hidden by
18
default, until the collapse plugin adds the appropriate classes that we use to
19
style each element. These classes control the overall appearance, as well as
20
the showing and hiding via CSS transitions. You can modify any of this with
21
custom CSS or overriding our default variables. It's also worth noting that
22
just about any HTML can go within the <code>.accordion-body</code>, though the
23
transition does limit overflow.
24
</div>
25
</div>
26
</div>
27
<div class="accordion-item">
28
<h2 class="accordion-header" id="headingTwoY">
29
<button class="accordion-button collapsed" type="button" data-mdb-toggle="collapse"
30
data-mdb-target="#collapseTwoY" aria-expanded="false" aria-controls="collapseTwoY">
31
<i class="fas fa-question-circle fa-sm me-2 opacity-70"></i>Accordion Item #2
32
</button>
33
</h2>
34
<div id="collapseTwoY" class="accordion-collapse collapse" aria-labelledby="headingTwoY"
35
data-mdb-parent="#accordionExampleY">
36
<div class="accordion-body">
37
<strong>This is the second item's accordion body.</strong> It is hidden by
38
default, until the collapse plugin adds the appropriate classes that we use to
39
style each element. These classes control the overall appearance, as well as
40
the showing and hiding via CSS transitions. You can modify any of this with
41
custom CSS or overriding our default variables. It's also worth noting that
42
just about any HTML can go within the <code>.accordion-body</code>, though the
43
transition does limit overflow.
44
</div>
45
</div>
46
</div>
47
<div class="accordion-item">
48
<h2 class="accordion-header" id="headingThreeY">
49
<button class="accordion-button collapsed" type="button" data-mdb-toggle="collapse"
50
data-mdb-target="#collapseThreeY" aria-expanded="false" aria-controls="collapseThreeY">
51
<i class="fas fa-question-circle fa-sm me-2 opacity-70"></i>Accordion Item #3
52
</button>
53
</h2>
54
<div id="collapseThreeY" class="accordion-collapse collapse" aria-labelledby="headingThreeY"
55
data-mdb-parent="#accordionExampleY">
56
<div class="accordion-body">
57
<strong>This is the third item's accordion body.</strong> It is hidden by
58
default, until the collapse plugin adds the appropriate classes that we use to
59
style each element. These classes control the overall appearance, as well as
60
the showing and hiding via CSS transitions. You can modify any of this with
61
custom CSS or overriding our default variables. It's also worth noting that
62
just about any HTML can go within the <code>.accordion-body</code>, though the
63
transition does limit overflow.
64
</div>
65
</div>
66
</div>
67
</div>
68
69
<div class='bg-secondary w-100' style='height:750px;'>
70
</div>
CSS
1
1
JS
xxxxxxxxxx
1
$('.collapse').on('shown.bs.collapse', function(e) {
2
var $card = $(this).closest('.accordion-item');
3
var $open = $($(this).data('parent')).find('.collapse.show');
4
5
var additionalOffset = 0;
6
if($card.prevAll().filter($open.closest('.accordion-item')).length !== 0)
7
{
8
additionalOffset = $open.height();
9
}
10
window.scrollTo({
11
top: $card.offset().top - additionalOffset,
12
left: 0,
13
behavior: 'smooth'
14
})
15
});
Console errors: 0