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

用户名

我的意图是在每张幻灯片上更改进度条的宽度。我目前正在使用引导程序提供的进度栏。到目前为止,每张幻灯片我都用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的Tim Rest(滑块示例):

<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>
蒂亚戈·席尔瓦(Tiago Silva)

创建一个保存幻灯片中当前进度百分比的变量,并在触发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模板)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在 angular 4 中使用 bootstrap 4 或 ng-bootstrap 组件

Angular ng-if和更改Twitter Bootstrap col-sm

使用NPM获取ng-bootstrap以用于angular cli项目

如何使用Angular 2修改ng-bootstrap轮播的CSS

无法在Angular 9中使用Ng-bootstrap

如何使用 Bootstrap Carousel 更改幻灯片?

如何使用ng-show / ng-hide隐藏Angular Bootstrap UI中的选项卡

如何使用angular-cli和ng-bootstrap自定义Bootstrap 4 SCSS变量?

导航栏未在使用ng-bootstrap和angular 4的浏览器中呈现

Angular2 + ng-bootstrap示例

Angular 2 ng-bootstrap close模态

带ng-hide的Angular Bootstrap Alert

来自Angular中ng-bootstrap的Popover

Angular JS-幻灯片视图但不显示主页-ng-animate

导航栏切换不适用于Angular 4 / ng-bootstrap

在angular-ui-bootstrap轮播上设置活动幻灯片

Angular UI Bootstrap滑块每张幻灯片有多个项目

动态更改Angular 5 ng-bootstrap模态对话框的模板

无法使用ng bootstrap和angular 6在同一日历中设置日期

Angular2:使用浏览器后退事件关闭ng-bootstrap模态

如何使用ng-bootstrap在Angular中显示动态选项卡内容

为什么angular的ng-disabled不能与bootstrap的btn类一起使用?

将ng-bootstrap与angular-cli 1.0.0-beta.18一起使用

Angular5 ng-bootstrap-如何仅使用ngb-panel

无法将ng-bootstrap与Webpack和Angular2一起使用

如何使用ng2-bootstrap覆盖angular2中的twitter引导样式

使用Angular Bootstrap手风琴将ID添加到ng-template

使用ng-bootstrap从api获取的Angular 6分页

如何使用ng bootstrap和angular 6以编程方式设置自定义日期?