使用JS显示倒计时数字时钟效果
程序员文章站
2022-06-29 13:18:53
本文实例讲述了js实现的网页倒计时数字时钟效果。分享给大家供大家参考。具体实现方法如下:
本文实例讲述了js实现的网页倒计时数字时钟效果。分享给大家供大家参考。具体实现方法如下:
<!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>javascript实现的倒计时时钟</title> <style> 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;} </style> <script> 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)); } } </script> </head> <body> <div id="countdown"> <span>01</span>分钟<span>40</span>秒 <input type="button" value="" /> </div> </body> </html>
通过本文的了解。希望本文所述对大家js程序设计有所帮助。
下一篇: JSP加载JS文件不起作用的有效解决方法