Welcome to Delicate template
Header
Just another WordPress site
Header

js倒计时代码

9月 27th, 2013 | Posted by 无 名 in js

将倒计时写成了js函数,来调用。
代码如下:

var now = new Date();
var grt= new Date("9/30/2013 20:00:00");
var ct = grt - now;
mytime.countdownTime(ct,3,2);


html显示代码:

<div id="remainTime_g_2" style="color:#000000; width:100%; text-align:center; font-size:45px;"></div>

倒计时js函数:


var mytime = {};
mytime.countdownTime = function(f, d, a) {
this.remainSec = f / 1000;
if (this.remainSec >= 0) {
var c = Math.floor(this.remainSec / (60 * 60 * 24));
var b = Math.floor(this.remainSec / (60 * 60)) – (c * 24);
var h = Math.floor(this.remainSec / 60) – (c * 24 * 60) – (b * 60);
var e = Math.floor(this.remainSec) – (c * 24 * 60 * 60) – (b * 60 * 60) – (h * 60);
if (b >= 0 && b <= 9) { b = "0" + b } if (h >= 0 && h <= 9) { h = "0" + h } if (e >= 0 && e <= 9) { e = "0" + e } msg = "" + b + "时" + h + "分"; if (c != 0) { msg = "" + c + "天" + msg } msg = msg + e + "秒"; if (d == "1") { msg = "距离开始:" + msg } else { msg = "剩余:" + msg } if (c == 0 && b == 0 && h == 0 && e == 0) { if (d != undefined && d == 1) { if (document.getElementById("li_" + a)) { var g = $("#li_" + a); g.find("div:first").attr("class", ""); g.find("#g_button").html("去看看"); g.find("#g_button").attr("onclick", null) } } } else { if (document.getElementById("remainTime_g_" + a)) { document.getElementById("remainTime_g_" + a).innerHTML = msg; setTimeout("mytime.countdownTime(" + (--this.remainSec) * 1000 + "," + d + ",'" + a + "')", 1000) } } } }; [/javascript] 参数释义,function(f, d, a) f:设定时间减去当前时间的一个时间差,类型为时间戳。 d:开始或者剩余,当为1时,表示距离活动开始还有多久,当倒计时恰好为0时,显示活动,可以点‘去看看’进去了。当不为1时,其它随机数,表示剩余倒计时。 a:要显示倒计时容器的id号。id前缀为“remainTime_g_”。

You can follow any responses to this entry through the RSS 2.0 Both comments and pings are currently closed.