JavaScript编写开发动态时钟
作者:https://blog.csdn.net/qq_46537431/article/details/107613758
这篇文章主要为大家详细介绍了JavaScript编写开发动态时钟,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了JavaScript编写开发动态时钟的具体代码,供大家参考,具体内容如下
效果图:


实质上就是调用时间库,再添加一个颜色数组,给显示的时间嵌套一个div盒子,再将颜色数组的颜色设置随机变化,这样就使得时间变化的时候颜色也会发生变化。
完整源代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>网页时钟</title>
<style>
</style>
</head>
<style>
.center{
background: url(img/shizhong.jpg) no-repeat center;
font-size: 50px;
height: 600px;
line-height: 620px;
text-align: center;
}
</style>
<body>
<div class="center" id = "spanTip"></div>
</body>
<script>
//格式化时间 给前面加个0;
function addZero(num){ return num < 10? '0' + num : num; }
function genDate(){
var date = new Date();
//获取时间
var dateStr =addZero(date.getHours()) +":"+addZero(date.getMinutes())+":"+addZero(date.getSeconds());
var spanTip = document.getElementById("spanTip");
spanTip.innerHTML = dateStr;
//颜色数组
var color = ['red', 'green', 'yellow', 'blue', 'black', 'gold', 'orange', 'gray', 'pink', 'maroon']; //钟表颜色数组
var radom = Math.floor(Math.random() * color.length ); //随机数
spanTip.style.color = color[radom]; //设置随机颜色
}
window.setInterval("genDate()",1000);
</script>
</html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
