number-pb.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. (function($) {
  2. var NumberProgressBar = function(element, options) {
  3. var settings = $.extend ({
  4. duration: 10000,
  5. percentage: 0,
  6. shownQuery: '.number-pb-shown',
  7. numQuery: '.number-pb-num',
  8. }, options || {});
  9. this.duration = settings.duration;
  10. this.last_percentage = -1;
  11. this.percentage = (settings.percentage >= 0 && settings.percentage <= 100) ? settings.percentage : 0;
  12. this.$element = $(element);
  13. this.width = this.$element.width();
  14. this.$shownBar = this.$element.find(settings.shownQuery);
  15. this.$num = this.$element.find(settings.numQuery);
  16. this.$progressFunction = settings.progressFunction;
  17. this.reach(this.percentage);
  18. }
  19. NumberProgressBar.prototype.reach = function(percentage) {
  20. if (this.last_percentage < 0) {
  21. this.last_percentage = 0;
  22. } else {
  23. this.last_percentage = this.percentage;
  24. }
  25. if (percentage < 0) {
  26. this.percentage = 0;
  27. } else if (percentage > 100) {
  28. this.percentage = 100;
  29. } else {
  30. this.percentage = percentage;
  31. }
  32. //console.log('reach: ', this.last_percentage, this.percentage, this.calDuration());
  33. this.moveShown();
  34. this.moveNum();
  35. }
  36. NumberProgressBar.prototype.calDuration = function() {
  37. return this.duration * Math.abs(this.percentage - this.last_percentage) / 100.0;
  38. }
  39. NumberProgressBar.prototype.moveShown = function() {
  40. //console.log('moveShown: ', this.percentage);
  41. this.$shownBar.velocity({
  42. width: this.percentage + '%'
  43. }, {
  44. duration: this.calDuration()
  45. })
  46. }
  47. NumberProgressBar.prototype.moveNum = function() {
  48. //console.log('moveNum: ', this.percentage);
  49. var self = this;
  50. var left = this.width * this.percentage / 100.0;
  51. var numWidth = this.$num.width();
  52. if (numWidth + left > this.width) {
  53. var percentage = (this.width - numWidth) / this.width * 100.0;
  54. } else {
  55. var percentage = this.percentage;
  56. }
  57. this.$num.velocity({
  58. left: percentage + '%'
  59. }, {
  60. duration: this.calDuration()
  61. })
  62. // number
  63. $({num: parseInt(this.$num.text())}).animate({
  64. num: this.percentage
  65. }, {
  66. queue: true,
  67. duration: self.calDuration(),
  68. step: function() {
  69. self.$num.text(Math.ceil(this.num) + '%');
  70. self.$progressFunction(this.num)
  71. },
  72. complete: function() {
  73. self.$num.text(self.percentage + '%');
  74. self.$progressFunction(this.num)
  75. }
  76. })
  77. }
  78. $.fn.NumberProgressBar = function(options) {
  79. return this.each(function () {
  80. var element = $(this);
  81. if (element.data('number-pb')) return;
  82. element.data('number-pb', new NumberProgressBar(this, options));
  83. })
  84. }
  85. $.fn.reach = function(percentage) {
  86. return this.each(function() {
  87. var element = $(this);
  88. var progressbar = element.data('number-pb');
  89. if (!progressbar) return;
  90. if (percentage < 0 || percentage > 100 || percentage == progressbar.percentage) return;
  91. progressbar.reach(percentage);
  92. })
  93. }
  94. })(jQuery);