本文实例讲述了js实现的网页倒计时数字时钟效果。分享给大家供大家参考。具体实现方法如下:
javascript实现的倒计时时钟body,div{margin:0;padding:0;}
body{color:#fff;font:16px/1.5 \5fae\8f6f\96c5\9ed1;}
#countdown{width:300px;text-align:center;background:#1a1a1a;margin:10px auto;padding:20px 0;}
input{border:0;width:283px;height:50px;cursor:pointer;margin-top:20px;background:url(//www.jb51.net/jscss/demoimg/201210/btn-1.png) no-repeat;}
input.cancel{background-position:0 -50px;}
span{color:#000;width:80px;line-height:2;background:#fbfbfb;border:2px solid #b4b4b4;margin:0 10px;padding:0 10px;}
window.onload = function ()
{
var ocountdown = document.getelementbyid("countdown");
var ainput = ocountdown.getelementsbytagname("input")[0];
var timer = null;
ainput.onclick = function ()
{
this.classname == "" ? (timer = setinterval(updatetime, 1000), updatetime()) : (clearinterval(timer));
this.classname = this.classname == '' ? "cancel" : '';
};
function format(a)
{
return a.tostring().replace(/^(\d)$/,'0$1')
}
function updatetime ()
{
var aspan = ocountdown.getelementsbytagname("span");
var oremain = aspan[0].innerhtml.replace(/^0/,'') * 60 + parseint(aspan[1].innerhtml.replace(/^0/,''));
if(oremain <= 0)
{
clearinterval(timer);
return
}
oremain--;
aspan[0].innerhtml = format(parseint(oremain / 60));
oremain %= 60;
aspan[1].innerhtml = format(parseint(oremain));
}
}
01分钟40秒
通过本文的了解。希望本文所述对大家js程序设计有所帮助。
希望与广大网友互动??
点此进行留言吧!