用javascript写的一个简单的常用js倒计时效果,小计一下,代码如下:
html&css:
<style type="text/css"> .time-box{width:650px;height:50px;margin:50px auto; font:600 26px/50px "微软雅黑"} .time-box span{color:#f00; margin:0 10px;} </style> <div class="time-box"> <p><b>1、杯具,2014年已经过了:</b></p> <div id="time-count"></div> </div> <div class="time-box"> <p><b>2、杯具,离过年还有这么久啊:</b></p> <div id="time-down"></div> </div>javascript:
var i=0; function countTime(starT,obj){ i++; var time=null; if(new Date().getTime()>new Date(starT).getTime()){ time=(new Date().getTime()-new Date(starT).getTime()+1000*i)/1000; }else{ time=(new Date(starT).getTime()-new Date().getTime()-1000*i)/1000; } var day=Math.floor(time/(24*60*60)), hours=Math.floor(time/(60*60))-day*24, minutes=Math.floor(time/60)-(day*24*60)-(hours*60), seconds=Math.floor(time)-(day*24*60*60)-(hours*60*60)-(minutes*60); if(day<10){day="0"+day} if(hours<10){hours="0"+hours} if(minutes<10){minutes="0"+minutes} if(seconds<10){seconds="0"+seconds} document.getElementById(obj).innerHTML="<span class='day'>"+day+"</span>天<span class='hours'>"+hours+"</span>小时<span class='minutes'>"+minutes+"</span>分钟<span class='seconds'>"+seconds+"</span>秒"; setTimeout(function(){ countTime(starT,obj); },1000); } countTime("0:00:00 01/01/2014","time-count"); countTime("0:00:00 12/30/2016","time-down");效果如下: