时钟制作

首先需要10张图片,上面是分别是0-9的数字,获得时间,将时间变为字符串,在循环,让相应的图片显示,达到时钟的目的,需要用到定时器

<!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>定时时钟</title>

<script>
function toDou(n){//将编程字符串
	if(n<10){
		return '0'+n;//例如03
	}else{
		return ''+n;//例如11
	}
}
window.onload=function (){
	var aImg=document.getElementsByTagName('img');//获得图片元素的集合
	function tick(){
		var oDate=new Date();//获得当前时间
		var str=toDou(oDate.getHours())+toDou(oDate.getMinutes())+toDou(oDate.getSeconds());
		//str='311232';//将当前的时分秒变为相应的字符串
		for(var i=0;i<aImg.length;i++){//for循环,根据字符串各位的数字变换相应的图像
			aImg[i].src='img/'+str[i]+'.png';
		}
		
	}
	setInterval(tick,1000);//一秒执行一次,变一次图片
	tick();//先执行函数,可以避免刷新时全变为0
}</script>
</head>

<body style="background:#0F0; color:#FC0; font-size:50px;">

<img src="img/0.png"/>
<img src="img/0.png"/>
:
<img src="img/0.png"/>
<img src="img/0.png"/>
:
<img src="img/0.png"/>
<img src="img/0.png"/>

</body>
</html>

 

posted @ 2015-06-20 15:59  飘逸110  阅读(205)  评论(0编辑  收藏  举报