As I show in the image in my question, I've needed to know the active index for me to be able to show the white line with css.
The autoplay feature have sadly a little bug and it is impossible to get the correct swiper.activeIndex
since it does never reset to 0.
I had to implement a custom feature and wished to share it with you.
percentage:number = 0
activeIndex = 0
defaultDelay = 500
slides = ['1', '2', '3']
private _initSwiper() {
const options: SwiperOptions = {...}
const swiperEl = this._swiperRef.nativeElement
Object.assign(swiperEl, options)
swiperEl.initialize()
if (this.swiper) this.swiper.currentBreakpoint = false // Breakpoint fixes
this.swiper = this._swiperRef.nativeElement.swiper
// Custom Autoplay Time
this.doAutoplay(this.defaultDelay, this.activeIndex)
// If the user manually change the slide
this.swiper.on('slideChange', (swiper: Swiper) => {
this.activeIndex = swiper.activeIndex
this.doAutoplay(this.defaultDelay, this.activeIndex)
})
}
doAutoplay(delay: number, index: number) {
if (this.activeIndex !== index) return
this.percentage = 100 - (delay / this.defaultDelay) * 100 + '%'
if (delay <= 0) {
setTimeout(() => {
if (this.activeIndex + 1 === this.slides.length) this.swiper.slideTo(0)
else this.swiper.slideTo(this.activeIndex + 1)
}, 300)
return
}
setTimeout(() => {
this.doAutoplay(delay - 10, index)
}, 10)
}