jquery.progression.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Progression - jQuery plugin for Progress Bar 1.3
  3. *
  4. * http://www.anthor.net/fr/jquery-progression.html
  5. *
  6. * Copyright (c) 2008 FOURNET Loïc
  7. *
  8. * Dual licensed under the MIT and GPL licenses:
  9. * http://www.opensource.org/licenses/mit-license.php
  10. * http://www.gnu.org/licenses/gpl.html
  11. *
  12. */
  13. (function($) {
  14. $.fn.progression = function(options) {
  15. // Récupération des options par défaut
  16. var opts = $.extend({
  17. Current: 50,
  18. Maximum: 100,
  19. Background: '#FFFFFF',
  20. TextColor: '#000000',
  21. aBackground: '#FF0000',
  22. aTextColor: '#FFFFFF',
  23. BorderColor: '#000000',
  24. Animate: true,
  25. AnimateTimeOut: 3000,
  26. Easing: 'linear',
  27. startFct : null,
  28. endFct : null
  29. }, $.fn.progression.defaults, options);
  30. if(options)
  31. var newCurrent = options.Current;
  32. // Boucle sur les éléments appelés
  33. return this.each(function() {
  34. $this = $(this); // Elément en cours
  35. $innerdiv=$this.find(".progress"); // On recherche si l'élément a déjà été traité
  36. // Options Spécifiques + Métadata ?
  37. var o = $.metadata ? $.extend({}, opts, $this.metadata()) : opts;
  38. // Premier traitement de l'élément, pour la mise en place
  39. if($innerdiv.length!=1)
  40. BuildBarre($this, o);
  41. else
  42. {
  43. // Si c'est une nouvelle valeur, la fonction doit avoir la priorité sur les métadata
  44. if(newCurrent)
  45. o.Current = newCurrent;
  46. o.Maximum = parseInt($this.attr('pmax'));
  47. }
  48. // Valeur supérieur au maximum ?
  49. if( o.Current > o.Maximum )
  50. {
  51. debug('La valeur demandee doit etre inférieur ou egale a la valeur maximale.')
  52. return false;
  53. }
  54. // Calcul du pourcentage actuel
  55. var aWidth = Math.round(parseInt($this.attr('pcur'))/o.Maximum*100);
  56. // Calcul du nouveaux pourcentage
  57. var Width = Math.round(parseInt(o.Current)/o.Maximum*100);
  58. //Start Callback
  59. if (typeof o.startFct == 'function')
  60. o.startFct(o);
  61. if(o.Animate)
  62. {
  63. var oldCurrent = parseInt($this.attr('pcur'));
  64. var Steps = Math.abs(oldCurrent - o.Current);
  65. var StepsTimeOut = Math.floor(o.AnimateTimeOut/o.Maximum);
  66. $innerdiv.queue("fx", []);
  67. $innerdiv.stop();
  68. $innerdiv.animate({ width: Width+"%" }, {
  69. duration: Math.round(StepsTimeOut*(Steps+1)),
  70. queue: false,
  71. easing: o.Easing,
  72. complete: function(){
  73. if (typeof o.endFct == 'function')
  74. o.endFct(o);
  75. }
  76. });
  77. for (i=0; i<=Steps; i++) {
  78. $innerdiv.animate({opacity: 1},{
  79. duration: Math.round(StepsTimeOut*i),
  80. queue: false,
  81. complete: function(){
  82. if(oldCurrent<=o.Current)
  83. $(this).progressionSetTextTo(oldCurrent++);
  84. else
  85. $(this).progressionSetTextTo(oldCurrent--);
  86. }
  87. });
  88. }
  89. }
  90. else
  91. {
  92. $innerdiv.css({ width: Width+'%' });
  93. $innerdiv.progressionSetTextTo(o.Current);
  94. if (typeof o.endFct == 'function')
  95. o.endFct(o);
  96. }
  97. });
  98. };
  99. // Fonction de création de la barre
  100. function BuildBarre($this, o) {
  101. // On vide la barre
  102. $this.html('');
  103. $this.css({
  104. textAlign: 'left',
  105. position: 'relative',
  106. overflow: 'hidden',
  107. backgroundColor: o.Background,
  108. borderColor: o.BorderColor,
  109. color: o.TextColor
  110. });
  111. // Si largeur Spécifique
  112. if(o.Width)
  113. $this.css('width', o.Width);
  114. // Si hauteur Spécifique
  115. if(o.Height)
  116. $this.css({ height: o.Height, lineHeight: o.Height });
  117. // Si image de fond
  118. if(o.BackgroundImg)
  119. $this.css({ backgroundImage: 'url(' + o.BackgroundImg + ')' });
  120. $innerdiv=$("<div class='progress'></div>");
  121. $("<div class='text'>&nbsp;</div>").css({
  122. position: 'absolute',
  123. width: '100%',
  124. height: '100%',
  125. textAlign: 'center'
  126. }).appendTo($this);
  127. $("<span class='text'>&nbsp;</span>")
  128. .css({
  129. position: 'absolute',
  130. width: $this.width(),
  131. textAlign: 'center'
  132. })
  133. .appendTo($innerdiv);
  134. $this.append($innerdiv);
  135. // On applique le CSS de $innerdiv
  136. $innerdiv.css({
  137. position: 'absolute',
  138. width: 0,
  139. height: '100%',
  140. overflow: 'hidden',
  141. backgroundColor: o.aBackground,
  142. color: o.aTextColor
  143. });
  144. // Si image de fond active
  145. if(o.aBackgroundImg)
  146. $innerdiv.css({ backgroundImage: 'url(' + o.aBackgroundImg + ')' });
  147. $this.attr('pmax', o.Maximum);
  148. $this.attr('pcur', 0);
  149. };
  150. // Fonction pour aller à une valeur précise
  151. $.fn.progressionSetTextTo = function(i) {
  152. return this.each(function() {
  153. $this = $(this).parent();
  154. if($this.attr('pmax')!=100)
  155. $this.find(".text").html(i+"/"+$this.attr('pmax'));
  156. else
  157. $this.find(".text").html(i+" %");
  158. $this.attr('pcur', i);
  159. });
  160. };
  161. // Fonction d'impression dans la console javascript
  162. function debug($txt) {
  163. if (window.console && window.console.log)
  164. window.console.log('jQuery Progression: ' + $txt);
  165. };
  166. $.fn.progression.defaults = {};
  167. })(jQuery);