我的意图是在每张幻灯片上更改进度条的宽度。我目前正在使用引导程序提供的进度栏。到目前为止,每张幻灯片我都用HTML调用一个事件。此事件获取有关幻灯片的以下信息:
Console:
{prev: "ngb-slide-0", current: "ngb-slide-1", direction: "left"}
进度条从currentSlide(app.component.ts)获取有关宽度的信息,该方法工作正常:
<div class="progress">
<div class="progress-bar" role="progressbar" [style.width.%]="currentSlide"></div>
</div>
到目前为止的打字稿进展:
export class AppComponent {
public currentSlide: number;
images = ['assets/images/image1.jpg', 'assets/images/image2.jpg', 'assets/images/image3.jpg'];
changeProgress(slide: any): void {
console.log(slide);
}
}
我的解决之道:
if current: ngb-slide-0 then currentSlide = 33,33
if current: ngb-slide-1 then currentSlide = 66,66
if current: ngb-slide-2 then currentSlide = 100
如何将这些单词转换为代码?
预先感谢,蒂姆
其余html(滑块示例):
<div class="container">
<div class="row">
<div class="col-12">
<ngb-carousel *ngIf="images" (slide)="changeProgress($event)">
<ng-template ngbSlide>
<img [src]="images[0]" alt="First slide">
</ng-template>
<ng-template ngbSlide>
<img [src]="images[1]" alt="Second slide">
</ng-template>
<ng-template ngbSlide>
<img [src]="images[2]" alt="Third slide">
</ng-template>
</ngb-carousel>
</div>
</div>
</div>
答案 0 :(得分:1)
创建一个保存幻灯片中当前进度百分比的变量,并在触发changeProgress方法并将其绑定到进度条时更改该值,如下所示:
<div class="progress-bar active" role="progressbar" [style.width.%]="currentPercentage"
[attr.aria-valuenow]="currentPercentage" aria-valuemin="0" aria-valuemax="100">{{this.currentPercentage}}%</div>
在您的组件中,它应该看起来像这样
export class AppComponent {
public currentSlide: number;
public currentPercentage: number;
images = ['assets/images/image1.jpg', 'assets/images/image2.jpg', 'assets/images/image3.jpg'];
changeProgress(event: any): void {
this.currentSlide = Number(event.current.substring(10));//10 because you want to remove the string 'ngb-slide-' and get only the selected image
this.currentPercentage = this.currentSlide / this.images.length * 100;
}
}
希望这会有所帮助!就像这样,以后您可以在幻灯片中添加更多图像并使用动态代码(我建议您使用ng-For来显示转盘的所有ng模板)
答案 1 :(得分:0)
尝试此代码:
// Add the caroussel to you component class, so you can access the slides array
@ViewChild(NgbCarousel) ngbCarousel;
....
changeProgress(slide: any): void {
// Find the current slide index.
let slideIndex = this.ngbCarousel.slides._results.findIndex( elem => {
return elem.id == slide.current;
});
// Convert it to a percentage
this.currentPercentage = (slideIndex + 1) / this.images.length * 100;
}