简体   繁体   English

如何在特定点停止JavaScript进度栏

[英]How to stop JavaScript progress bar at a certain point

I'm using the example progress bar shown in W3Schools.com . 我正在使用W3Schools.com中显示的示例进度栏。

How would I make the progress bar stop at a fixed point, let's say, 90%? 我如何使进度条停在某个固定点(例如90%)? I see that it has something to do with the width of the bar, but I can't figure out how to change it to a fixed point, or even a variable. 我看到它与条的宽度有关,但我不知道如何将其更改为固定点甚至变量。

 function move() { var elem = document.getElementById("myBar"); var width = 10; var id = setInterval(frame, 10); function frame() { if (width >= 100) { clearInterval(id); } else { width++; elem.style.width = width + '%'; document.getElementById("label").innerHTML = width * 1 + '%'; } } } 
 #myProgress { position: relative; width: 100%; height: 30px; background-color: #ddd; } #myBar { position: absolute; width: 10%; height: 100%; background-color: #4CAF50; } #label { text-align: center; line-height: 30px; color: white; } 
 <h1>JavaScript Progress Bar</h1> <div id="myProgress"> <div id="myBar"> <div id="label">10%</div> </div> </div> <br> <button onclick="move()">Click Me</button> 

Change the condition to width >= 90 . 将条件更改为width >= 90

Code: 码:

function move() {
  var elem = document.getElementById("myBar");   
  var width = 10;
  var id = setInterval(frame, 10);
  function frame() {
    /* CHANGE THIS TO 90 */
    if (width >= 90) {
      clearInterval(id);
    } else {
      width++; 
      elem.style.width = width + '%'; 
      document.getElementById("label").innerHTML = width * 1  + '%';
    }
  }
}

Working sample: https://jsfiddle.net/mspinks/vLfhno9x/ 工作示例: https : //jsfiddle.net/mspinks/vLfhno9x/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM