Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iDangero.us Swiper not working when inside a div set hidden display:none

I want the swiper gallery to show after clicking on a "Show the gallery" link. But it looks like Swiper is not working properly if initialized inside a hidden div.

Here is my page:

<div>
<a href=“javascript:;” onclick=“toggle_visibility('gallery');”><img src=“img.jpg” alt=“Click to open the gallery”></a>
</div>

<div id=“gallery” style:“display:none”>
<!— Swiper —>
<div id=“slide-event” class=“swiper-container swiper-container-event”>
<div class=“swiper-wrapper”>
…
</div>
<!— Add Arrows —>
<div id=“show-hide” class=“swiper-button-next”></div>
<div id=“show-hide” class=“swiper-button-prev”></div>
</div>
<!— End Swiper —>
</div>

<script src="js/swiper.jquery.js"></script>

This workaround doesn't work for me:

function reinitSwiper(swiper) {
  setTimeout(function () {
   swiper.reInit();
  }, 500);
}

Maybe there is another a javascript trick? Any suggestion?

like image 211
Frédéric Delaloire Avatar asked Nov 25 '25 14:11

Frédéric Delaloire


1 Answers

I found out these listeners:

observer: true

observer Set to true to enable Mutation Observer on Swiper and its elements. In this case Swiper will be updated (reinitialized) each time if you change its style (like hide/show) or modify its child elements (like adding/removing slides)

observeParents: true

observeParents Set to true if you also need to watch Mutations for Swiper parent elements

like image 170
Frédéric Delaloire Avatar answered Nov 28 '25 02:11

Frédéric Delaloire



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!