33

I'm trying to add the Swiper plugin to one of my page. What I'm trying to achieve is to integrate get the carousal slider over here http://idangero.us/swiper/demos/05-slides-per-view.html

HTML

<div class="swiper-container">
  <div class="swiper-wrapper">
    <div class="swiper-slide">Slide 1</div>
    <div class="swiper-slide">Slide 2</div>
    <div class="swiper-slide">Slide 3</div>
    <div class="swiper-slide">Slide 4</div>
    <div class="swiper-slide">Slide 5</div>
    <div class="swiper-slide">Slide 6</div>
    <div class="swiper-slide">Slide 7</div>
    <div class="swiper-slide">Slide 8</div>
    <div class="swiper-slide">Slide 9</div>
    <div class="swiper-slide">Slide 10</div>
  </div>
  <div class="swiper-pagination"></div>
</div>

JS

var swiper = new Swiper('.swiper-container', {
  pagination: '.swiper-pagination',
  slidesPerView: 3,
  paginationClickable: true,
  spaceBetween: 30,
  // Navigation arrows
  nextButton: '.swiper-button-next',
  prevButton: '.swiper-button-prev',
});

When i add it to a fiddle it works but when i add to my html page, the swiper doesnt work until i open firebug or resize the page (http://vidznet.com/ng1/swiper/swipe.html) Im not sure if its conflicting when initializing because There are no errors in the console.

After spending some time I thought it might be a jquery issue and wrapped the coding inside a

pagebeforecreate

 $(document).on( "pagebeforecreate", "#new_",function( event ) { 

but still the same,

I also added the below code

swiper.updateContainerSize();

which is supposed to update the container size, but still not working.

Any help will be much appreciated.

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
LiveEn
  • 3,193
  • 12
  • 59
  • 104

11 Answers11

51

Swiper's sliders, in order to work properly, requires you to either:

As stated by @Isaiahiroko and Swiper's creator (on this issue), executing one of the mentioned topics will solve your issue, for sure!

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
  • observer: true, solved my issue, As i was using vuejs, and need to fetch data after the slider been initialized. thanks. –  Aug 02 '20 at 09:00
  • 1
    should be the accepted answer here, this solves all update issues you may run into on css calculations. – rx2347 Feb 24 '21 at 11:57
24

Also i had face this problem , i think you should add two lines (Parameters) in the Java Script of swipper ,

var galleryThumbs = new Swiper('.gallery-thumbs', {
            observer: true,
            observeParents: true,
            loop: true,
            spaceBetween: 10,
            slidesPerView: 4,
            freeMode: true,
            watchSlidesVisibility: true,
            watchSlidesProgress: true,
        })

Where "obeserver : true" And "observerParents : true" are the parameters which effects your swipper to be work properly. This make my swipper working in properly ! Hope you get done !

Adarsh Bhalani
  • 451
  • 5
  • 10
13

Use this code:

swiper.update();
  • 4
    In my case, it is React-based app with animations when swipeable content first appears, I had to call it with a small delay in `setTimeout` – amankkg Dec 21 '19 at 15:37
3

I tried below code it worked for me my swiper was present inside a tab, when my document is loaded swipper was not working unless and unless I resize the screen,so i tried below code it worked

var swiper = new Swiper('#upcoming-appointments', {
        slidesPerView: 1,
        spaceBetween: 30,
        loop: true,
        pagination: {
            el: '.swiper-pagination',
            clickable: true,
        },
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
//<---Add this two lines in your code-->   
       observer: true,  
       observeParents: true,
    });
Omkar
  • 110
  • 3
3

For Angular:

Add (swiper)="onSwiper($event)" to your swiper component.

<swiper (swiper)="onSwiper($event)"></swiper>

And in your .ts add:

onSwiper(swiper) {
  swiper.update();
}
Zahema
  • 1,345
  • 2
  • 19
  • 35
2

This solved my problem

  1. Add this lines in your swiper initialization

    observer: true, observeParents: true,

    your initialization will look like this

      var swiper = new Swiper('.swiper-container', {
        pagination: '.swiper-pagination',
        slidesPerView: 3,
        observer: true,
        observeParents: true,
        paginationClickable: true,
        spaceBetween: 30,
        // Navigation arrows
    nextButton: '.swiper-button-next',
    prevButton: '.swiper-button-prev',
    });
    
  2. Then, update your swiper instance with

    swiper.update()

1

you could try :

$(window).on({
   load: function(){
      $(this).trigger('resize');
    } 
  });

though it's a bit of a stickytape solution.

Sam0
  • 1,459
  • 1
  • 11
  • 13
  • 1
    could you try $(window).trigger('resize'); in a setTimeout function with a one second delay? - not as a solution but just as a test. I suspect as you do that it's to do with loading sequences – Sam0 May 04 '17 at 16:57
0

For anyone reading this, and using version 6.x of Swiper, you need to import the additional modules (like Navigation, Pagination, etc.) for them to work.

// core version + navigation, pagination modules:
import Swiper, { Navigation, Pagination } from 'swiper';

// configure Swiper to use modules
Swiper.use([Navigation, Pagination]);

// init Swiper:
const swiper = new Swiper(...);
Dharman
  • 30,962
  • 25
  • 85
  • 135
daveaspinall
  • 1,395
  • 1
  • 17
  • 28
  • Here is the list of every module that you shall import https://swiperjs.com/api/#custom-build – Raphaël Balet Nov 11 '20 at 10:17
  • Even in newest swiper (and thats some years) the same bug remains, when using lazy and more images at time, they are not loaded till resize or till interaction with slider, no observer or anything mentioned above works, only way around is to use simple index and load first few images in normal way, all others in lazy way. – Jiro Matchonson Nov 11 '20 at 23:39
0

I had the similar issue,

in my case adding rebuildOnUpdate: true helped (you may also need observer: true as mentioned above)

p.s. I'm working with react-id-swiper and swiper

0

I had a similar issue, in my case, I replace the class name swiper-container with swiper in the main container

-2

Only add

autoplay: {delay:3000,disableOnInteraction:false}

to parameters

Penny Liu
  • 15,447
  • 5
  • 79
  • 98