countdown.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. function countdown(o) {
  2. // 确定传入参数
  3. if (o) {
  4. var o = o;
  5. } else {
  6. var o = {
  7. el: 'countdown',
  8. st: 00000000,
  9. ed: 1800000,
  10. dd:'dd',
  11. hh: 'hh',
  12. mm: 'mm',
  13. ss: 'ss',
  14. ms: 'ms'
  15. };
  16. }
  17. // 参数是否存在空值
  18. o.el = o.el ? o.el : 'countdown';
  19. o.st = o.st ? o.st : 00000000000;
  20. o.ed = o.ed ? o.ed : 1800000;
  21. o.dd = o.dd ? o.dd : 'dd';
  22. o.hh = o.hh ? o.hh : 'hh';
  23. o.mm = o.mm ? o.mm : 'mm';
  24. o.ss = o.ss ? o.ss : 'ss';
  25. o.ms = o.ms ? o.ms : 'ms';
  26. // 插入默认样式
  27. var head = document.getElementsByTagName('head').item(0);
  28. var link = document.createElement('link');
  29. link.href = 'countdown.css';
  30. link.rel = 'stylesheet';
  31. link.type = 'text/css';
  32. head.appendChild(link);
  33. // 计算倒计时时间长度
  34. this.c = (parseInt(o.ed) - parseInt(o.st)) / 1000;
  35. // 设置毫秒值
  36. this.ms = 9;
  37. // 返回倒计时时间
  38. this.downs = function() {
  39. var that = this;
  40. var d = parseInt(that.c / (3600*24));
  41. var h = parseInt(that.c %(3600*24) / 3600);
  42. var m = parseInt(that.c % 3600 / 60);
  43. var s = that.c % 3600 % 60;
  44. var ms = that.ms;
  45. if (h < 10) {
  46. h = '0' + h;
  47. }
  48. if (m < 10) {
  49. m = '0' + m;
  50. }
  51. if (s < 10) {
  52. s = '0' + s;
  53. }
  54. if (d=0){
  55. var t = h + ':' + m + ':' + s;
  56. document.getElementById(o.el).innerHTML = t;
  57. }
  58. else {
  59. var t =d +"天 "+ h + ':' + m + ':' + s;
  60. document.getElementById(o.el).innerHTML = t;
  61. }
  62. that.c = that.c - 1;
  63. setTimeout(function() {
  64. that.downs();
  65. }, 1000);
  66. return t;
  67. }
  68. // 返回倒计时时间
  69. this.downms = function() {
  70. var that = this;
  71. var d = parseInt(that.c / (3600*24));
  72. var h = parseInt(that.c %(3600*24) / 3600);
  73. var m = parseInt(that.c % 3600 / 60);
  74. var s = that.c % 3600 % 60;
  75. var ms = that.ms;
  76. if (h < 10) {
  77. h = '0' + h;
  78. }
  79. if (m < 10) {
  80. m = '0' + m;
  81. }
  82. if (s < 10) {
  83. s = '0' + s;
  84. }
  85. if (d=0){
  86. var t = h + ':' + m + ':' + s;
  87. document.getElementById(o.el).innerHTML = t;
  88. }
  89. else {
  90. var t =d +"天 "+ h + ':' + m + ':' + s;
  91. document.getElementById(o.el).innerHTML = t;
  92. }
  93. that.ms = that.ms - 1;
  94. if (ms <= 0) {
  95. that.ms = 9;
  96. that.c = that.c - 1;
  97. }
  98. setTimeout(function() {
  99. that.downms();
  100. }, 100);
  101. return t;
  102. }
  103. // 返回倒计时时间
  104. this.downmsElement = function() {
  105. var that = this;
  106. var d = parseInt(that.c / (3600*24));
  107. var h = parseInt(that.c %(3600*24)/ 3600);
  108. var m = parseInt(that.c % 3600 / 60);
  109. var s = that.c % 3600 % 60;
  110. if (h < 10) {
  111. h = '0' + h;
  112. }
  113. if (m < 10) {
  114. m = '0' + m;
  115. }
  116. if (s < 10) {
  117. s = '0' + s;
  118. }
  119. // var to = "<span class='hh'>" + h + "</span><span>:</span><span class='mm'>" + m + "</span><span>:</span><span class='ss'>" + s + "</span><span>:</span><span class='ms'>" + that.ms + "</span>";
  120. /**
  121. * 创建dom节点
  122. * */
  123. var dd=document.createElement('span');
  124. var hh = document.createElement('span');
  125. var mm = document.createElement('span');
  126. var ss = document.createElement('span');
  127. var ms = document.createElement('span');
  128. var f1 = document.createElement('span');
  129. var f2 = document.createElement('span');
  130. var f3 = document.createElement('span');
  131. var f4 = document.createElement('span');
  132. /**
  133. * 节点class
  134. * */
  135. hh.className = o.hh;
  136. mm.className = o.mm;
  137. ss.className = o.ss;
  138. ms.className = o.ms;
  139. dd.className = o.dd;
  140. /**
  141. * 创建文本节点
  142. * */
  143. var dt = document.createTextNode(d);
  144. var ht = document.createTextNode(h);
  145. var mt = document.createTextNode(m);
  146. var st = document.createTextNode(s);
  147. var mst = document.createTextNode(that.ms);
  148. var ft1 = document.createTextNode(":");
  149. var ft2 = document.createTextNode(":");
  150. var ft3 = document.createTextNode(":");
  151. var ft4 = document.createTextNode("天");
  152. /**
  153. * 将文本节点写入dom节点
  154. * */
  155. dd.appendChild(dt);
  156. hh.appendChild(ht);
  157. mm.appendChild(mt);
  158. ss.appendChild(st);
  159. ms.appendChild(mst);
  160. f1.appendChild(ft1);
  161. f2.appendChild(ft2);
  162. f3.appendChild(ft3);
  163. f4.appendChild(ft4);
  164. /**
  165. * 返回倒计时字符串
  166. * */
  167. var t = h + ':' + m + ':' + s + ':' + that.ms;
  168. if (d>0){
  169. t =d +"天 "+ h + ':' + m + ':' + s + ':' + that.ms;
  170. }
  171. /**
  172. * 将倒计时写入el节点
  173. * */
  174. var ele = document.getElementById(o.el);
  175. // ele.innerHTML = to;
  176. ele.innerHTML = "";
  177. ele.appendChild(dd);
  178. ele.appendChild(f4);
  179. ele.appendChild(hh);
  180. ele.appendChild(f1);
  181. ele.appendChild(mm);
  182. ele.appendChild(f2);
  183. ele.appendChild(ss);
  184. ele.appendChild(f3);
  185. ele.appendChild(ms);
  186. /**
  187. * 毫秒倒计时
  188. * */
  189. that.ms = that.ms - 1;
  190. if (that.ms < 0) {
  191. that.ms = 9;
  192. that.c = that.c - 1;
  193. }
  194. setTimeout(function() {
  195. that.downmsElement();
  196. }, 100);
  197. return t;
  198. }
  199. // 返回倒计时时间
  200. this.downsElement = function() {
  201. var that = this;
  202. var d = parseInt(that.c / (3600*24));
  203. var h = parseInt(that.c %(3600*24)/ 3600);
  204. var m = parseInt(that.c % 3600 / 60);
  205. var s = that.c % 3600 % 60;
  206. if (h < 10) {
  207. h = '0' + h;
  208. }
  209. if (m < 10) {
  210. m = '0' + m;
  211. }
  212. if (s < 10) {
  213. s = '0' + s;
  214. }
  215. // var to = "<span class='hh'>" + h + "</span><span>:</span><span class='mm'>" + m + "</span><span>:</span><span class='ss'>" + s + "</span>";
  216. /**
  217. * 创建dom节点
  218. * */
  219. var dd=document.createElement('span');
  220. var hh = document.createElement('span');
  221. var mm = document.createElement('span');
  222. var ss = document.createElement('span');
  223. var f1 = document.createElement('span');
  224. var f2 = document.createElement('span');
  225. var f4 = document.createElement('span');
  226. /**
  227. * 节点class
  228. * */
  229. hh.className = o.hh;
  230. mm.className = o.mm;
  231. ss.className = o.ss;
  232. dd.className = o.dd;
  233. /**
  234. * 创建文本节点
  235. * */
  236. var ht = document.createTextNode(h);
  237. var mt = document.createTextNode(m);
  238. var st = document.createTextNode(s);
  239. var ft1 = document.createTextNode(":");
  240. var ft2 = document.createTextNode(":");
  241. var dt = document.createTextNode(d);
  242. var ft4 = document.createTextNode("天");
  243. /**
  244. * 将文本节点写入dom节点
  245. * */
  246. hh.appendChild(ht);
  247. mm.appendChild(mt);
  248. ss.appendChild(st);
  249. f1.appendChild(ft1);
  250. f2.appendChild(ft2);
  251. dd.appendChild(dt);
  252. f4.appendChild(ft4);
  253. /**
  254. * 返回倒计时字符串
  255. * */
  256. var t = h + ':' + m + ':' + s;
  257. if (d>0){
  258. t =d +"天 "+ h + ':' + m + ':' + s;
  259. }
  260. /**
  261. * 将倒计时写入el节点
  262. * */
  263. var ele = document.getElementById(o.el);
  264. // ele.innerHTML = to;
  265. ele.innerHTML = '';
  266. ele.appendChild(dd);
  267. ele.appendChild(f4);
  268. ele.appendChild(hh);
  269. ele.appendChild(f1);
  270. ele.appendChild(mm);
  271. ele.appendChild(f2);
  272. ele.appendChild(ss);
  273. that.c = that.c - 1;
  274. setTimeout(function() {
  275. that.downsElement();
  276. }, 1000);
  277. return t;
  278. }
  279. }