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>
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.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With