突發奇想,JavaScript模仿下載進度條效果

來源:互聯網
上載者:User

標籤:lse   gre   更新   調用函數   調用   背景色   color   ==   http   

不定時的更新,這一次採用JavaScript 模仿下載進度條效果。原理:兩個div嵌套,裡面的寬度0,外部自己隨便定義,採用 setInterval() 函數增加 裡面的div 的 width的值,使其背景色慢慢平鋪開來,同時增加一個計數值“index” 表示進度。採用

 window.getComputedStyle()函數擷取寬度.

直接show code:

 

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title>    <style type="text/css">        #fa{            width: 600px;            height: 50px;            margin: 100px auto;            background-color:#ffffff;            border: 0.5px solid #999999;            border-radius: 1px;            box-sizing: content-box;        }        #ch{            width: 0px;            height: 50px;            background-color: pink;            text-align: right;            color: green;            line-height: 50px;            box-sizing: content-box;        }    </style></head><body>        <div id="fa">        <div id="ch">        </div>        </div><script type="text/javascript">    window.onload=function () {        var ta_length=600;  //fa 的寬        var ta_time=200;// 走到頭的時間        var ta_min=20;// 走一步的速度        Ago(ta_length,ta_time,ta_min); //調用函數    function Ago(ta_length,ta_time,ta_min) {           var  elem=document.getElementById("ch"); //擷取ch           var fa=document.getElementById("fa");           var stepLength=ta_length/ta_time;           var cover=0;           var index=0;            alert("開始下載");          var step=function() {                   index+=0.5;               if (cover+stepLength<=ta_length){                   elem.style.width=parseFloat(getStyle(elem,‘width‘))+stepLength+‘px‘; //擷取的寬度與每一步stapLength相加                   elem.innerHTML=index+"%";// 計數值                   cover+=stepLength; // ch的寬度增加                                              if (index==100){                            elem.style.backgroundColor="green";//百分百後背景顏色變化                        }               }               else {                   fa.style.border="0px";                   clearInterval(IntervalId);                   elem.style.border="1px solid green";                   elem.innerHTML=" ";                   alert("下載完成");               }           }        var IntervalId=setInterval(step,ta_min);    }    function getStyle(elem,cssname) {        if(window.getComputedStyle){            return window.getComputedStyle(elem)[cssname] //擷取ch 的寬度        }else{            return elem.currentStyle[cssname];        }    }    }</script></body></html>

 

 

 

   您可以:     點擊這裡查看效果

 

突發奇想,JavaScript模仿下載進度條效果

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.