jquery.progressbar.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * jQuery Progress Bar plugin
  3. * Version 1.1.0 (06/20/2008)
  4. * @requires jQuery v1.2.1 or later
  5. *
  6. * Copyright (c) 2008 Gary Teo
  7. * http://t.wits.sg
  8. USAGE:
  9. $(".someclass").progressBar();
  10. $("#progressbar").progressBar();
  11. $("#progressbar").progressBar(45); // percentage
  12. $("#progressbar").progressBar({showText: false }); // percentage with config
  13. $("#progressbar").progressBar(45, {showText: false }); // percentage with config
  14. */
  15. (function($) {
  16. $.extend({
  17. progressBar: new function() {
  18. this.defaults = {
  19. increment : 2,
  20. speed : 15,
  21. showText : true, // show text with percentage in next to the progressbar? - default : true
  22. width : 120, // Width of the progressbar - don't forget to adjust your image too!!!
  23. boxImage : '/shares/jquery.progressbar/images/progressbar.gif', // boxImage : image around the progress bar
  24. barImage : {
  25. 0: '/shares/jquery.progressbar/images/progressbg_red.gif',
  26. 30: '/shares/jquery.progressbar/images/progressbg_orange.gif',
  27. 70: '/shares/jquery.progressbar/images/progressbg_green.gif'
  28. }, // Image to use in the progressbar. Can be a single image too: 'images/progressbg_green.gif'
  29. height : 12 // Height of the progressbar - don't forget to adjust your image too!!!
  30. };
  31. /* public methods */
  32. this.construct = function(arg1, arg2) {
  33. var argpercentage = null;
  34. var argconfig = null;
  35. if (arg1 != null) {
  36. if (!isNaN(arg1)) {
  37. argpercentage = arg1;
  38. if (arg2 != null) {
  39. argconfig = arg2; }
  40. } else {
  41. argconfig = arg1;
  42. }
  43. }
  44. return this.each(function(child) {
  45. var pb = this;
  46. if (argpercentage != null && this.bar != null && this.config != null) {
  47. this.config.tpercentage = argpercentage;
  48. if (argconfig != null)
  49. pb.config = $.extend(this.config, argconfig);
  50. } else {
  51. var $this = $(this);
  52. var config = $.extend({}, $.progressBar.defaults, argconfig);
  53. var percentage = argpercentage;
  54. if (argpercentage == null)
  55. var percentage = $this.html().replace("%",""); // parsed percentage
  56. $this.html("");
  57. var bar = document.createElement('img');
  58. var text = document.createElement('span');
  59. bar.id = this.id + "_percentImage";
  60. text.id = this.id + "_percentText";
  61. bar.title = percentage + "%";
  62. bar.alt = percentage + "%";
  63. bar.src = config.boxImage;
  64. bar.width = config.width;
  65. var $bar = $(bar);
  66. var $text = $(text);
  67. this.bar = $bar;
  68. this.ntext = $text;
  69. this.config = config;
  70. this.config.cpercentage = 0;
  71. this.config.tpercentage = percentage;
  72. $bar.css("width", config.width + "px");
  73. $bar.css("height", config.height + "px");
  74. $bar.css("background-image", "url(" + getBarImage(this.config.cpercentage, config) + ")");
  75. $bar.css("padding", "0");
  76. $bar.css("margin", "0");
  77. $this.append($bar);
  78. $this.append($text);
  79. }
  80. function getBarImage (percentage, config) {
  81. var image = config.barImage;
  82. if (typeof(config.barImage) == 'object') {
  83. for (var i in config.barImage) {
  84. if (percentage >= parseInt(i)) {
  85. image = config.barImage[i];
  86. } else { break; }
  87. }
  88. }
  89. return image;
  90. }
  91. /* modify by tangj 这个是创建进度条的愿脚本,带动画功能,但由于这个动画太耗性能,因此修改之
  92. * 修改成不带动画,直接显示进度条。修改代码在本函数下方
  93. var t = setInterval(function() {
  94. var config = pb.config;
  95. var cpercentage = parseInt(config.cpercentage);
  96. var tpercentage = parseInt(config.tpercentage);
  97. var increment = parseInt(config.increment);
  98. var bar = pb.bar;
  99. var text = pb.ntext;
  100. var pixels = config.width / 100; // Define how many pixels go into 1%
  101. bar.css("background-image", "url(" + getBarImage(cpercentage, config) + ")");
  102. // modify by tangj 当进度条长度不是120时,会产生偏离,需要计算偏离值,因此加入 - (120 - config.width)) 2012-11-07
  103. bar.css("background-position", (((config.width * -1)) + (cpercentage * pixels) - (120 - config.width)) + 'px 50%');
  104. if (config.showText)
  105. text.html(" " + Math.round(cpercentage) + "%");
  106. if (cpercentage > tpercentage) {
  107. if (cpercentage - increment < tpercentage) {
  108. pb.config.cpercentage = 0 + tpercentage
  109. } else {
  110. pb.config.cpercentage -= increment;
  111. }
  112. }
  113. else if (pb.config.cpercentage < pb.config.tpercentage) {
  114. if (cpercentage + increment > tpercentage) {
  115. pb.config.cpercentage = tpercentage
  116. } else {
  117. pb.config.cpercentage += increment;
  118. }
  119. }
  120. else {
  121. clearInterval(t);
  122. }
  123. }, pb.config.speed);
  124. */
  125. var t = function() {
  126. while(true){
  127. // alert(1)
  128. var config = pb.config;
  129. var cpercentage = parseInt(config.cpercentage);
  130. var tpercentage = parseInt(config.tpercentage);
  131. var increment = parseInt(config.increment);
  132. var bar = pb.bar;
  133. var text = pb.ntext;
  134. var pixels = config.width / 100; // Define how many pixels go into 1%
  135. bar.css("background-image", "url(" + getBarImage(cpercentage, config) + ")");
  136. // modify by tangj 当进度条长度不是120时,会产生偏离,需要计算偏离值,因此加入 - (120 - config.width)) 2012-11-07
  137. bar.css("background-position", (((config.width * -1)) + (cpercentage * pixels) - (120 - config.width)) + 'px 50%');
  138. if (config.showText)
  139. text.html(" " + Math.round(cpercentage) + "%");
  140. if (cpercentage > tpercentage) {
  141. if (cpercentage - increment < tpercentage) {
  142. pb.config.cpercentage = 0 + tpercentage
  143. } else {
  144. pb.config.cpercentage -= increment;
  145. }
  146. }
  147. else if (pb.config.cpercentage < pb.config.tpercentage) {
  148. if (cpercentage + increment > tpercentage) {
  149. pb.config.cpercentage = tpercentage
  150. } else {
  151. pb.config.cpercentage += increment;
  152. }
  153. }
  154. else {
  155. break;
  156. }
  157. }
  158. };
  159. t();
  160. });
  161. };
  162. }
  163. });
  164. $.fn.extend({
  165. progressBar: $.progressBar.construct
  166. });
  167. })(jQuery);