简体   繁体   English

Angular/ng-bootstrap - 使用当前幻灯片更改进度条

[英]Angular/ng-bootstrap - Change Progressbar with current slide

My intention is to change the progress bar's width with every slide.我的目的是在每张幻灯片上更改进度条的宽度。 I'm currently using the progress bar given by bootstrap.我目前正在使用引导程序提供的进度条。 So far I'm calling an event in HTML with every slide.到目前为止,我正在为每张幻灯片调用 HTML 中的事件。 This event fetches this information about the slide:此事件获取有关幻灯片的以下信息:

Console:
{prev: "ngb-slide-0", current: "ngb-slide-1", direction: "left"}


The progress bar fetches its information regarding the width from currentSlide (app.component.ts), which works fine:进度条从 currentSlide (app.component.ts) 中获取有关宽度的信息,它工作正常:

<div class="progress">
    <div class="progress-bar" role="progressbar" [style.width.%]="currentSlide"></div>
  </div>


Typescript progression so far:到目前为止的打字稿进展:

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);
  }
}


My solution path in words:我的解决方案路径:

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

How to transform these words into code?如何将这些词转化为代码?
Thanks in advance, Tim提前致谢,蒂姆



Rest of html (Slider example):其余的 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>

Create a variable that holds the current percentage of the progress in the slides and change that value when you fire the changeProgress method and bind that percentage to your progress bar like so :创建一个保存幻灯片中当前进度百分比的变量,并在您触发 changeProgress 方法时更改该值并将该百分比绑定到您的进度条,如下所示:

<div class="progress-bar active" role="progressbar" [style.width.%]="currentPercentage"
             [attr.aria-valuenow]="currentPercentage" aria-valuemin="0" aria-valuemax="100">{{this.currentPercentage}}%</div>

and in your component it should look something like this在你的组件中它应该是这样的

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;
  }
}

Hope this helps!希望这可以帮助! and like so you can in the future add more images to your slide show and have a dynamic code ( i would suggest using a ng-For to display all ng-templates of your carrousel)就像这样,您将来可以向幻灯片放映添加更多图像并拥有动态代码(我建议使用 ng-For 来显示轮播的所有 ng-模板)

Try this code :试试这个代码:

// 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM