第一種方法:
Loading.js
復制代碼 代碼如下:
//頻率
var frequency = 50;
//步長
var step = 3;
//背景顏色
var loadingBgcolor = "#ffffff";
//寬度
var loadingWidth = 354;
/*
*參數說明:
*content:顯示內容,可以為空;
*imageURL:將引用JS文件的路徑設置即可;
*left:進度條顯示位置left
*top:進度條顯示位置top
*/
function Loading(content, imageURL, left, top)
{
imageURL = imageURL + "Loading.jpg";
LoadTable(content, imageURL, left, top);
showimage.style.display="";
window.setInterval("RefAct();", frequency);
}
function RefAct()
{
imgAct.width += step;
if(imgAct.width > loadingWidth-4)
{
imgAct.width = 0;
}
}
function LoadTable(content, imageURL, left, top)
{
var strLoading;
strLoading = "";
strLoading += "<div id=\"showimage\" style=\"DISPLAY:none;Z-INDEX:100;LEFT:" + left+ "px;POSITION:absolute;TOP:" + top+ "px;\" align=\"center\">";
strLoading += "<TABLE id=\"Table1\" cellSpacing=\"0\" cellPadding=\"0\" width=\"" + loadingWidth + "\" border=\"0\" bgcolor=\"" + loadingBgcolor+ "\">";
if(content != "")
{
strLoading += "<tr>";
strLoading += "<td align=\"center\">";
strLoading += "<font size=\"4\" face=\"Courier New, Courier, mono\"><strong>" + content + "</strong></font>";
strLoading += "</td>";
strLoading += "</tr>";
strLoading += "<TR>";
}
strLoading += "<TD class=\"Loading\" height=\"8\">";
strLoading += "<IMG id=\"imgAct\" height=\"8\" alt=\"\" src=\"" + imageURL + "\" width=\"0\">";
strLoading += "</TD>";
strLoading += "</TR>";
strLoading += "</TABLE>";
strLoading += "</div>";
document.getElementById("loading_div").innerHTML = strLoading;
}
使用頁:
復制代碼 代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script src="LoadingJS/Loading.js" type="text/javascript"></script>
</head>
<body>
<input type="button" name="Button" value="Button" onclick="Loading('顯示標簽內容', 'LoadingJS/', 20, 100)">
<div id="loading_div" ></div>
</body>
</html>
第二種方法:
JS實現進度顯示功能
復制代碼 代碼如下:
progress.js類文件:
$ = function (id) {
return document.getElementById(id);
}
//文本轉JSON字符串
String.prototype.toJson = function () {
if (this == ) {
return null;
}
else {
try {
return eval(( + this.replace(/rn/g, rn) + ));
}
catch (err) {
}
}
};
//字符格式化方法
String.prototype.format = function () {
var newStr = this;
var reg = null;
for (var i = 0; i < arguments.length; i++) {
reg = RegExp('{' + (i) + '}', 'gm');
newStr = newStr.replace(reg, arguments[i]);
}
return newStr;
};
//進度條類
function Progress(objId) {
//window.setInterval對象
this.interval = null;
//進度條對象ID
this.id = objId;
//當前進度 起始進度為0
this.progress = 0;
//進度條顯示進度的DIV ID
this.progressId = inner + this.id;
//進度條邊框ID
this.progressFrameId = frame + this.id;
//進度條類參數配置
this.config = {
//寬度
width: 150,
//高度
height: 20,
//左邊距
left: 0,
//頂部邊距
top: 0,
//進度顏色
progressColor: red,
//邊框顏色
borderColor: #ccc,
//邊框寬度
borderHeight: 2,
//進度完成後間隔N秒後隱藏進度條 0為立即隱藏
hiddenSplit:2000
};
}
//進度條類初始化方法
Progress.prototype.init = function () {
//新建進度條邊框DIV
var progressdiv = document.createElement(div);
//邊框DIV樣式
var progressdiv_css_text = position:absolute;width:{0}px;height:{1}px;left:{2}px;top:{3}px;border:{4} {5}px solid;.format
(
this.config.width,
this.config.height,
this.config.left,
this.config.top,
this.config.borderColor,
this.config.borderHeight
);
//重置進度為0
this.progress = 0;
//設置邊框ID
progressdiv.id = this.progressFrameId;
//設置邊框樣式
progressdiv.style.cssText = progressdiv_css_text;
//設置進度條HTML
progressdiv.innerHTML = '
'.format(this.progressId, this.config.height, this.config.progressColor);
//加入body中
document.body.appendChild(progressdiv);
};
//進度條隱藏函數
Progress.prototype.hiddenProgress = function () {
document.body.removeChild(document.getElementById(this.progressFrameId));
window.clearInterval(this.interval);
}
//進度結束時觸發的函數
Progress.prototype.complete = function () {
window.clearInterval(this.interval);
this.interval = window.setTimeout(this.id+.hiddenProgress();,this.config.hiddenSplit);
if (this.completeCallBack) {
this.completeCallBack();
}
}
//進度每次運行後的回調函數
Progress.prototype.callback = function () {
var progressDiv = document.getElementById(this.progressId);
var progressFrameDiv = document.getElementById(this.progressFrameId);
progressDiv.innerHTML = (this.progress*100)+ %;
progressFrameDiv.title = 上傳進度: + (this.progress*100) + %;
progressDiv.style.width = (this.progress*100) + %;
if (this.progress >= 1) {
this.complete();
progressDiv.innerHTML = 文件上傳成功!;
}
}
//進度條運行函數。需要用戶自己實現
Progress.prototype.run = function () {
alert(請實現 + this.id + .run方法以實現異步進度請求,回發回請調回調 + this.id + .callback方法。);
window.clearInterval(this.interval);
}
//啟動進度
Progress.prototype.start = function () {
this.init();
this.interval = window.setInterval(this.id + .run(), 1000);
}
圖片加載進度實時顯示
復制代碼 代碼如下:
<script>
var l=0;
var imgs;
var sum=0;
var imgs=new Array();
function chk(){
l--;
document.getElementById("aa").innerText=""+((sum-l)*100/sum)+"%"
if (l==0){
for (var i=0;i<sum;i++)
document.body.innerHTML+="<img src='"+imgs[i].src+"'>"
}
}
if (document.images){
imgs[0]=new Image()
imgs[1]=new Image()
imgs[2]=new Image()
imgs[3]=new Image()
imgs[4]=new Image()
imgs[5]=new Image()
imgs[6]=new Image()
imgs[7]=new Image()
imgs[0].src="/articleimg/2006/08/3859/01.jpg";
imgs[1].src="/articleimg/2006/08/3859/02.jpg";
imgs[2].src="/articleimg/2006/08/3859/03.jpg";
imgs[3].src="/articleimg/2006/08/3859/04.jpg";
imgs[4].src="/articleimg/2006/08/3859/05.jpg";
imgs[5].src="/articleimg/2006/08/3859/06.jpg";
imgs[6].src="/articleimg/2006/08/3859/07.jpg";
imgs[7].src="/articleimg/2006/08/3859/08.jpg";
}
</script>
<body>
<div id="aa">0%</div>
<script>
sum=l=imgs.length;
for (var i=0;i<l;i++){
imgs[i].onload=chk;
imgs[i].onerror=chk;//無論圖片是否加載成功,都執行指定方法
}
</script>
</body>