【问题标题】:Countinuous Progress bar连续进度条
【发布时间】:2022-12-02 23:33:31
【问题描述】:

我实现了一个进度条,但我遇到了问题。

完成进度条应该需要 5 秒。我的问题是该值每秒更新一次,我希望它是连续的,没有任何停止。

DEMO

HTML

<dx-progress-bar
#progressBar
[showStatus]="false"
id="progress-bar-status"
[rtlEnabled]="true"
[class.complete]="progressBar.value == maxValue"
[min]="0"
[max]="maxValue"
[value]="maxValue - seconds"
>
</dx-progress-bar>

.TS

 seconds = 5;
  maxValue = 5;
  intervalId: number;

  onButtonClick() {
    this.seconds = 5;
    this.intervalId = window.setInterval(() => this.timer(), 1000);
  }

  timer() {
    this.seconds--;
    if (this.seconds == 0) {
      clearInterval(this.intervalId);
    }
  }

预期结果

Example

【问题讨论】:

  • 对于动画你应该看看Window.requestAnimationFrame()
  • 您需要更改 css transition..example: .dx-trackbar-range { transition: width 3s ease-in-out !important; }

标签: javascript angular typescript devextreme


【解决方案1】:

计时器功能每秒运行一次,因此每个动画帧都应该运行它:

   seconds = 5;
   maxValue = 5;
   lastFrame = undefined;
   intervalId: number;
    
   onButtonClick() {
     this.seconds = 5;
     this.intervalId = window.requestAnimationFrame(this.timer);
   }
    
   timer(time) {
     if (this.lastFrame == undefined) {
       this.lastFrame = time;
     }
     this.seconds -= (time - this.lastFrame);
     this.lastFrame = time;
     if (this.seconds != 0) {
       this.intervalId = window.requestAnimationFrame(this.timer);
     }
   }

【讨论】:

  • 每毫秒可能有点多。为什么不改用requestAnimationFrame()呢?
  • 是的,这是真的。我会编辑它。
猜你喜欢
  • 2011-07-26
  • 1970-01-01
  • 1970-01-01
  • 2014-09-09
  • 2020-10-12
  • 2010-12-24
  • 1970-01-01
  • 2019-06-01
  • 1970-01-01
相关资源
最近更新 更多