Advertisement
imranhs

Counter

Aug 15th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. Making Count
  2.  
  3. HTML:_____________________________
  4.  
  5. <div class="counter-area">
  6. <div class="container">
  7. <div class="row">
  8. <div class="counter col-sm-3">
  9. <i class="fa fa-code fa-2x"></i>
  10. <h2 class="timer count-title count-number" data-to="300" data-speed="2500">300</h2>
  11. <p class="count-text ">SomeText GoesHere</p>
  12. </div>
  13.  
  14. <div class="counter col-sm-3">
  15. <i class="fa fa-coffee fa-2x"></i>
  16. <h2 class="timer count-title count-number" data-to="17870" data-speed="2500">17870</h2>
  17. <p class="count-text ">SomeText GoesHere</p>
  18. </div>
  19.  
  20. <div class="counter col-sm-3">
  21. <i class="fa fa-lightbulb-o fa-2x"></i>
  22. <h2 class="timer count-title count-number" data-to="847" data-speed="2500">847</h2>
  23. <p class="count-text ">SomeText GoesHere</p>
  24. </div>
  25.  
  26. <div class="counter col-sm-3">
  27. <i class="fa fa-bug fa-2x"></i>
  28. <h2 class="timer count-title count-number" data-to="157" data-speed="2500">157</h2>
  29. <p class="count-text ">SomeText GoesHere</p>
  30. </div>
  31. </div>
  32. </div>
  33. </div>
  34.  
  35.  
  36. CSS:______________________
  37.  
  38. .counter {
  39. background-color:#ffffff;
  40. padding:20px 0;
  41. border-radius: 5px;
  42. text-align: center;
  43. }
  44. .count-title {
  45. font-size: 40px;
  46. font-weight: normal;
  47. margin-top: 10px;
  48. margin-bottom: 0;
  49. }
  50. .count-text {
  51. font-size: 13px;
  52. font-weight: normal;
  53. margin-top: 10px;
  54. margin-bottom: 0;
  55. }
  56. .fa-2x {
  57. margin: 0 auto;
  58. color: #4ad1e5;
  59. }
  60.  
  61.  
  62.  
  63. Script:___________________script ta body ar thik upore rakhte hobe nahole kaj korbena
  64.  
  65.  
  66.  
  67. (function ($) {
  68. $.fn.countTo = function (options) {
  69. options = options || {};
  70.  
  71. return $(this).each(function () {
  72. // set options for current element
  73. var settings = $.extend({}, $.fn.countTo.defaults, {
  74. from: $(this).data('from'),
  75. to: $(this).data('to'),
  76. speed: $(this).data('speed'),
  77. refreshInterval: $(this).data('refresh-interval'),
  78. decimals: $(this).data('decimals')
  79. }, options);
  80.  
  81. // how many times to update the value, and how much to increment the value on each update
  82. var loops = Math.ceil(settings.speed / settings.refreshInterval),
  83. increment = (settings.to - settings.from) / loops;
  84.  
  85. // references & variables that will change with each update
  86. var self = this,
  87. $self = $(this),
  88. loopCount = 0,
  89. value = settings.from,
  90. data = $self.data('countTo') || {};
  91.  
  92. $self.data('countTo', data);
  93.  
  94. // if an existing interval can be found, clear it first
  95. if (data.interval) {
  96. clearInterval(data.interval);
  97. }
  98. data.interval = setInterval(updateTimer, settings.refreshInterval);
  99.  
  100. // initialize the element with the starting value
  101. render(value);
  102.  
  103. function updateTimer() {
  104. value += increment;
  105. loopCount++;
  106.  
  107. render(value);
  108.  
  109. if (typeof(settings.onUpdate) == 'function') {
  110. settings.onUpdate.call(self, value);
  111. }
  112.  
  113. if (loopCount >= loops) {
  114. // remove the interval
  115. $self.removeData('countTo');
  116. clearInterval(data.interval);
  117. value = settings.to;
  118.  
  119. if (typeof(settings.onComplete) == 'function') {
  120. settings.onComplete.call(self, value);
  121. }
  122. }
  123. }
  124.  
  125. function render(value) {
  126. var formattedValue = settings.formatter.call(self, value, settings);
  127. $self.html(formattedValue);
  128. }
  129. });
  130. };
  131.  
  132. $.fn.countTo.defaults = {
  133. from: 0, // the number the element should start at
  134. to: 0, // the number the element should end at
  135. speed: 1000, // how long it should take to count between the target numbers
  136. refreshInterval: 100, // how often the element should be updated
  137. decimals: 0, // the number of decimal places to show
  138. formatter: formatter, // handler for formatting the value before rendering
  139. onUpdate: null, // callback method for every time the element is updated
  140. onComplete: null // callback method for when the element finishes updating
  141. };
  142.  
  143. function formatter(value, settings) {
  144. return value.toFixed(settings.decimals);
  145. }
  146. }(jQuery));
  147.  
  148. jQuery(function ($) {
  149. // custom formatting example
  150. $('.count-number').data('countToOptions', {
  151. formatter: function (value, options) {
  152. return value.toFixed(options.decimals).replace(/\B(?=(?:\d{3})+(?!\d))/g, ',');
  153. }
  154. });
  155.  
  156. // start all the timers
  157. $('.timer').each(count);
  158.  
  159. function count(options) {
  160. var $this = $(this);
  161. options = $.extend({}, options || {}, $this.data('countToOptions') || {});
  162. $this.countTo(options);
  163. }
  164. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement