Discuz教程网

JS 进度条效果实现代码整理

[复制链接]
authicon 星火燎原 发表于 2011-5-21 22:25:41 | 显示全部楼层 |阅读模式
进度条效果实现代码,有助于缓解页面显示慢的页面,给用户一个等待时间的效果
第一种方法:
Loading.js
代码如下:
  1. //频率
  2. var frequency = 50;
  3. //步长
  4. var step = 3;
  5. //背景颜色
  6. var loadingBgcolor = "#ffffff";
  7. //宽度
  8. var loadingWidth = 354;

  9. /*
  10. *参数说明:
  11. *content:显示内容,可以为空;
  12. *imageURL:将引用JS文件的路径设置即可;
  13. *left:进度条显示位置left
  14. *top:进度条显示位置top
  15. */
  16. function Loading(content, imageURL, left, top)
  17. {
  18. imageURL = imageURL + "Loading.jpg";

  19. LoadTable(content, imageURL, left, top);
  20. showimage.style.display="";
  21. window.setInterval("RefAct();", frequency);
  22. }

  23. function RefAct()
  24. {
  25. imgAct.width += step;
  26. if(imgAct.width > loadingWidth-4)
  27. {
  28. imgAct.width = 0;
  29. }
  30. }

  31. function LoadTable(content, imageURL, left, top)
  32. {
  33. var strLoading;
  34. strLoading = "";
  35. strLoading += "<div id="showimage" style="DISPLAY:none;Z-INDEX:100;LEFT:" + left+ "px;POSITION:absolute;TOP:" + top+ "px;" align="center">";
  36. strLoading += "<TABLE id="Table1" cellSpacing="0" cellPadding="0" width="" + loadingWidth + "" border="0" bgcolor="" + loadingBgcolor+ "">";
  37. if(content != "")
  38. {
  39. strLoading += "<tr>";
  40. strLoading += "<td align="center">";
  41. strLoading += "<font size="4" face="Courier New, Courier, mono"><strong>" + content + "</strong></font>";
  42. strLoading += "</td>";
  43. strLoading += "</tr>";
  44. strLoading += "<TR>";
  45. }
  46. strLoading += "<TD class="Loading" height="8">";
  47. strLoading += "<IMG id="imgAct" height="8" alt="" src="" + imageURL + "" width="0">";
  48. strLoading += "</TD>";
  49. strLoading += "</TR>";
  50. strLoading += "</TABLE>";
  51. strLoading += "</div>";

  52. document.getElementById("loading_div").innerHTML = strLoading;
  53. }

复制代码

使用页:
代码如下:
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>Untitled Document</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  6. <script src="LoadingJS/Loading.js" type="text/javascript"></script>

  7. </head>

  8. <body>
  9. <input type="button" name="Button" value="Button" onclick="Loading('显示标签内容', 'LoadingJS/', 20, 100)">
  10. <div id="loading_div" ></div>
  11. </body>
  12. </html>

复制代码

