前端JavaScript-百分比动态进度条

作者: 560b7bb7b879 | 来源:发表于2019-05-15 22:14 被阅读5次

    实现了百分比动态进度条效果,实际应用通常需要和后台配合使用。

    代码实例如下:

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <meta name="author" content="http://www.softwhy.com/" />
    <title>web前端学习交流扣qun:731771211   志同道合者进</title>
    <style>
    #progressBox{ 
      width: 300px; 
      height: 40px; 
      border: 1px solid #c8c8c8; 
      background: white;
      position: relative;
    }
    #progressBar{ 
      position: absolute;
      left: 0;
      top: 0;
      z-index: 2;
      height: 40px; 
      width: 100%; 
      line-height: 40px; 
      color: white; 
      text-align: center; 
      font-size: 20px; 
      font-weight: bold;
      font-family: Georgia; 
      clip: rect(0px,0px,40px,0px); 
      background: #00A1F5;
    }
    #progressText{
      position: absolute;
      left: 0;
      top: 0;
      z-index: 1;
      width: 100%; 
      height: 40px; 
      line-height: 40px; 
      color: black; 
      text-align: center;
      font-size: 20px;
      font-weight: bold;
      font-family: Georgia;
    }
    </style>
    <script>
    window.onload = function () {
      var iNow = 0;
      var timer = setInterval(function (argument) {
        if (iNow == 100) {
          clearInterval(timer);
        } else {
          iNow += 1;
          progressFn(iNow);
        }
      
      }, 30);
      
      function progressFn(cent) {
        var oDiv1 = document.getElementById('progressBox');
        var oDiv2 = document.getElementById('progressBar');
        var oDiv3 = document.getElementById('progressText');
      
        var allWidth = parseInt(getStyle(oDiv1, 'width'));
      
        oDiv2.innerHTML = cent + '%';
        oDiv3.innerHTML = cent + '%';
        oDiv2.style.clip = 'rect(0px,' + cent / 100 * allWidth + 'px,40px,0px)';
      
        function getStyle(obj, attr) {
          if (obj.currentStyle) {
            return obj.currentStyle[attr];
          }
          else {
            return getComputedStyle(obj, false)[attr];
          }
        }
      }
    }       
    </script>
    </head>
    <body>
      <div id="progressBox">
        <div id="progressBar">0%</div>
        <div id="progressText">0%</div>
      </div>
    </body>
    </html>
    
    
    

    相关文章

      网友评论

        本文标题:前端JavaScript-百分比动态进度条

        本文链接:https://www.haomeiwen.com/subject/qsxvaqtx.html