9

I want to slide automatically the ionic slider but its not working. slider contains images also. data is got from API call.

  <ion-slides  autoplay="5000" loop="true" speed="300" pager="true" >
        <ion-slide *`ngFor`="let item of topStories">
            <ion-card (click)=" this.newsService.onGoToTopStoryPage(item)">
                <ion-card-content >
                    <ion-img [src] = "item.image"></ion-img>
                    <h2> <b>{{item.title}} </b></h2>
                    <h4>{{item.summary}}</h4>
            </ion-card-content>
            </ion-card>
        </ion-slide>
       </ion-slides>
Sampath
  • 63,341
  • 64
  • 307
  • 441

8 Answers8

29

Try this might work in your app:

settings the options autoplay to true

slideOptsOne = {
 initialSlide: 0,
 slidesPerView: 1,
 autoplay:true
};

page.html

<ion-slides [options]="slideOptsOne">
 <ion-slide *`ngFor`="let item of topStories">
   <ion-card (click)=" this.newsService.onGoToTopStoryPage(item)">
     <ion-card-content>
       <ion-img [src]="item.image"></ion-img>
       <h2> <b>{{item.title}} </b></h2>
       <h4>{{item.summary}}</h4>
     </ion-card-content>
   </ion-card>
 </ion-slide>
</ion-slides>

Add your config in options in the page.ts file. Let me know it's work or not.

Jaydeep Rathod
  • 422
  • 3
  • 8
9

Don't use ion-img. It shows a broken image for the next slides for the first time. Use <img> as shown below.

Note: Ionic team has developed ion-img for ion-virtual-scroll.

.ts

 import { IonSlides } from '@ionic/angular';

  slideOptions = {
    initialSlide: 1,
    speed: 400,
  };

  constructor() { }

  slidesDidLoad(slides: IonSlides): void {
    slides.startAutoplay();
  }

.html

<ion-slides [options]="slideOptions" pager="true" #slider (ionSlidesDidLoad)="slidesDidLoad(slider)">
        <ion-slide *ngFor="let img of data?.images">
          <img [src]="img">
        </ion-slide>
      </ion-slides>
Sampath
  • 63,341
  • 64
  • 307
  • 441
1

This is the typescript file:

export class HomePage {
    slider: any;
    slideOptions = {
    initialSlide: 0,
    slidesPerView: 1,
    autoplay: true
  };
  
  constructor() {}
  slideChanged()
  {
     this.slider.stopAutoplay(); //this code for slide after page change
     }
  }

This is the HTML file:

<ion-slides  #slider loop="true" pager="true" [options]="slideOpts" (ionSlideTouchEnd)="slideChanged()" >
  <ion-slide>
    <img src="https://png.pngtree.com/thumb_back/fh260/back_pic/00/02/44/5056179b42b174f.jpg" />
  </ion-slide>
  <ion-slide>
    <img src="https://www.neowebtec.com/images/banner-imgg.jpg" />
  </ion-slide>
  <ion-slide>
    <img src="https://i.pinimg.com/originals/0b/a3/d6/0ba3d60362c7e6d256cfc1f37156bad9.jpg" />
  </ion-slide>
</ion-slides>
DGarvanski
  • 2,555
  • 3
  • 26
  • 35
BAPUJI
  • 11
  • 2
0

I guess you ran into a racing condition. Perhaps the element was rendered before the data (topStories) is ready. In that case, there is no slide to "autoplay".

Try to put an *ngIf to the element to render the element when data is ready. For example:

this.data_loaded = false;
...

this.apiGetData().then((data) => {
    ...
    this.data_loaded = true;
});


######################################

<ion-slides *ngIf="data_loaded">
...
</ion-slides>
Sem
  • 1,237
  • 16
  • 17
0
<ion-slides pager #Slider class="slides (ionSlidesDidLoad)="afterslidesLoad(Slider)">
   <ion-slide>
     <img src="image1" />
   </ion-slide>
   <ion-slide>
     <img src="image2" />
   </ion-slide>
</ion-slides>

afterslidesLoad(slides) {
  slides.startAutoplay();
}

Above code is my working code. You have to implement startAutoPlay method.

0

Use input property [autoplay]="3000" to pass milliseconds:

<ion-slides [pager]="true"  [autoplay]="3000">
    <ion-slide>
      Slide 1
    </ion-slide>
    <ion-slide>
      Slide 2
    </ion-slide>
    <ion-slide>
      Slide 3
    </ion-slide>
</ion-slides>
Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
0

please try this solution:

  <ion-slides *ngIf="topStories.length!=0" autoplay="5000" loop="true" speed="300" pager="true" >
    <ion-slide *ngFor="let item of topStories">
        <ion-card (click)=" this.newsService.onGoToTopStoryPage(item)">
            <ion-card-content >
                <ion-img [src] = "item.image"></ion-img>
                <h2> <b>{{item.title}} </b></h2>
                <h4>{{item.summary}}</h4>
        </ion-card-content>
        </ion-card>
    </ion-slide>
   </ion-slides>
Nazeeh
  • 45
  • 1
  • 10
0

This is how to use the autoplay setting in Ionic 5

<ion-slides [options]="{ slidesPerView:2, autoplay: {delay: 1500} }">
Stef Van Looveren
  • 302
  • 1
  • 5
  • 15