Previously, I provided a solution for a similar question regarding materialize.css.
Feel free to adjust the logic based on your preferences, as my style might have influenced this response. To maintain consistency with my earlier answer, it's important to note that the scrolling effect won't be applied if:
- It's the initial panel element in the accordion,
- The height of its body is less than the desired height.
This implementation activates the scrolling effect once the panel body is "shown" post-animation by listening for bootstrap's collapse event, shown.bs.collapse
, allowing validation based on whether the height surpasses the specified threshold.
$('.panel-collapse').each(function(){
$(this).on('shown.bs.collapse', function(){
var $this = $(this),
//$header = $this.siblings('.panel-heading'),
$parent = $this.parent(),
height = $this.height(),
maxHeight = 400;
if ($parent.is(':first-child')) return;
if (height > maxHeight)
$('html, body').animate({
scrollTop: $parent.offset().top
}, 500);
});
})
Access updated fiddle here
A feedback mentioned that the previous snippet does not scroll back up to the header if the height doesn't exceed the specified maximum height (adjusted to 400 in the code above).
If you wish to eliminate the validation criteria, simply remove all the if
conditions and proceed accordingly.
$('.panel-collapse').each(function(){
$(this).on('shown.bs.collapse', function(){
var $this = $(this),
$parent = $this.parent();
$('html, body').animate({
scrollTop: $parent.offset().top
}, 500);
});
})