纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.9 KiB

1 year ago
  1. /*
  2. * 倒计时 小插件
  3. * @since 2018/11/7 12:39
  4. * @author FlyTiger
  5. * */
  6. (function (global, factory) {
  7. typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory() : typeof define === "function" && define.amd ? (function(){ define(["jquery"],factory);global.countDown = factory();})() : (global.countDown = factory());
  8. })(this, function () {
  9. var ___=window;
  10. //模板
  11. var $defaultTitle=["距离","开始","还有"];
  12. var $template="<span class=\"active-time pull-right\"><em class=\"time_d sd\"></em><span class=\"sd\">天</span><em class=\"time_h sh\"></em><span class=\"sh\">时</span><em class=\"time_m sm\"></em><span class=\"sm\">分</span><em class=\"time_s\"></em>秒";
  13. //事件
  14. var events={
  15. started:"countDownStarted",//开启
  16. ended:"countDownEnded",//结束
  17. restarted:"countDownRestarted"//重启
  18. };
  19. //方法
  20. var fns={
  21. //标题处理
  22. _title:function(){
  23. var _opts=this.opts;
  24. var $prefix=this.$timeEms_.$prefix,$suffix=this.$timeEms_.$suffix;
  25. if((_opts.title&&_opts.title.length)||(!_opts.prefix&&!_opts.suffix)){
  26. var _1= [].concat($defaultTitle);
  27. _1.splice(1,1,_opts.title||$defaultTitle[1]);
  28. $prefix.html(_1.join(""));
  29. }else{
  30. $prefix.html(_opts.prefix||"");
  31. $suffix.html(_opts.suffix||"");
  32. }
  33. },
  34. //初始化
  35. prepare:function(){
  36. fns._title.call(this);
  37. fns._f.call(this);
  38. },
  39. _f:function () {
  40. var _this=this;
  41. var time_end=this.opts.time_end||new Date().getTime();
  42. var _ems=this.$timeEms_;
  43. var f_=function _f(_time_end){
  44. var time_start = new Date().getTime(); //设定当前时间
  45. var time_end = new Date(_time_end).getTime(); //设定目标时间
  46. // 计算时间差
  47. var time_distance = time_start - time_end;
  48. if(time_distance<1){
  49. if(_this._timestamp_){
  50. clearTimeout(_this._timestamp_);
  51. }
  52. _this._runing++;
  53. _this.$container_.hide().triggerHandler(events.ended,[_this._runing,time_start]);
  54. return false;
  55. }
  56. // 天
  57. var int_day = Math.floor(time_distance/86400000)
  58. time_distance -= int_day * 86400000;
  59. // 时
  60. var int_hour = Math.floor(time_distance/3600000)
  61. time_distance -= int_hour * 3600000;
  62. // 分
  63. var int_minute = Math.floor(time_distance/60000)
  64. time_distance -= int_minute * 60000;
  65. // 秒
  66. var int_second = Math.floor(time_distance/1000)
  67. // 显示时间
  68. _ems.$d.html(int_day);
  69. _ems.$h.html(int_hour);
  70. _ems.$m.html(int_minute);
  71. _ems.$s.html(int_second);
  72. // 设置定时器
  73. if(_this._timestamp_){
  74. clearTimeout(_this._timestamp_);
  75. }
  76. _this._timestamp_=setTimeout(function (_) {
  77. _f(_);
  78. },1000,_time_end);
  79. };
  80. if(this._runing){
  81. this.$container_.triggerHandler(events.restarted,[this._runing]);
  82. }else{
  83. this.$container_.triggerHandler(events.started,[this._runing]);
  84. }
  85. this.$container_.show();
  86. f_(time_end);
  87. }
  88. };
  89. return function($container,$opts){
  90. if(!___.jQuery){
  91. throw new Error("jQuery is required for this plugin");
  92. }
  93. if(!$container||!$container.length){
  94. throw new Error("The container you given is not useful!");
  95. }
  96. return new CountDown($container,$opts);
  97. }
  98. });