萬盛學電腦網

 萬盛學電腦網 >> 腳本專題 >> javascript >> js 倒計時 代碼

js 倒計時 代碼

 <body>

<div id='res'><span id='d'></span><span id="h"></span><span id="m"></span><span id="s"></span></div>

</body>
<script src="./jquery-1.4.2.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">
var now = new Date(); //當前時間
//2012-07-26 16:22:53
var EndTime = new Date(now.getTime()+1000*60*60*24*2); //當前時間+2天 注意 js中getTime()是以毫秒計算 所以*1000 php是以秒計算
function timer(){
now = new Date();
var t = EndTime.getTime()-now.getTime();
if(t > 0){
var s = Math.floor(t/1000)%60; //秒
var m = Math.floor(t/(1000*60))%60; //分
var h = Math.floor(t/(1000*60*60))%24; //時
var d = Math.floor(t/(1000*60*60*24)); //天
s = (s<10)? '0'+s:s;
document.getElementById("d").innerHTML=d+"天";
document.getElementById("h").innerHTML=h+"小時";
document.getElementById("m").innerHTML=m+"分";
document.getElementById("s").innerHTML=s+"秒";

}else{ //到時間後邊框陰影發光

if(t % 2 == 0){
$('#res').css({"box-shadow":" 0 0 12px rgba(72, 106, 170, 0.5)","-o-box-shadow":" 0 0 22px rgba(72, 106, 170, 0.5)","-moz-box-shadow":" 0 0 12px rgba(72, 106, 170, 0.5)","-webkit-box-shadow":" 0 0 12px rgba(72, 106, 170, 0.5)","border-color":"rgba(160, 179, 214, 0.5)"});
}else{
$('#res').css({"box-shadow":"0 0 12px rgba(72, 106, 170, 1.0), 0 0 18px rgba(0, 140, 255, 1.0)","-o-box-shadow":"0 0 12px rgba(72, 106, 170, 1.0), 0 0 18px rgba(0, 140, 255, 1.0)","-moz-box-shadow":"0 0 22px rgba(72, 106, 170, 1.0), 0 0 18px rgba(0, 140, 255, 1.0)","-webkit-box-shadow":"0 0 12px rgba(72, 106, 170, 1.0), 0 0 18px rgba(0, 140, 255, 1.0)","border-color":"rgba(160, 179, 214, 1.0)"});
}
}
}
setInterval("timer()",1000);
</script>

copyright © 萬盛學電腦網 all rights reserved