第二种方法:
JS实现进度显示功能
代码如下:
progress.js类文件:

  1. $ = function (id) {
  2. return document.getElementById(id);
  3. }
  4. //文本转JSON字符串
  5. String.prototype.toJson = function () {
  6. if (this == ) {
  7. return null;
  8. }
  9. else {
  10. try {
  11. return eval(( + this.replace(/rn/g, rn) + ));
  12. }
  13. catch (err) {
  14. }
  15. }
  16. };
  17. //字符格式化方法
  18. String.prototype.format = function () {
  19. var newStr = this;
  20. var reg = null;
  21. for (var i = 0; i < arguments.length; i++) {
  22. reg = RegExp('{' + (i) + '}', 'gm');
  23. newStr = newStr.replace(reg, arguments);
  24. }
  25. return newStr;
  26. };
  27. //进度条类
  28. function Progress(objId) {
  29. //window.setInterval对象
  30. this.interval = null;
  31. //进度条对象ID
  32. this.id = objId;
  33. //当前进度 起始进度为0
  34. this.progress = 0;
  35. //进度条显示进度的DIV ID
  36. this.progressId = inner + this.id;
  37. //进度条边框ID
  38. this.progressFrameId = frame + this.id;
  39. //进度条类参数配置
  40. this.config = {
  41. //宽度
  42. width: 150,
  43. //高度
  44. height: 20,
  45. //左边距
  46. left: 0,
  47. //顶部边距
  48. top: 0,
  49. //进度颜色
  50. progressColor: red,
  51. //边框颜色
  52. borderColor: #ccc,
  53. //边框宽度
  54. borderHeight: 2,
  55. //进度完成后间隔N秒后隐藏进度条 0为立即隐藏
  56. hiddenSplit:2000
  57. };
  58. }
  59. //进度条类初始化方法
  60. Progress.prototype.init = function () {
  61. //新建进度条边框DIV
  62. var progressdiv = document.createElement(div);
  63. //边框DIV样式
  64. var progressdiv_css_text = position:absolute;width:{0}px;height:{1}px;left:{2}px;top:{3}px;border:{4} {5}px solid;.format
  65. (
  66. this.config.width,
  67. this.config.height,
  68. this.config.left,
  69. this.config.top,
  70. this.config.borderColor,
  71. this.config.borderHeight
  72. );
  73. //重置进度为0
  74. this.progress = 0;
  75. //设置边框ID
  76. progressdiv.id = this.progressFrameId;
  77. //设置边框样式
  78. progressdiv.style.cssText = progressdiv_css_text;
  79. //设置进度条HTML
  80. progressdiv.innerHTML = '

  81. '.format(this.progressId, this.config.height, this.config.progressColor);
  82. //加入body中
  83. document.body.appendChild(progressdiv);
  84. };
  85. //进度条隐藏函数
  86. Progress.prototype.hiddenProgress = function () {
  87. document.body.removeChild(document.getElementById(this.progressFrameId));
  88. window.clearInterval(this.interval);
  89. }
  90. //进度结束时触发的函数
  91. Progress.prototype.complete = function () {
  92. window.clearInterval(this.interval);
  93. this.interval = window.setTimeout(this.id+.hiddenProgress();,this.config.hiddenSplit);
  94. if (this.completeCallBack) {
  95. this.completeCallBack();
  96. }
  97. }
  98. //进度每次运行后的回调函数
  99. Progress.prototype.callback = function () {
  100. var progressDiv = document.getElementById(this.progressId);
  101. var progressFrameDiv = document.getElementById(this.progressFrameId);
  102. progressDiv.innerHTML = (this.progress*100)+ %;
  103. progressFrameDiv.title = 上传进度: + (this.progress*100) + %;
  104. progressDiv.style.width = (this.progress*100) + %;
  105. if (this.progress >= 1) {
  106. this.complete();
  107. progressDiv.innerHTML = 文件上传成功!;
  108. }
  109. }
  110. //进度条运行函数。需要用户自己实现
  111. Progress.prototype.run = function () {
  112. alert(请实现 + this.id + .run方法以实现异步进度请求,回发回请调回调 + this.id + .callback方法。);
  113. window.clearInterval(this.interval);
  114. }
  115. //启动进度
  116. Progress.prototype.start = function () {
  117. this.init();
  118. this.interval = window.setInterval(this.id + .run(), 1000);
  119. }

复制代码

图片加载进度实时显示
代码如下:
  1. <script>
  2. var l=0;
  3. var imgs;
  4. var sum=0;
  5. var imgs=new Array();
  6. function chk(){
  7. l--;
  8. document.getElementById("aa").innerText=""+((sum-l)*100/sum)+"%"
  9. if (l==0){
  10. for (var i=0;i<sum;i++)
  11. document.body.innerHTML+="<img src='"+imgs[i].src+"'>"
  12. }
  13. }
  14. if (document.images){
  15. imgs[0]=new Image()
  16. imgs[1]=new Image()
  17. imgs[2]=new Image()
  18. imgs[3]=new Image()
  19. imgs[4]=new Image()
  20. imgs[5]=new Image()
  21. imgs[6]=new Image()
  22. imgs[7]=new Image()
  23. imgs[0].src="/articleimg/2006/08/3859/01.jpg";
  24. imgs[1].src="/articleimg/2006/08/3859/02.jpg";
  25. imgs[2].src="/articleimg/2006/08/3859/03.jpg";
  26. imgs[3].src="/articleimg/2006/08/3859/04.jpg";
  27. imgs[4].src="/articleimg/2006/08/3859/05.jpg";
  28. imgs[5].src="/articleimg/2006/08/3859/06.jpg";
  29. imgs[6].src="/articleimg/2006/08/3859/07.jpg";
  30. imgs[7].src="/articleimg/2006/08/3859/08.jpg";
  31. }

  32. </script>
  33. <body>
  34. <div id="aa">0%</div>
  35. <script>
  36. sum=l=imgs.length;
  37. for (var i=0;i<l;i++){
  38. imgs[i].onload=chk;
  39. imgs[i].onerror=chk;//无论图片是否加载成功,都执行指定方法
  40. }
  41. </script>
  42. </body>
复制代码




上一篇:Discuz! X2 社会化登录插件(5.12更新QQ、MSN、淘宝、支付宝、雅虎登录)
下一篇:PHP header()函数的应用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-1 23:36

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表