Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 4 Slider autoplay

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>
like image 581
Thilina Ranaweera Avatar asked Apr 12 '19 11:04

Thilina Ranaweera


3 Answers

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.

like image 180
Jaydeep Rathod Avatar answered Nov 16 '22 05:11

Jaydeep Rathod


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>
like image 25
Sampath Avatar answered Nov 16 '22 05:11

Sampath


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>
like image 43
BAPUJI Avatar answered Nov 16 '22 06:11

BAPUJI