html process bar

I try to take a processbar myself, without JQuery,
I do it one by one,

step 1
the origin code, coding hard, not sort, for read it to easy


<html>
<head>
<title></title>
<style>

</style>
<script>
window.onload=function(){
init();
}
var per=0; //0-100, not 0-1.0
function tout(){
var e=document.getElementById("pbar");
e.style.visibility="visible";
var lbl=document.getElementById("lblBar");
var total=document.getElementById("d2").offsetWidth;
var value=per*0.01*total-2; //2px for border left & right
e.width=value;
lbl.innerHTML=per+"%";
if(per>=100)
return;
per+=5;
setTimeout(tout, 100)
}
function init(){
//move the label lblBar("100%") to fit processbar width, height, and center
var d1=document.getElementById("d1");
var d2=document.getElementById("lblBar");
var x=d1.offsetLeft, y=d1.offsetTop;
d2.style.position="absolute";
d2.style.left=x;
d2.style.top=y;
}
function setTotalWidth(){
var e=document.getElementById("txtWidth");
var width=e.value;
var d2=document.getElementById("d2");
d2.style.width=width+"px";
e=document.getElementById("lblBar");
e.style.width=width+"px";
alert(width);
}
</script>
</head>
<body>
<div id="d1" style="width: auto">
<div id="d2" style="background: url('images/bg.gif'); width: 100px; height: 22px; border: 1px solid #777;"><img id="pbar" style="visibility: hidden" src="images/on.gif" width="0" height="22" /></div>
<label id="lblBar" style="height: 22px; width: 100px; line-height: 22px; text-align: center">0%</label>
</div>
<input id="txtWidth" type="text" value="100" /><input type="button" value="TOTAL WIDTH" onclick="setTotalWidth()" />
<input type="button" value="START" onclick="tout()" />
</body>
</html>


here is result
[img]http://dl.iteye.com/upload/attachment/547307/b229beaa-89b1-3f75-946a-cfa620b4b667.jpg[/img]

step 2
now, begin optimize
resize gif, reduce bar-bg.gif & bar-on.gif to 1 width, separator with 2 file, not combine to 1X44px file, else the bar-on effect will be damaged
coding css style to div d2:
style="background: url('images/bar-bg.gif') repeat-x; width: 100px; height: 22px; border: 1px solid #777;"
pls note the result I want is: repeat-x 1 px gif

step 3
remove html code, encapsulate with JS

<html>
<head>
<title></title>
<style>

</style>
<script>
(function(){
var Pbar=function(id){
this.per=0;
var txt="<div id=\"d2\" style=\"background: url('images/bar-bg.gif') repeat-x; width: 100px; height: 22px; border: 1px solid #777;\"><img id=\"pbar\" style=\"visibility: hidden\" src=\"images/bar-on.gif\" width=\"0\" height=\"22\" /></div>\n"
+"<label id=\"lblBar\" style=\"height: 22px; width: 100px; line-height: 22px; text-align: center\">0%</label>";
var e=document.getElementById(id);
e.innerHTML=txt;
this.init();
}
Pbar.prototype.init=function(){
var d1=document.getElementById("d1");
var d2=document.getElementById("lblBar");
var x=d1.offsetLeft, y=d1.offsetTop;
d2.style.position="absolute";
d2.style.left=x;
d2.style.top=y;
}
Pbar.prototype.processBar=function(n){
this.per+=n;
var e=document.getElementById("pbar");
e.style.visibility="visible";
var lbl=document.getElementById("lblBar");
var total=document.getElementById("d2").offsetWidth;
var value=this.per*0.01*total-2;
e.width=value;
lbl.innerHTML=this.per+"%";
if(this.per>=100)
return;
}
window.$Pbar=Pbar;
})();
var bar;
window.onload=function(){
bar=new $Pbar("d1");
}
function demo(){
if(bar.per>=100)
return;
bar.processBar(10);
setTimeout(demo, 200);
}
</script>
</head>
<body>
<div id="d1">
<!--
<div id="d2" style="background: url('images/bar-bg.gif') repeat-x; width: 100px; height: 22px; border: 1px solid #777;"><img id="pbar" style="visibility: hidden" src="images/bar-on.gif" width="0" height="22" /></div>
<label id="lblBar" style="height: 22px; width: 100px; line-height: 22px; text-align: center">0%</label>
-->
</div>
<input id="txtWidth" type="text" value="100" /><input type="button" value="TOTAL WIDTH" onclick="setTotalWidth()" />
<input type="button" value="START" onclick="if(100==bar.per) bar.per=0;demo()" />
</body>
</html>


