Js : 封装对象显示倒计时...........
时间:2010-09-26 来源:且行且思
timeCountDown.js 对象插件:
代码 /* by zhangxinxu 2010-07-27
* http://www.zhangxinxu.com/
* 倒计时的实现
*/
var fnTimeCountDown = function(d, o){
var f = {
zero: function(n){
var n = parseInt(n, 10);
if(n > 0){
if(n <= 9){
n = "0" + n;
}
return String(n);
}else{
return "00";
}
},
dv: function(){
d = d || Date.UTC(2050, 0, 1); //如果未定义时间,则我们设定倒计时日期是2050年1月1日
var future = new Date(d), now = new Date();
//现在将来秒差值
var dur = Math.round((future.getTime() - now.getTime()) / 1000) + future.getTimezoneOffset() * 60, pms = {
sec: "00",
mini: "00",
hour: "00",
day: "00",
month: "00",
year: "0"
};
if(dur > 0){
pms.sec = f.zero(dur % 60);
pms.mini = Math.floor((dur / 60)) > 0? f.zero(Math.floor((dur / 60)) % 60) : "00";
pms.hour = Math.floor((dur / 3600)) > 0? f.zero(Math.floor((dur / 3600)) % 24) : "00";
pms.day = Math.floor((dur / 86400)) > 0? f.zero(Math.floor((dur / 86400)) % 30) : "00";
//月份,以实际平均每月秒数计算
pms.month = Math.floor((dur / 2629744)) > 0? f.zero(Math.floor((dur / 2629744)) % 12) : "00";
//年份,按按回归年365天5时48分46秒算
pms.year = Math.floor((dur / 31556926)) > 0? Math.floor((dur / 31556926)) : "0";
}
return pms;
},
ui: function(){
if(o.sec){
o.sec.innerHTML = f.dv().sec;
}
if(o.mini){
o.mini.innerHTML = f.dv().mini;
}
if(o.hour){
o.hour.innerHTML = f.dv().hour;
}
if(o.day){
o.day.innerHTML = f.dv().day;
}
if(o.month){
o.month.innerHTML = f.dv().month;
}
if(o.year){
o.year.innerHTML = f.dv().year;
}
setTimeout(f.ui, 1000);
}
};
f.ui();
};
倒计时 - 实例
代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>艾纳科技整理www.ainatec.com-JQ倒计时</title>
<style type="text/css">
.fl{ float:left;}span{ float:left}
</style>
<script type="text/javascript" src="http://www.gzse7en.com/js/timeCountDown.js"></script>
</head>
<body>
<div class="fl">
<div id="year" class="year fl">0</div><span>年</span>
<div id="month" class="month fl">00</div><span>月</span>
<div id="day" class="day fl">00</div><span>日</span>
<div id="hour" class="hour fl">00</div><span>时</span>
<div id="mini" class="fl mini ml5">00</div><span>分</span>
<div id="sec" class="sec fl">00</div><span>秒</span>
</div>
<script type="text/javascript">
var zxx = {
$: function(id){
return document.getElementById(id);
},
futureDate: Date.UTC(2050, 7, 30, 12,0,0),
obj: function(){
return {
sec: zxx.$("sec"),
mini: zxx.$("mini"),
hour: zxx.$("hour"),
day: zxx.$("day"),
month: zxx.$("month"),
year: zxx.$("year")
}
}
};
fnTimeCountDown(zxx.futureDate, zxx.obj());
</script>
</body>
</html>
相关阅读 更多 +