首页 > 编程语言 > js实现动态时钟
2020
09-24

js实现动态时钟

本文实例为大家分享了js实现动态时钟的具体代码,供大家参考,具体内容如下

<!DOCTYPE html>
<html>
<head>
 <title>时钟</title>
</head>
<script type="text/javascript">
 function todou(n){
 if(n<10){
  return '0'+n;
 }
 else {
  return ''+n;
 }
 }

 window.onload=function(){
 setInterval(function(){
   var date = new Date();
 
 var ma = document.getElementsByTagName('img');
  var str =todou(date.getHours())+todou(date.getMinutes())+todou(date.getSeconds());
  for(var i=0;i<ma.length;i++){
  ma[i].src='images/'+str[i]+'.png';
  }
  // alert(todou(date.getSeconds()));
 // var date = new Date();
 //alert(todou(date.getSeconds()));
 }  ,1000)
    
 }
</script>
<body>

<div style="background: grey;color: red; text-align: center;font-size: 100px;" >
 <img src="images/0.png" width="100px" height="90px">
 <img src="images/0.png" width="100px" height="90px">
  时
 <img src="images/0.png" width="100px" height="90px">
 <img src="images/0.png" width="100px" height="90px">
 分
 <img src="images/0.png" width="100px" height="90px">
 <img src="images/0.png" width="100px" height="90px">
  秒
</div>
</body>
</html>

示例展示:

更多JavaScript时钟特效点击查看:JavaScript时钟特效专题

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

编程技巧