Angular Material 2 : Progress Bar animation choppy

标签 angular typescript animation

我正在尝试使用 Angular 构建一个页面,该页面具有各种进度条,表示为需要一定时间的操作所取得的进度。我想要做的是使用 Angular Material 进度条制作从时间 0 到结束时间的平滑动画。我目前的解决方案只是每 50ms 更新一次进程的百分比,并将进度条的值设置为该值。我正在使用 rxjs 计时器完成此操作,如下所示:

const start = new Date().getTime();
const source = timer(0, 50);
const subscribe = source.subscribe(() => {
  const time = new Date().getTime();
  if (time - start > length) {
    subscribe.unsubscribe();
  }

  this.progressValue = 100 * (time - start) / length;
});

这是进度条 HTML 本身:
 <mat-progress-bar mode="determinant" [value]="progressValue"></mat-progress-bar>

然而,结果是进度条的进展非常不稳定。有一个更好的方法吗?我每次都知道这个过程的长度,所以我觉得这段时间的平滑过渡应该相对容易。谢谢!

最佳答案

这是我想出的:

html

<mat-progress-bar color="accent" mode="determinate" [value]="uploadPercentage$ | async"></mat-progress-bar>

成分:

@ViewChild(MatProgressBar) progressBar: MatProgressBar;
uploadPercentage$: Observable<number>;
uploadProgress$: Subject<HttpProgressEvent> = new Subject<HttpProgressEvent>();

ngAfterViewInit(): void {
    // wait until animation ends and only then change value
    this.uploadPercentage$ = this.progressBar.animationEnd
      .pipe(
        startWith({
          value: 0
        }),
        switchMap((animationState: ProgressAnimationEnd) => this.uploadProgress$
          .pipe(
            map((progress: HttpProgressEvent): number => this.progressToPercents(progress)),
            distinctUntilChanged((val: number) => animationState.value !== val)
          )
        ),
      );
  }

关于 Angular Material 2 : Progress Bar animation choppy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54058952/

相关文章:

javascript - 如何从2个javascript数组中获取唯一记录?

安卓开发 : Share animated Gif from Internal Storage

javascript - Typescript:类型 'X' 与签名 '(prevState: undefined): undefined' 不匹配

angular - "this.appInits[i]"不是函数

javascript - 通过数字动画化 CSS 属性

java - 如何创建自定义插值器以在 android 中应用翻译动画

jquery - 如何在 angular2 webpack 中使用 jquery?获取 $ not defined

javascript - NgOnChanges 在用户键入时覆盖表单控件值

CSS 未显示在谷歌浏览器的覆盖选项卡中

angular - 如何规范化 ngrx/store 中的深层嵌套数据?