countdown.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // +----------------------------------------------------------------------
  2. // | A3Mall
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  5. // +----------------------------------------------------------------------
  6. // | Author: xzncit <158373108@qq.com>
  7. // +----------------------------------------------------------------------
  8. (function ($) {
  9. var defaluts = {
  10. fillZero: {
  11. day : { count : 86400, num : 2, def : '00' },
  12. hour : { count : 3600, num : 2, def : '00' },
  13. minute : { count : 60, num : 2, def : '00' },
  14. second : { count : 1 , num : 2, def : '00' }
  15. },
  16. sClass: {
  17. before: ".before",
  18. day: ".day",
  19. hour: ".hour",
  20. minute: ".minute",
  21. second: ".second",
  22. after: ".after",
  23. tips: ".tips"
  24. },
  25. text: {
  26. after: {
  27. "day" : "天","hour" : "时", "minute" : "分","second" : "秒"
  28. }
  29. },
  30. isStatus: function (status,index){}
  31. };
  32. // return false; break
  33. // return true; continue
  34. $.fn.countdown = function (options){
  35. var config = $.extend({}, defaluts, options);
  36. return $(this).each(function (index) {
  37. var that = $(this);
  38. var nowTime = parseInt($(this).attr("data-nowTime"));
  39. var startTime = parseInt($(this).attr("data-startTime"));
  40. var endTime = parseInt($(this).attr("data-endTime"));
  41. var totalTime = endTime - nowTime;
  42. var nextTotal = 0;
  43. var type = 0;
  44. var timer = null;
  45. initTag(that,config);
  46. if(totalTime <= 0){
  47. config.isStatus(0,index);
  48. $(config.sClass.tips,that).html("活动己结束");
  49. return true;
  50. }else if(nowTime < startTime){
  51. $(config.sClass.before,that).html("距开始仅剩:");
  52. totalTime = startTime - nowTime;
  53. nextTotal = endTime - nowTime - totalTime;
  54. type = 1;
  55. config.isStatus(2);
  56. }else if(nowTime > startTime && endTime > nowTime){
  57. $(config.sClass.before,that).html("距结束仅剩:");
  58. type = 2;
  59. config.isStatus(1);
  60. }
  61. var run = function (){
  62. if(totalTime <= 0){
  63. timer && clearInterval(timer);
  64. if(type == 1){
  65. totalTime = nextTotal;
  66. type = 2;
  67. config.isStatus(1);
  68. $(config.sClass.before,that).html("距结束仅剩:");
  69. run();
  70. timer = setInterval(run,1000);
  71. }else{
  72. config.isStatus(0);
  73. initTag(that,config);
  74. $(config.sClass.tips,that).html("活动己结束");
  75. }
  76. }else{
  77. let dateTime = totalTime;
  78. var json = { "day" : "","hour" : "", "minute" : "","second" : "" };
  79. for(let i in config.fillZero){
  80. let data = config.fillZero[i];
  81. let flag = dateTime >= data.count ? true : false;
  82. if(!flag){
  83. json[i] = data.def;
  84. }
  85. if(flag){
  86. let value = Math.floor(dateTime / data.count);
  87. json[i] = fill(value.toString(),data.num);
  88. dateTime -= value * data.count;
  89. }
  90. if($(config.sClass[i])[0]){
  91. $(config.sClass[i],that).html(parseInt(json[i]) + config.text.after[i]);
  92. }
  93. }
  94. totalTime--;
  95. }
  96. };
  97. run();
  98. timer = setInterval(run,1000);
  99. });
  100. };
  101. var initTag = function (obj,config){
  102. for(var i in config.sClass){
  103. $(config.sClass[i],obj).html("");
  104. }
  105. };
  106. var fill = function (i,n){
  107. var str = '' + i;
  108. while(str.length < n){
  109. str = '0' + str;
  110. }
  111. return str;
  112. };
  113. })(jQuery);