I am trying to get one flickity carousel to work in each of my accordion items and my lack of knowledge in js gets me unfortunately to a dead end. What happens is that I see my carousels broken (not working) inside my accordion items until I slightly resize my viewport. Then the one that is open adjusts itself, while the others remain broken.
My js looks like that at the moment:
$(document).ready(function(){
$('.event-carousel').flickity({
cellAlign: 'left',
setGallerySize: false,
freeScroll: true,
wrapAround: true,
prevNextButtons: false,
pageDots: false
});
$('.accordion__item').on('click', function() {
var $this = $(this);
if ($this.hasClass('collapsed')) {
$('.accordion__item').addClass('collapsed');
$this.removeClass('collapsed');
} else {
$this.addClass('collapsed');
}
});
}
While my html is so:
<div class="event-carousel">
<?php foreach ($slides as $slide):
$img = $slide->image()->toFile();
?>
<div class="carousel-cell">
<?= $img->thumb(['format' => 'webp']) ?>
</div>
<?php endforeach ?>
</div>
</div>
My gut feeling tells me that somehow I should tell my js to initialise or destroy the carousel everytime these classes are added or removed.
Could anyone give me a lesson here? Thank you!