question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Swiper in modal with Ajax

See original GitHub issue

Hello!

I have problem with loading Swiper in Bootstrap modal with Ajax.

Problem is that slider not working. It seems like slider blocked, pagination have only 1 dot, but in real there are more images downloaded from DB. And next/prev buttons also blocked.

I tried use reInit:

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

after “$(‘.swiper-container’).swiper({…});” in script.js , but it change nothing

Here my code:

/script.js/

$('#id').on('click', function () {
    var ID = $(this).attr('id');
    $.ajax({
        url: './',
        type: 'POST',
        data: {ID: ID},
        success: function(res){
            $(res).appendTo($('body'));
            /*Swiper*/
            $('.swiper-container').swiper({
                pagination: '.swiper-pagination',
                paginationClickable: true,
                nextButton: '.swiper-button-next',
                prevButton: '.swiper-button-prev'
            });
            /*Bootstrap modal run*/
            $('#myModal').modal('show');
        },
        error: function(){
            alert('Error');
        }
    });
});

/controller.php/

if(isset($_POST['ID'])){
    $ID = $_POST['ID'];
    $img = getImgfromDB($dbconn, $ID);
    require_once 'modalImg.php';
    exit;
}

/modalImg.php/

<div class="modal fade bs-example-modal-lg" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header text-center">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
            </div>
            <div class='galleryContent'>
                    <div class="swiper-container">
                        <div class="swiper-wrapper">
                            <?php foreach($img as $item): ?>
                            <div class="swiper-slide"><img src="_img/<?=$item?>" /></div>
                            <?php endforeach; ?>
                        </div>
                        <div class="swiper-pagination"></div>
                        <div class="swiper-button-next"></div>
                        <div class="swiper-button-prev"></div>
                    </div>
            </div>
        </div>
    </div>
</div>

Thanks in advance, for your help!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
wwwwwwoorkscommented, Jan 13, 2017

I just discovered that to fix this issue in most of the cases adding:

  observer: true,
  observeParents: true,

to the swiper call do the trick.

So from the example above:

var swiper = new Swiper('#slide-event', {
  effect: 'slide',
  keyboardControl: true,
  centeredSlides: true,
  loop: false,
  observer: true,
  observeParents: true,
  nextButton: '.swiper-button-next',
  prevButton: '.swiper-button-prev',
  speed: 0,
})
0reactions
W1773NDcommented, Feb 13, 2018

thanks wwwwwwoorks !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Swiper in modal with Ajax · Issue #1598
Hello! I have problem with loading Swiper in Bootstrap modal with Ajax. Problem is that slider not working. It seems like slider blocked, ......
Read more >
Swiper Ajax Lazy loading test case
$(document). ready(function() { var swiper = new Swiper(". swiper-container", { // Optional parameters effect: "fade", //autoplay: 2500, loop: true, nextButton ...
Read more >
Swipe not working when added dynamically via AJAX
Finally I have figured out the underlying issue. As Page B is loaded by Ajax each time, Swipe script is not loaded.
Read more >
[Best solution]-Set active slide in modal click on Swiper slider
Im having a table where it has a a button which opens up a modal window and inside the modal there is a...
Read more >
VenoBox 2 - Javascript modal window plugin, touch swipe ...
The big difference compared to many others modal-window plugins is that ... Touch Swipe and Arrow Keys navigation enabled ... Gmaps iFrame Ajax...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found