I have a requirement to start a carousel to start with a dynamic index, not from 0. Do we have any npm package, support for Angular 4 or above .
@ngu/carousel is already use, myCarousel.moveTo() doesn't seem to work for this senario
I have a requirement to start a carousel to start with a dynamic index, not from 0. Do we have any npm package, support for Angular 4 or above .
@ngu/carousel is already use, myCarousel.moveTo() doesn't seem to work for this senario
You can just randomize your primary Array (the array with images), that way each new view of the page will see a different sequence in your carousel
relevant HTML:
<ngu-carousel #myCarousel [inputs]="carouselConfig" [dataSource]="carouselItems">
<div *nguCarouselDef="let item;" class="item">
<div class="tile">{{item}}</div>
</div>
<button NguCarouselNext class="rightRs">></button>
<button NguCarouselPrev class="leftRs"><</button>
<ul class="myPoint" NguCarouselPoint>
<li *ngFor="let j of myCarousel.pointNumbers; let j = index" [class.active]="j==myCarousel.activePoint" (click)="myCarousel.moveTo(j)"></li>
</ul>
</ngu-carousel>
relevant TS:
import { Component, AfterViewInit, ViewChild, ChangeDetectorRef } from '@angular/core';
import { NguCarousel, NguCarouselConfig } from '@ngu/carousel';
@Component({
selector: 'app-simple',
templateUrl: './simple.component.html',
styleUrls: ['./simple.component.css']
})
export class SimpleComponent implements AfterViewInit {
name = 'Angular';
slideNo = 0;
withAnim = true;
resetAnim = true;
@ViewChild('myCarousel') myCarousel: NguCarousel;
carouselConfig: NguCarouselConfig = {
grid: { xs: 1, sm: 1, md: 1, lg: 1, all: 0 },
load: 3,
interval: { timing: 4000, initialDelay: 1000 },
loop: true,
touch: true,
velocity: 0.2
}
carouselItems: any[];
constructor(private cdr: ChangeDetectorRef) {
let myArray: any[] = ['item 1', 'item 2', 'item 3'];
myArray = this.shuffleArray(myArray);
this.carouselItems = myArray;
}
shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
ngAfterViewInit() {
this.cdr.detectChanges();
}
reset() {
this.myCarousel.reset(!this.resetAnim);
}
moveTo(slide) {
this.myCarousel.moveTo(slide, !this.withAnim);
}
}
check a working stackblitz here
Moving myCarousel.moveTo()
to within ngAfterViewInit(){}
rather than ngOnInit(){}
worked for me.
Responding to old thread as I overcame same problem in different way to previous answer.