Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. (function() {
  2. (function($) {
  3. $.countdown = function(el, options) {
  4. var getDateData,
  5. _this = this;
  6. this.el = el;
  7. this.$el = $(el);
  8. this.$el.data("countdown", this);
  9. this.init = function() {
  10. _this.options = $.extend({}, $.countdown.defaultOptions, options);
  11. if (_this.options.refresh) {
  12. _this.interval = setInterval(function() {
  13. return _this.render();
  14. }, _this.options.refresh);
  15. }
  16. _this.render();
  17. return _this;
  18. };
  19. getDateData = function(endDate) {
  20. var dateData, diff;
  21. endDate = Date.parse($.isPlainObject(_this.options.date) ? _this.options.date : new Date(_this.options.date));
  22. diff = (endDate - Date.parse(new Date)) / 1000;
  23. if (diff <= 0) {
  24. diff = 0;
  25. if (_this.interval) {
  26. _this.stop();
  27. }
  28. _this.options.onEnd.apply(_this);
  29. }
  30. dateData = {
  31. years: 0,
  32. days: 0,
  33. hours: 0,
  34. min: 0,
  35. sec: 0,
  36. millisec: 0
  37. };
  38. if (diff >= (365.25 * 86400)) {
  39. dateData.years = Math.floor(diff / (365.25 * 86400));
  40. diff -= dateData.years * 365.25 * 86400;
  41. }
  42. if (diff >= 86400) {
  43. dateData.days = Math.floor(diff / 86400);
  44. diff -= dateData.days * 86400;
  45. }
  46. if (diff >= 3600) {
  47. dateData.hours = Math.floor(diff / 3600);
  48. diff -= dateData.hours * 3600;
  49. }
  50. if (diff >= 60) {
  51. dateData.min = Math.floor(diff / 60);
  52. diff -= dateData.min * 60;
  53. }
  54. dateData.sec = diff;
  55. return dateData;
  56. };
  57. this.leadingZeros = function(num, length) {
  58. if (length == null) {
  59. length = 2;
  60. }
  61. num = String(num);
  62. while (num.length < length) {
  63. num = "0" + num;
  64. }
  65. return num;
  66. };
  67. this.update = function(newDate) {
  68. _this.options.date = newDate;
  69. return _this;
  70. };
  71. this.render = function() {
  72. _this.options.render.apply(_this, [getDateData(_this.options.date)]);
  73. return _this;
  74. };
  75. this.stop = function() {
  76. if (_this.interval) {
  77. clearInterval(_this.interval);
  78. }
  79. _this.interval = null;
  80. return _this;
  81. };
  82. this.start = function(refresh) {
  83. if (refresh == null) {
  84. refresh = _this.options.refresh || $.countdown.defaultOptions.refresh;
  85. }
  86. if (_this.interval) {
  87. clearInterval(_this.interval);
  88. }
  89. _this.render();
  90. _this.options.refresh = refresh;
  91. _this.interval = setInterval(function() {
  92. return _this.render();
  93. }, _this.options.refresh);
  94. return _this;
  95. };
  96. return this.init();
  97. };
  98. $.countdown.defaultOptions = {
  99. date: "June 7, 2087 15:03:25",
  100. refresh: 1000,
  101. onEnd: $.noop,
  102. render: function(date) {
  103. return $(this.el).html("" + date.years + " years, " + date.days + " days, " + (this.leadingZeros(date.hours)) + " hours, " + (this.leadingZeros(date.min)) + " min and " + (this.leadingZeros(date.sec)) + " sec");
  104. }
  105. };
  106. $.fn.countdown = function(options) {
  107. return $.each(this, function(i, el) {
  108. var $el;
  109. $el = $(el);
  110. if (!$el.data('countdown')) {
  111. return $el.data('countdown', new $.countdown(el, options));
  112. }
  113. });
  114. };
  115. return void 0;
  116. })(jQuery);
  117.  
  118. }).
  119.  
  120. call(this);
  121.  
  122. $(document).ready(function() {
  123. $(function() {
  124. $('.date').countdown({
  125. date: "september 26, 2015 17:00:00",
  126. render: function(data) {
  127. $(this.el).html("<div class='date-block'><div class='hex-bg'></div><span class='count'>" + this.leadingZeros(data.days, 3) + "</span><span class='count2'>days</span></div><div class='date-block'><div class='hex-bg'></div><span class='count'>" +
  128. this.leadingZeros(data.hours, 2) + "</span><span class='count2'>hours</span></div><div class='date-block'><div class='hex-bg'></div><span class='count'>" +
  129. this.leadingZeros(data.min, 2) + "</span><span class='count2'>minutes</span></div><div class='date-block'><div class='hex-bg'></div><span class='count'>" +
  130. this.leadingZeros(data.sec, 2) + "</span><span class='count2'>seconds</span></div>");
  131. }
  132.  
  133. });
  134. });
  135. });
  136.  
  137. $(window).load(function () {
  138. $('.hex-bg').load('images/hexagon.svg');
  139. })
  140.  
  141. .date {
  142. width: 700px;
  143. padding: 50px 0 160px 0;
  144. margin: 0 auto;
  145. text-align: center;
  146. }
  147.  
  148. .date-block {
  149. position: relative;
  150. width: 150px;
  151. height:170px;
  152. display: inline-block;
  153. margin-left: 40px;
  154. font-size: 40px;
  155. font-weight: 400;
  156. line-height: 1;
  157. text-align: center;
  158. }
  159.  
  160. .hex-bg {
  161. position: absolute;
  162. margin: auto;
  163. top: 0;
  164. bottom: 0;
  165. left: 0;
  166. right: 0;
  167. }
  168.  
  169. .date-block:first-child {
  170. margin-left: 0;
  171. }
  172.  
  173. .date-block .count2 {
  174. position: relative;
  175. display: block;
  176. padding-top: 6px;
  177. color: white;
  178. font-size: 14px;
  179. font-weight: normal;
  180. text-transform: uppercase;
  181. text-align: center;
  182. z-index: 3;
  183. }
  184.  
  185. .count {
  186. display: block;
  187. padding-top: 32px;
  188. color: #5e2444;
  189. z-index: 3;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement