Countdown.js
(function() { /*var lastTime = 0; var vendors = ['ms', 'moz', 'webkit', 'o']; for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame']; window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame']; } if (!window.requestAnimationFrame) window.requestAnimationFrame = function(callback, element) { var currTime = new Date().getTime(); var timeToCall = Math.max(0, 16 - (currTime - lastTime)); var id = window.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall); lastTime = currTime + timeToCall; return id; }; if (!window.cancelAnimationFrame) window.cancelAnimationFrame = function(id) { clearTimeout(id); }; */ var CountDown=function(options){ var that=this; var today=Date.now(); that.options={ template:'<span class="label">距结束:</span><span class="number" id="countdown_day_{{id}}">{{day}}</span>天<span class="number" id="countdown_hour_{{id}}">{{hour}}</span>时<span class="number" id="countdown_minutes_{{id}}">{{minutes}}</span>分<span class="number" id="countdown_seconds_{{id}}">{{seconds}}</span>秒', id:"countdown",//对象ID currTime:today,//服务器当前时间 beginTime:today+24*60*60*1000, //倒计时开始时间 ext:today,//Id后缀 timeoutId:0,//定时器ID callback:function(){//需要回调的函数 console.warn("当前没有传入的回调!"); } }; for(var i in options){ that.options[i]=options[i]; } that.options.ext=that.options.id+that.options.ext+Math.round(100*Math.random(10)); var id=that.options.ext; console.log("id=="+id); that.options.template=that.options.template.replace(/(\{\{)\s*(\w)\s*(\}\})/g,"$1$2$3").replace(/\{\{id\}\}/g,id); var countObj=$(that.options.id); if(countObj){ hideVisi(countObj); countObj.innerHTML=that.options.template; that.getCountDownAreaTemplate(); } that.init(); }; function $(id){ return document.getElementById(id); } function showVisi(obj){ if(obj.style.visibility!="visible"){ obj.style.visibility="visible"; } } function hideVisi(obj){ if(obj.style.visibility!="hidden"){ obj.style.visibility="hidden"; } } CountDown.prototype={ init:function(){ var that=this; var currTime=that.options.currTime; var beginTime=that.options.beginTime; var intervalTime=beginTime-currTime;//间隔时间 if(intervalTime<0){ console.error("传入的倒计时开始时间比当前时间小!请检查!"); return; } //that.render(intervalTime); that.options.timeoutId=setTimeout(function(){ that.countdown(intervalTime); },1000); }, getCountDownAreaTemplate:function(){ var that=this; var ext=that.options.ext; var dayObj=$("countdown_day_"+ext); if(dayObj){ hideVisi(dayObj); that.options.dayTemplate=dayObj.innerHTML; } var hourObj=$("countdown_hour_"+ext); if(hourObj){ hideVisi(hourObj); that.options.hourTemplate=hourObj.innerHTML; } var minutesObj=$("countdown_minutes_"+ext); if(minutesObj){ hideVisi(minutesObj); that.options.minutesTemplate=minutesObj.innerHTML; } var secondsObj=$("countdown_seconds_"+ext); if(secondsObj){ hideVisi(secondsObj); that.options.secondsTemplate=secondsObj.innerHTML; } }, countdown:function(intervalTime){ var that=this; var t=intervalTime; t=t-1000; that.render(t); if(t<=0){ console.log("%c 倒计已经结束!","color: #fff; background: #f40; font-size: 24px;"); if(that.options.callback){ that.options.callback();//执行回调 } clearTimeout(that.options.timeoutId);//清除定时器 return; } that.options.timeoutId=setTimeout(function(){ that.countdown(t); },1000); }, render:function(intervalTime){//渲染模版 var that=this; var t=intervalTime; var template=that.options.template; var ext=that.options.ext; var day=parseInt(t/(1000*60*60*24),10); var hour=parseInt((t-(day*1000*60*60*24))/(1000*60*60),10); var minutes=parseInt((t-(day*1000*60*60*24)-(hour*1000*60*60))/(1000*60),10); var seconds=parseInt((t-(day*1000*60*60*24)-(hour*1000*60*60)-(minutes*1000*60))/1000,10); if(template.indexOf("{{day}}")!=-1){ if(day<10){ day="0"+day; } if(hour<10){ hour="0"+hour; } if(minutes<10){ minutes="0"+minutes; } if(seconds<10){ seconds="0"+seconds; } //template=template.replace(/({{)\s*(\w)\s*(}})/g,"$1$2$3").replace("{{day}}",day).replace("{{hour}}",hour).replace("{{minutes}}",minutes).replace("{{seconds}}",seconds); that.showCountDown({day:day,hour:hour,minutes:minutes,seconds:seconds}); }else{ if(template.indexOf("{{hour}}")!=-1){ // var hour=parseInt(t/(1000*60*60),10); // var minutes=parseInt((t-(hour*1000*60*60))/(1000*60),10); // var seconds=parseInt((t-(hour*1000*60*60)-(minutes*1000*60))/1000,10); if(hour<10){ hour="0"+hour; } if(minutes<10){ minutes="0"+minutes; } if(seconds<10){ seconds="0"+seconds; } //template=template.replace(/({{)\s*(\w)\s*(}})/g,"$1$2$3").replace("{{hour}}",hour).replace("{{minutes}}",minutes).replace("{{seconds}}",seconds); that.showCountDown({hour:hour,minutes:minutes,seconds:seconds}); }else{ if(template.indexOf("{{minutes}}")!=-1){ // var minutes=parseInt(t/(1000*60),10); // var seconds=parseInt((t-(hour*1000*60*60)-(minutes*1000*60))/1000,10); if(minutes<10){ minutes="0"+minutes; } if(seconds<10){ seconds="0"+seconds; } //template=template.replace(/({{)\s*(\w)\s*(}})/g,"$1$2$3").replace("{{minutes}}",minutes).replace("{{seconds}}",seconds); that.showCountDown({minutes:minutes,seconds:seconds}); }else{ if(template.indexOf("{{seconds}}")!=-1){ // var seconds=parseInt((t-(hour*1000*60*60)-(minutes*1000*60))/1000,10); if(seconds<10){ seconds="0"+seconds; } //template=template.replace(/({{)\s*(\w)\s*(}})/g,"$1$2$3").replace("{{seconds}}",seconds); that.showCountDown({seconds:seconds}); } } } } var countObj=$(that.options.id); if(countObj){ showVisi(countObj); } // var countObj=$(that.options.id); // if(countObj){ // countObj.innerHTML=template; // } }, showCountDown:function(options){//ext ID后缀 options 选项 var that=this; var ext=that.options.ext; var dayObj=$("countdown_day_"+ext); if(dayObj){ dayObj.innerHTML=that.options.dayTemplate.replace("{{day}}",options.day); showVisi(dayObj); } var hourObj=$("countdown_hour_"+ext); if(hourObj){ hourObj.innerHTML=that.options.hourTemplate.replace("{{hour}}",options.hour); showVisi(hourObj); } var minutesObj=$("countdown_minutes_"+ext); if(minutesObj){ minutesObj.innerHTML=that.options.minutesTemplate.replace("{{minutes}}",options.minutes); showVisi(minutesObj); } var secondsObj=$("countdown_seconds_"+ext); if(secondsObj){ secondsObj.innerHTML=that.options.secondsTemplate.replace("{{seconds}}",options.seconds); showVisi(secondsObj); } } }; window.CountDown=CountDown; }());
index.html
<!DOCTYPE html> <html> <head> <meta charset ="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <META name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=1, minimum-scale=1.0, maximum-scale=1.0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <META content="no-cache" http-equiv="pragma"> <META content="no-cache" http-equiv="cache-control"> <META content="0" http-equiv="expires"> <title>倒计时js</title> <style> .countdown .number { font-size: 16px; font-weight: 600; padding: 8px 5px; text-align: center; color: #FFF; background: #67625E; margin: 0 5px; border-radius: 5px; } pre{ padding: 16px; overflow: auto; font-size: 85%; line-height: 1.45; background-color: #F7F7F7; border-radius: 3px; } </style> <script src="countdown.js"></script> </head> <body> <section> <h2>默认倒计时:</h2> <div id="countdown" class="countdown"> </div> <h6>代码格式:</h6> <pre> new CountDown(); </pre> <script> new CountDown(); </script> </section> <section> <h2>自定义模版倒计时:</h2> <div id="countdown2" class="countdown"> </div> <h6>代码格式:</h6> <pre> new CountDown({"template":'<span class="label">自定义倒计时模版:</span><span class="number" id="countdown_hour_{{id}}">{{hour}}</span>小时<span class="number" id="countdown_minutes_{{id}}">{{minutes}}</span>分<span class="number" id="countdown_seconds_{{id}}">{{seconds}}</span>秒'}); 注意:日,时,分,秒的容器ID前缀和数据填充部分“{{xxx}}”不能修改!其它可以任意定义 {{id}}代表日,时,分,秒的容器ID后缀 由当前倒计时实例的时间戳生成 {{day}}:代表天 {{hour}}:代表时 {{minutes}}:代表分 {{seconds}}:代表秒 </pre> <script> //new CountDown({"id":"countdown2","template":'<span class="label">自定义倒计时模版:</span><span class="number" id="countdown_hour_{{id}}">{{hour}}</span>小时<span class="number" id="countdown_minutes_{{id}}">{{minutes}}</span>分<span class="number" id="countdown_seconds_{{id}}">{{seconds}}</span>秒'}); var today=new Date().getTime(); new CountDown({"id":"countdown2","currTime":today,"beginTime":today+3*24*60*60*1000,"template":'<span class="label">自定义倒计时模版:</span><span class="number" id="countdown_day_{{id}}">{{day}}</span>天'}); </script> </section> <section> <h2>传入系统时间倒计时:</h2> <div id="countdown3" class="countdown"> </div> <h6>代码格式:</h6> <pre> var today=new Date().getTime(); new CountDown({"id":"countdown3","currTime":today,"beginTime":today+3*24*60*60*1000}); currTime:代表系统的当前时间 单位为毫秒 beginTime:代表系统传回的倒计时开始时间 单位为毫秒 </pre> <script> var today=new Date().getTime(); new CountDown({"id":"countdown3","currTime":today,"beginTime":today+3*24*60*60*1000}); </script> </section> <section> <h2>有传入回调的倒计时:</h2> <div id="countdown4" class="countdown"> </div> <h6>代码格式:</h6> <pre> var today=new Date().getTime(); new CountDown({"id":"countdown4","currTime":today,"beginTime":today+10*1000,callback:function(){ alert("倒计时已经完成!"); }}); callback:可传入任何你需要在页面上处理的操作! </pre> <script> var today=new Date().getTime(); new CountDown({"id":"countdown4","currTime":today,"beginTime":today+10*1000,callback:function(){ alert("倒计时已经完成!"); }}); </script> </section> </body> </html>
github:
https://github.com/wangyong31893189/countDwon
相关推荐
网页模板——基于HTML5 time元素的倒计时jquery插件
PT倒计时器精美模板PT倒计时器精美模板
网站倒计时插件,多个倒计时同事进行,也可以实现倒计时结束,该快区域消失或者显示活动已经结束。
wp-clock 插件支持在单独页面中呈现一个倒计时时钟。如果你的网站、博客想搞一搞上线倒计时,这个插件是不二之选。
网页模板——基于jquery插件实现的发光倒计时
雨滴网站上线倒计时模板是一款大气宽屏的雨滴背景风格网站倒计时建设页面模板下载。
HTML5背景图倒计时网站模板源码,好看倒计时,公司年会倒计时,可以更改为,朋友生日倒计时,活动倒计时,网站源码,各种风格都有,代码上手简单,代码独立,可以直接使用。也可直接预览效果。 效果演示地址: ...
jCountr基于jquery的倒计时插件.zip
网站上线倒计时单页专题模板,倒计时基于jquery.countdown.js这个插件,适用于域名已经注册,网站还没设计好的用户。模板主题:时间属于在这里的每一个人,一路走来,谢谢你们的陪伴,终于等到你,还好你没放弃!
单机操作。操作比较简单,可以进行字体和颜色设置。-,Delphi,教育系统应用
上线倒计时邮箱订阅网页模板
本文实例为大家分享了使用countdown插件实现倒计时的具体代码,供大家参考,具体内容如下 实现的效果如下: 这里实现的是一个活动倒计时,获取当前时间和活动开始时间,相减得出的时间差就是我们需要的倒计时。使用...
此外,它还内置了丰富的JavaScript插件,助您轻松实现各种复杂的交互效果。这套源代码文件具有极高的可定制性,您可以根据您的项目需求,对页面布局、颜色和内容进行个性化调整,轻松打造出独具特色的网站。同时,...
此外,它还内置了丰富的JavaScript插件,助您轻松实现各种复杂的交互效果。这套源代码文件具有极高的可定制性,您可以根据您的项目需求,对页面布局、颜色和内容进行个性化调整,轻松打造出独具特色的网站。同时,...
此外,它还内置了丰富的JavaScript插件,助您轻松实现各种复杂的交互效果。这套源代码文件具有极高的可定制性,您可以根据您的项目需求,对页面布局、颜色和内容进行个性化调整,轻松打造出独具特色的网站。同时,...
此外,它还内置了丰富的JavaScript插件,助您轻松实现各种复杂的交互效果。这套源代码文件具有极高的可定制性,您可以根据您的项目需求,对页面布局、颜色和内容进行个性化调整,轻松打造出独具特色的网站。同时,...
此外,它还内置了丰富的JavaScript插件,助您轻松实现各种复杂的交互效果。这套源代码文件具有极高的可定制性,您可以根据您的项目需求,对页面布局、颜色和内容进行个性化调整,轻松打造出独具特色的网站。同时,...
此外,它还内置了丰富的JavaScript插件,助您轻松实现各种复杂的交互效果。这套源代码文件具有极高的可定制性,您可以根据您的项目需求,对页面布局、颜色和内容进行个性化调整,轻松打造出独具特色的网站。同时,...
此外,它还包含了丰富的JavaScript插件,可以实现各种复杂的交互效果。这套源代码文件还具有高度的可定制性。您可以根据自己的需求对页面进行布局调整、颜色更改以及内容替换,轻松打造出符合您项目风格的网站。同时...