进度条在达到100时不会刷新

时间:2017-04-25 07:57:23

标签: javascript html css progress

我有一个圆形进度条可以工作,但当它达到100%时,它应该刷新并重新开始,但事实并非如此。这是我的JSFiddle(由于某种原因它在那里不起作用,但它适用于我的浏览器)

https://jsfiddle.net/BrsJsk/5e9hs69y/5/

    window.onload = function() {
  var progressbar = document.querySelector('div[data-progress]'),
    quad1 = document.querySelector('.quad1'),
    quad2 = document.querySelector('.quad2'),
    quad3 = document.querySelector('.quad3'),
    quad4 = document.querySelector('.quad4'),
    counter = document.querySelector('.counter');


  var progInc = setInterval(incrementProg, 1000); // call function every second

  function incrementProg() {
    progress = progressbar.getAttribute('data-progress'); //get current value
    progress++; // increment the progress bar value by 1 with every iteration
    progressbar.setAttribute('data-progress', progress); //set value to attribute
    counter.textContent = 100 - parseInt(progress, 10); // set countdown timer's value
    setPie(progress); // call the paint progress bar function based on progress value
    if (progress == 100) {
      clearInterval(progInc); // clear timer when countdown is complete

    }
  }

  function setPie(progress) {
    /* If progress is less than 25, modify skew angle the first quadrant */
    if (progress <= 25) {
      quad1.setAttribute('style', 'transform: skew(' + progress * (-90 / 25) + 'deg)');
      progressbar.setAttribute('style', 'box-shadow: inset 0px 0px 0px 50px #6edbf2');

    }

    /* Between 25-50, hide 1st quadrant + modify skew angle of 2nd quadrant */
    else if (progress > 25 && progress <= 50) {
      quad1.setAttribute('style', 'transform: skew(-90deg)'); // hides 1st completely
      quad2.setAttribute('style', 'transform: skewY(' + (progress - 25) * (90 / 25) + 'deg)');
      progressbar.setAttribute('style', 'box-shadow: inset 0px 0px 0px 50px #6edbf2');
    }

    /* Between 50-75, hide first 2 quadrants + modify skew angle of 3rd quadrant */
    else if (progress > 50 && progress <= 75) {
      quad1.setAttribute('style', 'transform: skew(-90deg)'); // hides 1st completely
      quad2.setAttribute('style', 'transform: skewY(90deg)'); // hides 2nd completely
      quad3.setAttribute('style', 'transform: skew(' + (progress - 50) * (-90 / 25) + 'deg)');
      progressbar.setAttribute('style', 'box-shadow: inset 0px 0px 0px 50px #6edbf2');
    }

    /* Similar to above for value between 75-100 */
    else if (progress > 75 && progress < 100) {
      quad1.setAttribute('style', 'transform: skew(-90deg)'); // hides 1st completely
      quad2.setAttribute('style', 'transform: skewY(90deg)'); // hides 2nd completely
      quad3.setAttribute('style', 'transform: skew(-90deg)'); // hides 3rd completely
      quad4.setAttribute('style', 'transform: skewY(' + (progress - 75) * (90 / 25) + 'deg)');
      progressbar.setAttribute('style', 'box-shadow: inset 0px 0px 0px 50px #6edbf2');
    }
  }
}
.quad1,
.quad3 {
  transform: skew(0deg);
  /* invisible at -90deg */
}

.quad2,
.quad4 {
  transform: skewY(0deg);
  /* invisible at 90deg */
}


/* Just for demo */

div[data-progress] {
  margin: 40px auto;
}

<div data-progress="0">
  <div class="quad1"></div>
  <div class="quad2"></div>
  <div class="quad3"></div>
  <div class="quad4"></div>
  <div class='counter'>100</div>
</div>

3 个答案:

答案 0 :(得分:2)

您只能在达到100%进度时停止间隔。要重新开始,您必须主动重置进度。你可以这样做。

不要读取进度并增加进度,而是执行此操作。

progress = parseInt(progressbar.getAttribute('data-progress'));
progress = ((progress + 1) % 100);

即,treat作为整数的进度(因为它是什么),然后使用modulo operator将进度包围到0时。

您还必须重置进度条的所有象限。

if (progress === 0) {
    quad1.setAttribute('style', 'transform: skew(0deg)');
    quad2.setAttribute('style', 'transform: skewY(0deg)');
    quad3.setAttribute('style', 'transform: skew(0deg)');
    quad4.setAttribute('style', 'transform: skewY(0deg)');
}

您可以在更新进度后立即插入。你可以摆脱清除间隔的线条。

完整演示。我将间隔稍微加快一点,仅用于演示目的。

&#13;
&#13;
var progressbar = document.querySelector('div[data-progress]'),
  quad1 = document.querySelector('.quad1'),
  quad2 = document.querySelector('.quad2'),
  quad3 = document.querySelector('.quad3'),
  quad4 = document.querySelector('.quad4'),
  counter = document.querySelector('.counter');


var progInc = setInterval(incrementProg, 50);

