Advertisement
Guest User

Untitled

a guest
Apr 10th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1.  
  2. /**
  3. * @name jQuery Countdown Plugin
  4. * @author Martin Angelov
  5. * @version 1.0
  6. * @url http://tutorialzine.com/2011/12/countdown-jquery/
  7. * @license MIT License
  8. */
  9.  
  10. (function($){
  11.  
  12. // Number of seconds in every time division
  13. var days = 24*60*60,
  14. hours = 60*60,
  15. minutes = 60;
  16.  
  17. // Creating the plugin
  18. $.fn.countdown = function(prop){
  19.  
  20. var options = $.extend({
  21. callback : function(){},
  22. timestamp : 0
  23. },prop);
  24.  
  25. var left, d, c, m, s, positions;
  26.  
  27. // Initialize the plugin
  28. init(this, options);
  29.  
  30. positions = this.find('.position');
  31.  
  32. (function tick(){
  33.  
  34. // Time left
  35. left = Math.floor((options.timestamp - (new Date())) / 1000);
  36.  
  37. if(left < 0){
  38. left = 0;
  39. }
  40. var posEx = 0;
  41. // Number of days left
  42. d = Math.floor(left / days);
  43.  
  44. if( d >= 100 ) {
  45. posEx = 1;
  46. updateTri(0, 1, 2, d);
  47. } else {
  48. updateDuo(0+posEx, 1+posEx, d);
  49. }
  50. left -= d*days;
  51.  
  52. // Number of hours left
  53. h = Math.floor(left / hours);
  54. updateDuo(2+posEx, 3+posEx, c);
  55. left -= h*hours;
  56.  
  57. // Number of minutes left
  58. m = Math.floor(left / minutes);
  59. updateDuo(4+posEx, 5+posEx, m1);
  60. left -= m*minutes;
  61.  
  62. // Number of seconds left
  63. s = left;
  64. updateDuo(6+posEx, 7+posEx, s);
  65.  
  66. // Calling an optional user supplied callback
  67. options.callback(d, h, m, s);
  68.  
  69. // Scheduling another call of this function in 1s
  70. setTimeout(tick, 1000);
  71. })();
  72.  
  73. // This function updates two digit positions at once
  74. function updateDuo(minor,major,value){
  75. switchDigit(positions.eq(minor),Math.floor(value/10)%10);
  76. switchDigit(positions.eq(major),value%10);
  77. }
  78.  
  79. // This function updates three digit positions at once
  80. function updateTri(first, second, third, value){
  81. switchDigit(positions.eq(first),Math.floor(value/100)%100);
  82. switchDigit(positions.eq(second),Math.floor(value/10)%10);
  83. switchDigit(positions.eq(third),value%10);
  84. }
  85.  
  86. return this;
  87. };
  88.  
  89.  
  90. function init(elem, options){
  91. elem.addClass('countdownHolder');
  92.  
  93. // Creating the markup inside the container
  94. $.each(['Days','Hours','Minutes','Seconds'],function(i){
  95. var boxName;
  96. if(this=="Days") {
  97. boxName = "д";
  98. }
  99. else if(this=="Hours") {
  100. boxName = "с";
  101. }
  102. else if(this=="Minutes") {
  103. boxName = "m";
  104. }
  105. else {
  106. boxName = "s";
  107. }
  108. left = Math.floor((options.timestamp - (new Date())) / 1000);
  109.  
  110. if(left < 0){
  111. left = 0;
  112. }
  113.  
  114. // Number of days left
  115. d = Math.floor(left / days);
  116. var html = '<div class="count'+this+'">';
  117. if( this=="Days" && d >= 100 ) {
  118. html += '<span class="position">' +
  119. '<span class="digit static">0</span>' +
  120. '</span>';
  121. }
  122. html += '<span class="position">' +
  123. '<span class="digit static">0</span>' +
  124. '</span>' +
  125. '<span class="position">' +
  126. '<span class="digit static">0</span>' +
  127. '</span>' +
  128. '<span class="boxName">' +
  129. '<span class="'+this+'">' + boxName + '</span>' +
  130. '</span>'
  131. $( html ).appendTo(elem);
  132.  
  133. // if(this!="Seconds"){
  134. // elem.append('<span class="points">:</span><span class="countDiv countDiv'+i+'"></span>');
  135. // }
  136. });
  137.  
  138. }
  139.  
  140. // Creates an animated transition between the two numbers
  141. function switchDigit(position,number){
  142.  
  143. var digit = position.find('.digit')
  144.  
  145. if(digit.is(':animated')){
  146. return false;
  147. }
  148.  
  149. if(position.data('digit') == number){
  150. // We are already showing this number
  151. return false;
  152. }
  153.  
  154. position.data('digit', number);
  155.  
  156. var replacement = $('<span>',{
  157. 'class':'digit',
  158. css:{
  159. top:0,
  160. opacity:0
  161. },
  162. html:number
  163. });
  164.  
  165. // The .static class is added when the animation
  166. // completes. This makes it run smoother.
  167.  
  168. digit
  169. .before(replacement)
  170. .removeClass('static')
  171. .animate({top:0,opacity:0},'fast',function(){
  172. digit.remove();
  173. })
  174.  
  175. replacement
  176. .delay(100)
  177. .animate({top:0,opacity:1},'fast',function(){
  178. replacement.addClass('static');
  179. });
  180. }
  181. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement