標籤: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模仿下載進度條效果