function incrementProg() {
  progress = parseInt(progressbar.getAttribute('data-progress'));
  progress = ((progress + 1) % 100);
  if (progress === 0) {
    quad1.setAttribute('style', 'transform: skew(0deg)');
    quad2.setAttribute('style', 'transform: skewY(0deg)');
    quad3.setAttribute('style', 'transform: skew(0deg)');
    quad4.setAttribute('style', 'transform: skewY(0deg)');
  }
  progressbar.setAttribute('data-progress', progress); //set value to attribute
  counter.textContent = 100 - parseInt(progress, 10); // set countdown timer's value
  setPie(progress); // call the paint progress bar function based on progress value
}

function setPie(progress) {
  /* If progress is less than 25, modify skew angle the first quadrant */
  if (progress <= 25) {
    quad1.setAttribute('style', 'transform: skew(' + progress * (-90 / 25) + 'deg)');
    progressbar.setAttribute('style', 'box-shadow: inset 0px 0px 0px 50px #6edbf2');

  }

  /* Between 25-50, hide 1st quadrant + modify skew angle of 2nd quadrant */
  else if (progress > 25 && progress <= 50) {
    quad1.setAttribute('style', 'transform: skew(-90deg)'); // hides 1st completely
    quad2.setAttribute('style', 'transform: skewY(' + (progress - 25) * (90 / 25) + 'deg)');
    progressbar.setAttribute('style', 'box-shadow: inset 0px 0px 0px 50px #6edbf2');
  }

  /* Between 50-75, hide first 2 quadrants + modify skew angle of 3rd quadrant */
  else if (progress > 50 && progress <= 75) {
    quad1.setAttribute('style', 'transform: skew(-90deg)'); // hides 1st completely
    quad2.setAttribute('style', 'transform: skewY(90deg)'); // hides 2nd completely
    quad3.setAttribute('style', 'transform: skew(' + (progress - 50) * (-90 / 25) + 'deg)');
    progressbar.setAttribute('style', 'box-shadow: inset 0px 0px 0px 50px #6edbf2');
  }

  /* Similar to above for value between 75-100 */
  else if (progress > 75 && progress < 100) {
    quad1.setAttribute('style', 'transform: skew(-90deg)'); // hides 1st completely
    quad2.setAttribute('style', 'transform: skewY(90deg)'); // hides 2nd completely
    quad3.setAttribute('style', 'transform: skew(-90deg)'); // hides 3rd completely
    quad4.setAttribute('style', 'transform: skewY(' + (progress - 75) * (90 / 25) + 'deg)');
    progressbar.setAttribute('style', 'box-shadow: inset 0px 0px 0px 50px #6edbf2');
  }
}
&#13;
div[data-progress] {
  box-sizing: border-box;
  position: relative;
  height: 200px;
  width: 200px;
  background: #c8c9cb;
  border-radius: 50%;
  transition: all 1s;
  overflow: hidden;
}

.counter {
  position: absolute;
  height: 100%;
  width: 100%;
  top: 0%;
  left: 0%;
  text-align: center;
  line-height: 200px;
  border-radius: 50%;
  background: transparent;
  z-index: 4;
}

div>div {
  position: absolute;
  height: 50%;
  width: 50%;
  background: inherit;
  border-radius: 0%;
}

.quad1,
.quad2 {
  left: 50%;
  transform-origin: left bottom;
}

.quad3,
.quad4 {
  left: 0%;
  transform-origin: right top;
}

.quad1,
.quad4 {
  top: 0%;
}

.quad2,
.quad3 {
  top: 50%;
}

.quad1,
.quad3 {
  transform: skew(0deg);
  /* invisible at -90deg */
}

.quad2,
.quad4 {
  transform: skewY(0deg);
  /* invisible at 90deg */
}


/* Just for demo */

div[data-progress] {
  margin: 40px auto;
}
&#13;
<div data-progress="0">
  <div class="quad1"></div>
  <div class="quad2"></div>
  <div class="quad3"></div>
  <div class="quad4"></div>
  <div class='counter'>100</div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以编写初始化函数并清除您的饼图。所以你可以在clearInterval(progInc)之后调用它。

  function initPie() {
        quad1.setAttribute('style', 'transform: skew(0deg)');
            quad2.setAttribute('style', 'transform: skewY(0deg)');
            quad3.setAttribute('style', 'transform: skew(0deg)');
            quad4.setAttribute('style', 'transform: skewY(0deg)');
      progress = progressbar.setAttribute('data-progress', '-1');
      progInc = setInterval(incrementProg, 100);
  }

https://jsfiddle.net/5e9hs69y/11/

答案 2 :(得分:0)

要重新开始,一旦达到100,就需要使progress 0,并且不要clearInterval。然后,您需要将样式恢复为初始状态。

示例(为我工作https://jsfiddle.net/mvv07jdk/):

if (progress == 100) {
    progressbar.setAttribute('data-progress', 0);
    quad1.setAttribute('style', 'transform: skew(0)');
    quad2.setAttribute('style', 'transform: skew(0)');
    quad3.setAttribute('style', 'transform: skew(0)'); 
    quad4.setAttribute('style', 'transform: skew(0)');
}