step 4
add dynamic width into class, and detach div, img, label to respective object.
add showText option, to show bar% text or not, because the valign present not well when it special width to the <div id="d1">

<html>
<head>
<title></title>
<style>

</style>
<script>
(function(){
var Pbar=function(id, totalWidth, showText){
this.per=0;
this.totalWidth=totalWidth;
this.div=id+"_pbarUtil_Bg";
this.bar=id+"_pbarUtil_Bar";
this.showText=showText;
this.barText=id+"_pbarUtil_BarText";
var txt="<div id='"+this.div+"' style='background: url(\"images/bar-bg.gif\") repeat-x; width: "+this.totalWidth+"; height: 22px; border: 1px solid #777;'><img id='"+this.bar+"' style='visibility: hidden' src='images/bar-on.gif' width='0' height='22' /></div>\n";
if(this.showText)
txt+="<label id='"+this.barText+"' style='height: 22px; width: "+this.totalWidth+"; line-height: 22px; text-align: center'>0%</label>";
var e=document.getElementById(id);
e.innerHTML=txt;
this.init();
}
Pbar.prototype.init=function(){
if(!this.showText)
return;
var d1=document.getElementById(this.div);
var d2=document.getElementById(this.barText);
var x=d1.offsetLeft, y=d1.offsetTop, w=d1.offsetWidth;
d2.style.position="absolute";
d2.style.left=x;
d2.style.top=y;
d2.style.width=w;
}
Pbar.prototype.processBar=function(n){
this.per+=n;
var e=document.getElementById(this.bar);
if(e.style.visibility=="hidden")
e.style.visibility="visible";
var total=document.getElementById(this.div).offsetWidth;
var value=this.per*0.01*total-2;
e.width=value;
if(this.showText){
var lbl=document.getElementById(this.barText);
lbl.innerHTML=this.per+"%";
}
if(this.per>=100)
return;
}
window.$Pbar=Pbar;
})();
var bar;
function setTotalWidth(){
//as per IE compatible, the auto bar% text can't valign middle well
//so set Pbar showText false, draw bar% with below span lblBar, custom x, y and style feedom
var w=document.getElementById("txtWidth").value;
bar=new $Pbar("d1", w, false);
//custom lblBar position
var d=document.getElementById("d1");
var e=document.getElementById("lblBar");
var x=d.offsetLeft, y=d.offsetTop;
e.style.position="absolute";
e.style.left=x+"px";
e.style.top=y+"px";
e.style.width=w;
}
function demo(){
if(bar.per>=100)
return;
bar.processBar(10);
var e=document.getElementById("lblBar");
e.innerHTML=bar.per+"%";
setTimeout(demo, 200);
}
</script>
</head>
<body>
<div>
<div id="d1" style="width: 60%;">
<!--keep inner empty-->
</div>
<!-- set Pbar showText false, draw bar% with below span lblBar, custom x, y and style feedom -->
<span id="lblBar" style="height: 22px; line-height: 22px; text-align: center">0%</span>
</div>
<br />
<hr size="1" />
<input id="txtWidth" type="text" value="100" /><input type="button" value="TOTAL WIDTH" onclick="setTotalWidth()" />
<input type="button" value="START" onclick="if(100==bar.per) bar.per=0;demo()" />
</body>
</html>


the result in IE, FF, Chrome, looks like no bad, hoho~

[img]http://dl.iteye.com/upload/attachment/547463/1b38412c-f8db-3d6b-ad4b-825ea797e532.jpg[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值