来自 服务器&运维 2019-12-14 06:31 的文章
当前位置: 澳门威尼斯人平台 > 服务器&运维 > 正文

原生js实现倒计时--2018_javascript技巧_脚本之家

思路:

本文实例为大家分享了Moment.js实现多个同时倒计时的具体代码,供大家参考,具体内容如下

 倒计时-多种格式调用-原生js封装      /*公共*/ html{ height:100%; } body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, textarea, p, blockquote, th, td { margin: 0; padding: 0 } ol, ul { list-style: none } body{ position: relative; min-height:100%; font-size:14px; font-family: Tahoma, Verdana,"Microsoft Yahei"; color:#333; } a{ text-decoration: none; color:#333; } .header{ width: 960px; padding-top: 15px; margin: 0 auto; line-height: 30px; text-align: right; } .header a{ margin: 0 5px; } .main{ width:960px; margin: 50px auto 0; } .code{ border:1px dashed #e2e2e2; padding:10px 5px; margin-bottom:25px; } pre { font-family: "Microsoft Yahei",Arial,Helvetica; white-space: pre-wrap; /*css-3*/ white-space: -moz-pre-wrap; /*Mozilla,since1999*/ white-space: -pre-wrap; /*Opera4-6*/ white-space: -o-pre-wrap; /*Opera7*/ word-wrap: break-word; /*InternetExplorer5.5+*/ } .example{ padding-top:40px; margin-bottom:90px; } .example .call{ padding:18px 5px; background:#f0f5f8; } .example h2{ padding-top:20px; margin-bottom:7px; } .example table { width:100%; table-layout:fixed; border-collapse: collapse; border-spacing: 0; border: 1px solid #cee1ee; border-left: 0; } .example thead { border-bottom: 1px solid #cee1ee; background-color: #e3eef8; } .example tr { line-height: 24px; font-size: 13px; } .example tr:nth-child { background-color: #f0f5f8; } .example tr th,.example tr td { border-left: 1px solid #cee1ee; word-break: break-all; word-wrap: break-word; padding:0 10px; font-weight: normal; } .example tr th { color: #555; padding-top: 2px; padding-bottom: 2px; text-align: left; } /*公共*/ .countdown { margin-bottom: 15px; }   /*封装代码*/  { var Countdown = function { var self = this; var defaults = { 'format': 'hh:mm:ss', //格式 'endtime': '', //结束时间 'interval': 1000, //多久倒计时一次 单位:ms 'starttime':r[0].innerHTML, //开始时间 'countEach': function { //每单位时间出发事件,传入一个对象,包含时间信息和时间格式化输出[0].innerHTML=time['format'] }, 'countEnd':function  {} //倒计时结束回调事件 } opts = opts || {}; for  { if ("undefined" == typeof opts[w]) { opts[w] = defaults[w]; } } this.params = opts; this.container = r; if (this.container.length > 1) { var x = []; return this.container.each { x.push(new Countdown, x } this._hander=null; this._start=0; this._end=0; this.isTimestamp = isNaN(this.params.starttime)||isNaN;//是否为秒计数模式 this.init(); } Countdown.prototype = { //初始化 init: function() { var self = this; this.reset(); }, reset:function(){ var self = this; if  { this._start = this.params.starttime ? this.getTimestamp(this.params.starttime) : ; this._end = this.getTimestamp; } else { this._start = this.params.starttime * 1e3; this._end = this.params.endtime * 1e3; } this.count(); }, count:function(){ var self = this; this._hander = setInterval{ self._start-=self.params.interval; self.params.countEach(self.getTime; if(self._start<=self._end){ clearInterval; self.params.countEnd(); } },self.params.interval); }, //获取时间戳 getTimestamp:function{ return +new Date||+new Date; }, timeFormat:function{ var date = new Date; var o = { "M+" : date.getMonth()+1, //月份 "d+" : date.getDate(), //日 "h+" : date.getHours(), //小时 "m+" : date.getMinutes(), //分 "s+" : date.getSeconds(), //秒 "q+" : Math.floor/3), //季度 "S" : date.getMilliseconds/.test{ fmt=fmt.replace(RegExp.$1, +"").substr); } for{ if").test{ fmt = fmt.replace(RegExp.$1,  ? .substr; } } return fmt; }, getTime: function { var self = this; var date, format; if  { date = new Date; format = self.timeFormat(self.params.format, timestamp); } else { date = new Date(); format = timestamp / 1e3; } return { 'year': date.getFullYear(), 'month': date.getMonth() + 1, 'day': date.getDate(), 'hour': date.getHours(), 'minute': date.getMinutes(), 'second': date.getSeconds(), 'quarter': Math.floor / 3), 'microsecond': date.getMilliseconds(), 'format': format }; } } var r =  { var e = function { var a = this, t = 0; for (t = 0; t < e.length; t++) { a[t] = e[t]; } return a.length = e.length, this }; e.prototype = { addClass: function { if ("undefined" == typeof e) return this; for , t = 0; t < a.length; t++) for (var r = 0; r < this.length; r++) this[r].classList.add; return this }, each: function { for (var a = 0; a < this.length; a++) e.call; return this }, html: function { if ("undefined" == typeof e) return this[0] ? this[0].innerHTML : void 0; for (var a = 0; a < this.length; a++) this[a].innerHTML = e; return this }, find: function { for (var t = [], r = 0; r < this.length; r++) for (var i = this[r].querySelectorAll, s = 0; s < i.length; s++) t.push; return new e }, append: function { var t, r; for (t = 0; t < this.length; t++) if  { var i = document.createElement; for (i.innerHTML = a; i.firstChild;) this[t].appendChild } else if  for (r = 0; r < a.length; r++) this[t].appendChild; else this[t].appendChild; return this }, } var a = function { var r = [], i = 0; if (a && !t && a instanceof e) { return a; } if  { if  { var s, n, o = a.trim >= 0 && o.indexOf { var l = "div"; for (0 === o.indexOf, 0 === o.indexOf && , (0 === o.indexOf || 0 === o.indexOf, 0 === o.indexOf && , 0 === o.indexOf && , n = document.createElement, n.innerHTML = a, i = 0; i < n.childNodes.length; i++) r.push } else for (s = t || "#" !== a[0] || a.match ? .querySelectorAll : [document.getElementById], i = 0; i < s.length; i++) s[i] && r.push } else if (a.nodeType || a === window || a === document) { r.push; } else if (a.length > 0 && a[0].nodeType) { for (i = 0; i < a.length; i++) { r.push; } } } return new e window.countdown = Countdown; })() /*封装代码*/   项目地址 返回首页    #例子1# 12:00:00到11:50:00 12:00:00   new countdown("#countdown", { endtime: '11:50:00' });    endtime: '11:50:00',设置结束时间/默认值为空||0,执行其他默认参数  new countdown("#countdown", { endtime: '11:50:00' });   #例子2# 60到50 60   new countdown("#countdown1", { endtime: '50', countEnd: function;    countEnd: 'function',设置结束倒计时后触发的函数/默认值为空,执行其他默认参数  new countdown("#countdown1", { endtime: '50', countEnd: function;       new countdown("#countdown2", { starttime:'60' });    starttime:'60',设置开始时间/默认值为元素内容,执行其他默认参数  new countdown("#countdown2", { starttime:'60' });   #例子4# 2017/01/11,11:00:00到1970/1/1 2017/01/11,11:00:00   new countdown("#countdown3", { format:'yy:MM:dd:hh:mm:ss' });    format:'yy:MM:dd:hh:mm:ss',格式化输出的时间格式/默认值为'hh:mm:ss',执行其他默认参数  new countdown("#countdown3", { format:'yy:MM:dd:hh:mm:ss' });    调用方法: new countdown;  options参数    参数
function checkTime { i = "0" +i; } return i; }; function GetRTime{ h=Math.floor; m=Math.floor; s=Math.floor; d = checkTime; m = checkTime; $; $; $; $; }

获取日期对象、现在距离1970年的毫秒数、2018年距离1970年的毫秒数、2018年距离现在的总秒数;

第一步:

默认值

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

总秒数/86400,整数部分代表天;

首先项目中需要引入moment.js。安装方法如下:

说明

余数部分/3600,整数部分代表小时;

bower install moment --save # bowernpm install moment --save # npm

format

余数部分/60,整数部分代表分钟;

安装成功之后引入到项目中:

'hh:mm:ss'

效果如下:

import moment from 'moment'

格式化输出的时间格式,年、分

本文由澳门威尼斯人平台发布于服务器&运维,转载请注明出处:原生js实现倒计时--2018_javascript技巧_脚本之家

关键词: