[英]Angular/ng-bootstrap - Change Progressbar with current slide
我的目的是在每张幻灯片上更改进度条的宽度。 我目前正在使用引导程序提供的进度条。 到目前为止,我正在为每张幻灯片调用 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>
创建一个保存幻灯片中当前进度百分比的变量,并在您触发 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-模板)
试试这个代码:
// 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.