下载jqueryprogressbar.js
源码如下:
(function($) {
//Main Method
$.fn.reportprogress = function(val,maxVal) {
var max=100;
if(maxVal)
max=maxVal;
return this.each(
function(){
var div=$(this);
var innerdiv=div.find(".progress");
if(innerdiv.length!=1){
innerdiv=$("<div class='progress'></div>");
div.append("<div class='text'> </div>");
$("<span class='text'> </span>").css("width",div.width()).appendTo(innerdiv);
div.append(innerdiv);
}
var width=Math.round(val/max*100);
innerdiv.css("width",width+"%");
div.find(".text").html(width+" %");
}
);
};
})(jQuery);
页面代码:
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="jquery.js" type="text/javascript"></script>
<SCRIPT src="jqueryprogressbar.js" type="text/javascript"></SCRIPT>
<SCRIPT type=text/javascript>
var pct=0;
var handle=0;
function update(){
$("#progressbar").reportprogress(++pct);
if(pct==100){ //设定总长度值
clearInterval(handle);
$("#run").val("start");
pct=0;
}
}
jQuery(function($){
$("#run").click(
function(){
if(this.value=="start"){
handle=setInterval("update()",100); //设定增长的时间
this.value="stop";
}else{
clearInterval(handle);
this.value="start";
}
}
);
$("#reset").click(function(){
pct=0;
$("#progressbar").reportprogress(0);
});
});
</SCRIPT>
<STYLE>
#progressbar {
BORDER-RIGHT: black 1px solid;
BORDER-TOP: black 1px solid;
BORDER-LEFT: black 1px solid;
WIDTH: 200px;
COLOR: black;
BORDER-BOTTOM: black 1px solid;
POSITION: relative; HEIGHT: 20px
}
#progressbar DIV.progress {
OVERFLOW: hidden;
WIDTH: 0px;
POSITION: absolute;
HEIGHT: 100%;
BACKGROUND-COLOR: blue;
}
#progressbar DIV.progress .text {
COLOR: white;
POSITION: absolute;
TEXT-ALIGN: center; FONT-SIZE:larger;
}
#progressbar DIV.text {
WIDTH: 100%;
POSITION: absolute;
HEIGHT: 100%;
TEXT-ALIGN: center;
FONT-SIZE:larger;
}
</STYLE>
</HEAD>
<BODY>
<DIV id="progressbar">
</DIV>
<INPUT id="run" type="button" value="start"> <INPUT id="reset" type="button" value="reset">
</DIV>
</DIV>
</DIV>
</BODY>
</HTML>