【问题标题】:Vue - set bootstrap 5 progress bar width according to countdown timer advanceVue - 根据倒数计时器提前设置引导5进度条宽度
【发布时间】:2021-07-21 19:23:33
【问题描述】:

我想使用 bootstrap 5 进度条来显示倒计时的剩余时间。我已经在我的 vuejs 组件模板中创建了所需的标记

          <div class="progress">
            <div class="progress-bar" ref="elaspedTime" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">
              {{ minutes }} seconds
            </div>
          </div>

在我的方法中,我有这两个功能,一个是开始倒计时,另一个是在分钟到零时停止倒计时。

    startTimer(){
      this.$refs.elaspedTime.width = '0%';
      this.countdown = setInterval( () => {
        this.minutes--;
        if( this.$refs.elaspedTime.width < '100%' ){
          this.$refs.elaspedTime.width += '0.6%';
        }
        if( this.minutes === 0 ){
          this.stopTimer();
        }
      },1000);
    },
    stopTimer(){
      clearInterval(this.countdown);
    }

如何根据已过的倒计时分钟更改进度条宽度直到达到 100%?

【问题讨论】:

  • 启动/停止计时器的代码/触发器在哪里? Vue 数据()?请包含所有相关代码

标签: javascript vue.js bootstrap-5


【解决方案1】:

你可以做这样的事情......

new Vue({
    el: "#app",
    data: () => ({
        minutes: 10,
        countdown: null
    }),
    methods: {
        startTimer(){
          let seconds = this.minutes // initial time
          let et = this.$refs.elaspedTime
          et.style.width = '0%'
          this.countdown = setInterval(() => {
            this.minutes--;
            et.style.width = ((seconds - this.minutes) * 100)/seconds + '%'
            if(this.minutes === 0){
              this.stopTimer();
            }
          },1000);
        },
        stopTimer(){
          clearInterval(this.countdown);
        }
    },
})

Demo


另见:Using Bootstrap 5 with Vue

【讨论】:

  • 谢谢你,像魅力一样工作。我错过了el.style.width和关于倒计时的计算((seconds - this.minutes) * 100)/seconds + '%'
  • 问个问题,进度条宽度达到100%会停止填充吗?
  • 是的,因为计时器以分钟停止 === 0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多