How to make a progress bar

后端 未结 19 1490
失恋的感觉
失恋的感觉 2020-12-04 18:09

How would one go about making a progress bar in html/css/javascript. I don\'t really want to use Flash. Something along the lines of what can be found here: http://dustincur

19条回答
  •  离开以前
    2020-12-04 18:45

    I was writing up an answer to a similar question that got deleted, so I'm posting it here in case it's of use to anyone.

    The markup can be dropped in anywhere and takes up 50px of vertical real estate even when hidden. (To have it take up no vertical space and instead overlay the top 50px, we can just give the progressContainerDiv absolute positioning (inside any positioned element) and style the display property instead of the visible property.)

    The general structure is based on code presented in this Geeks for Geeks article.

    const
      progressContainerDiv = document.getElementById("progressContainerDiv");
      progressShownDiv = document.getElementById("progressShownDiv");
    let
      progress = 0,
      percentageIncrease = 10;
    
    function animateProgress(){
      progressContainerDiv.style.visibility = "visible";
      const repeater = setInterval(increaseRepeatedly, 100);
      function increaseRepeatedly(){
        if(progress >= 100){
          clearInterval(repeater);
          progressContainerDiv.style.visibility = "hidden";
          progressNumberSpan.innerHTML = "";
          progress = 1;
        }
        else{
          progress = Math.min(100, progress + percentageIncrease);
          progressShownDiv.style.width = progress + "%";
          progressNumberSpan.innerHTML = progress + "%";
        }
      }
    }
    #progressContainerDiv{
      visibility: hidden;
      height: 40px;
      margin: 5px;
    }
    
    #progressBackgroundDiv {
      width: 50%;
      margin-left: 24%;
      background-color: #ffffd;
    }
      
    #progressShownDiv {
      width: 1%;
      height: 20px;
      background-color: #4CAF50;
    }
    
    #progressNumberSpan{
      margin: 0 auto;
    }

提交回复
热议问题