Javascriptで簡単なプログレスバーを実装してみました!ソースコードは以下の通りです。
function ProgressBar(id, max_count, bar_char, color)
{
this.div = setupProgressDIV(id, max_count, bar_char, color);
this.startProgress = startProgress;
this.stopProgress = stopProgress;
this.finish = finish;
var finishFlag = 0;
function setupProgressDIV(id, max_count, bar_char, color)
{
if(!id){
id = "_progress"+(new Date()).getTime();
if(document.getElementsByTagName('BODY').length==0)
document.write('<body>')
var creprgDIV = document.createElement("DIV") ;
this.div = document.body.appendChild(creprgDIV) ;
this.div.setAttribute("id",id) ;
this.div.style.position = "relative";
this.div.style.top = "0px";
this.div.style.left = "0px";
this.div.style.width = "0px";
this.div.style.height = "0px";
} else {
this.div = document.getElementById(id)
}
this.div.style.color = color ; // color of bar
this.div.style.margin = '0px' ;
this.div.style.padding = '4px';
this.div.prog_bar= bar_char; // character used for bar
this.div.prog_interval= 50; // Interval
this.div.prog_count = 0; // Initial count
this.div.prog_count_max = max_count; // Maximum count
this.div.prog_array= [];
return this.div
}
function startProgress()
{
this.div.style.height ="12px";
this.div.style.width ="auto";
this.div.prog_array.unshift(
setInterval(function(){
doProgress()
}
, this.div.prog_interval
)
)
}
function stopProgress()
{
clearInterval(this.div.prog_array[0])
this.div.prog_count = 0;
this.div.prog_array.shift()
this.div.style.width ="0px";
this.div.style.height ="0px";
this.div.innerHTML = '' ;
}
function doProgress()
{
if(finishFlag != 0){
stopProgress();
}
else{
if(this.div.prog_count >= this.div.prog_count_max){
this.div.innerHTML = '' ;
this.div.prog_count = 0;
}
this.div.innerHTML += this.div.prog_bar;
this.div.prog_count++;
}
}
function finish(){
finishFlag = 1;
}
return this;
}
function createProgressBarAndStart(id, max_count, bar_char, color) {
bar = new ProgressBar(id, max_count, bar_char, color);
bar.startProgress();
}
コメント