【发布时间】:2021-02-05 10:17:11
【问题描述】:
工作样本在此Stackblitz Link
我正在使用 rxjs 构建角度轮播滑块更改组件。每张幻灯片在 5 秒后更换。目前工作正常。但是我的问题是,当单击dot-button 时,单击的相同点幻灯片会返回并显示该幻灯片,但下一个更改不是从上一次单击的位置开始,如果单击,则检测我在上面的堆栈闪电战链接中所说的内容看到下一张幻灯片的任何点都不会从单击它的位置开始。我目前在做什么如下所示..
export class AppComponent {
dotsLength;
selectedDotIndexSubject = new BehaviorSubject<number>(undefined);
selectedDotIndexSubject$ = this.selectedDotIndexSubject
.asObservable()
.pipe(
switchMap(index => concat(of(index), of(undefined).pipe(delay(5000))))
);
readonly currentIndex$ = range(1, 3).pipe(
concatMap(value => of(value).pipe(delay(5000))),
repeat()
);
mainSlider$ = combineLatest([
this.selectedDotIndexSubject$,
this.currentIndex$
]).pipe(
map(([selectedIndex, autoIndex]) => {
return selectedIndex ?? autoIndex;
})
);
readonly images = ["6027869", "3889926", "6027869"];
ngOnInit() {
this.dotsLength = new Array(this.images.length).fill(0);
}
dotClick(index: any) {
this.selectedDotIndexSubject.next(index);
}
}
如上代码所示,我正在组合两个rxjs流,一个是点击主题,另一个是range()操作符和repeat()操作符。
这是组件的模板文件
<ng-container *ngIf="mainSlider$ | async as currentIndex">
<h2>Automatic Slideshow</h2>
{{currentIndex}}
<p>Change image every 2 seconds:</p>
<div class="slideshow-container">
<div *ngFor="let image of images; let i = index;" class="mySlides fade" [class.active]="i === currentIndex - 1">
<div class="numbertext">{{ i + 1 }} / {{ images.length }}</div>
<img
[src]="'https://images.pexels.com/photos/' + image + '/pexels-photo-' + image + '.jpeg'">
<div class="text">
Caption {{ i + 1 }}
</div>
</div>
</div>
<br>
<div style="text-align:center">
<span *ngFor="let dots of dotsLength; let i = index;"
[class.active]="currentIndex - 1 === i"
#dot
(click)="dotClick(i + 1)"
class="dot">
</span>
</div>
</ng-container>
【问题讨论】:
标签: javascript angular rxjs carousel