123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- function countdown(o) {
- // 确定传入参数
- if (o) {
- var o = o;
- } else {
- var o = {
- el: 'countdown',
- st: 00000000,
- ed: 1800000,
- dd:'dd',
- hh: 'hh',
- mm: 'mm',
- ss: 'ss',
- ms: 'ms'
- };
- }
- // 参数是否存在空值
- o.el = o.el ? o.el : 'countdown';
- o.st = o.st ? o.st : 00000000000;
- o.ed = o.ed ? o.ed : 1800000;
- o.dd = o.dd ? o.dd : 'dd';
- o.hh = o.hh ? o.hh : 'hh';
- o.mm = o.mm ? o.mm : 'mm';
- o.ss = o.ss ? o.ss : 'ss';
- o.ms = o.ms ? o.ms : 'ms';
- // 插入默认样式
- var head = document.getElementsByTagName('head').item(0);
- var link = document.createElement('link');
- link.href = 'countdown.css';
- link.rel = 'stylesheet';
- link.type = 'text/css';
- head.appendChild(link);
- // 计算倒计时时间长度
- this.c = (parseInt(o.ed) - parseInt(o.st)) / 1000;
- // 设置毫秒值
- this.ms = 9;
- // 返回倒计时时间
- this.downs = function() {
- var that = this;
- var d = parseInt(that.c / (3600*24));
- var h = parseInt(that.c %(3600*24) / 3600);
- var m = parseInt(that.c % 3600 / 60);
- var s = that.c % 3600 % 60;
- var ms = that.ms;
- if (h < 10) {
- h = '0' + h;
- }
- if (m < 10) {
- m = '0' + m;
- }
- if (s < 10) {
- s = '0' + s;
- }
- if (d=0){
- var t = h + ':' + m + ':' + s;
- document.getElementById(o.el).innerHTML = t;
- }
- else {
- var t =d +"天 "+ h + ':' + m + ':' + s;
- document.getElementById(o.el).innerHTML = t;
- }
- that.c = that.c - 1;
- setTimeout(function() {
- that.downs();
- }, 1000);
- return t;
- }
- // 返回倒计时时间
- this.downms = function() {
- var that = this;
- var d = parseInt(that.c / (3600*24));
- var h = parseInt(that.c %(3600*24) / 3600);
- var m = parseInt(that.c % 3600 / 60);
- var s = that.c % 3600 % 60;
- var ms = that.ms;
- if (h < 10) {
- h = '0' + h;
- }
- if (m < 10) {
- m = '0' + m;
- }
- if (s < 10) {
- s = '0' + s;
- }
- if (d=0){
- var t = h + ':' + m + ':' + s;
- document.getElementById(o.el).innerHTML = t;
- }
- else {
- var t =d +"天 "+ h + ':' + m + ':' + s;
- document.getElementById(o.el).innerHTML = t;
- }
- that.ms = that.ms - 1;
- if (ms <= 0) {
- that.ms = 9;
- that.c = that.c - 1;
- }
- setTimeout(function() {
- that.downms();
- }, 100);
- return t;
- }
- // 返回倒计时时间
- this.downmsElement = function() {
- var that = this;
- var d = parseInt(that.c / (3600*24));
- var h = parseInt(that.c %(3600*24)/ 3600);
- var m = parseInt(that.c % 3600 / 60);
- var s = that.c % 3600 % 60;
- if (h < 10) {
- h = '0' + h;
- }
- if (m < 10) {
- m = '0' + m;
- }
- if (s < 10) {
- s = '0' + s;
- }
- // 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>";
- /**
- * 创建dom节点
- * */
- var dd=document.createElement('span');
- var hh = document.createElement('span');
- var mm = document.createElement('span');
- var ss = document.createElement('span');
- var ms = document.createElement('span');
- var f1 = document.createElement('span');
- var f2 = document.createElement('span');
- var f3 = document.createElement('span');
- var f4 = document.createElement('span');
- /**
- * 节点class
- * */
- hh.className = o.hh;
- mm.className = o.mm;
- ss.className = o.ss;
- ms.className = o.ms;
- dd.className = o.dd;
- /**
- * 创建文本节点
- * */
- var dt = document.createTextNode(d);
- var ht = document.createTextNode(h);
- var mt = document.createTextNode(m);
- var st = document.createTextNode(s);
- var mst = document.createTextNode(that.ms);
- var ft1 = document.createTextNode(":");
- var ft2 = document.createTextNode(":");
- var ft3 = document.createTextNode(":");
- var ft4 = document.createTextNode("天");
- /**
- * 将文本节点写入dom节点
- * */
- dd.appendChild(dt);
- hh.appendChild(ht);
- mm.appendChild(mt);
- ss.appendChild(st);
- ms.appendChild(mst);
- f1.appendChild(ft1);
- f2.appendChild(ft2);
- f3.appendChild(ft3);
- f4.appendChild(ft4);
- /**
- * 返回倒计时字符串
- * */
- var t = h + ':' + m + ':' + s + ':' + that.ms;
- if (d>0){
- t =d +"天 "+ h + ':' + m + ':' + s + ':' + that.ms;
- }
-
- /**
- * 将倒计时写入el节点
- * */
- var ele = document.getElementById(o.el);
- // ele.innerHTML = to;
- ele.innerHTML = "";
- ele.appendChild(dd);
- ele.appendChild(f4);
- ele.appendChild(hh);
- ele.appendChild(f1);
- ele.appendChild(mm);
- ele.appendChild(f2);
- ele.appendChild(ss);
- ele.appendChild(f3);
- ele.appendChild(ms);
- /**
- * 毫秒倒计时
- * */
- that.ms = that.ms - 1;
- if (that.ms < 0) {
- that.ms = 9;
- that.c = that.c - 1;
- }
- setTimeout(function() {
- that.downmsElement();
- }, 100);
- return t;
- }
- // 返回倒计时时间
- this.downsElement = function() {
-
- var that = this;
- var d = parseInt(that.c / (3600*24));
- var h = parseInt(that.c %(3600*24)/ 3600);
- var m = parseInt(that.c % 3600 / 60);
- var s = that.c % 3600 % 60;
- if (h < 10) {
- h = '0' + h;
- }
- if (m < 10) {
- m = '0' + m;
- }
- if (s < 10) {
- s = '0' + s;
- }
- // var to = "<span class='hh'>" + h + "</span><span>:</span><span class='mm'>" + m + "</span><span>:</span><span class='ss'>" + s + "</span>";
- /**
- * 创建dom节点
- * */
- var dd=document.createElement('span');
- var hh = document.createElement('span');
- var mm = document.createElement('span');
- var ss = document.createElement('span');
- var f1 = document.createElement('span');
- var f2 = document.createElement('span');
- var f4 = document.createElement('span');
- /**
- * 节点class
- * */
-
- hh.className = o.hh;
- mm.className = o.mm;
- ss.className = o.ss;
- dd.className = o.dd;
- /**
- * 创建文本节点
- * */
-
- var ht = document.createTextNode(h);
- var mt = document.createTextNode(m);
- var st = document.createTextNode(s);
- var ft1 = document.createTextNode(":");
- var ft2 = document.createTextNode(":");
- var dt = document.createTextNode(d);
- var ft4 = document.createTextNode("天");
- /**
- * 将文本节点写入dom节点
- * */
- hh.appendChild(ht);
- mm.appendChild(mt);
- ss.appendChild(st);
- f1.appendChild(ft1);
- f2.appendChild(ft2);
- dd.appendChild(dt);
- f4.appendChild(ft4);
- /**
- * 返回倒计时字符串
- * */
- var t = h + ':' + m + ':' + s;
- if (d>0){
- t =d +"天 "+ h + ':' + m + ':' + s;
- }
- /**
- * 将倒计时写入el节点
- * */
- var ele = document.getElementById(o.el);
-
- // ele.innerHTML = to;
- ele.innerHTML = '';
- ele.appendChild(dd);
- ele.appendChild(f4);
- ele.appendChild(hh);
- ele.appendChild(f1);
- ele.appendChild(mm);
- ele.appendChild(f2);
- ele.appendChild(ss);
- that.c = that.c - 1;
- setTimeout(function() {
- that.downsElement();
- }, 1000);
- return t;
- }
- }
|