JaveScript 之 即时钟表
时钟
自己刚做的时钟,有兴趣的可以看看
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<style type="text/css">
#wrap{
width: 240px;
height: 240px;
border-radius: 120px;
background: cyan;
margin: 50px auto;
position: relative;
}
#clock{
width: 200px;
height: 200px;
border-radius: 50%;
background: white;
position: absolute;
left: 20px;
top: 20px;
}
#bNumber{
width: 100%;
height: 100%;
position: relative;
}
#bNumber div{
width: 190px;
height: 20px;
position: absolute;
left: 10px;
top:90px;
}
#bNumber span{
display: block;
width: 20px;
height: 20px;
}
.number{
position: absolute;
bottom:100px;
transform-origin: 50% 90%;
}
#hour{
width: 5px;
height: 60px;
left:100px;
background: black;
}
#minute{
width: 3px;
height: 80px;
left: 101px;
background: gray;
}
#second{
width: 1px;
height: 90px;
left: 102px;
background: red;
}
</style>
<body>
<!--表盘-->
<div id="wrap">
<!--钟表-->
<div id="clock">
<!--指针数-->
<div id="bNumber">
<div><span>9</span></div>
<div><span>10</span></div>
<div><span>11</span></div>
<div><span>12</span></div>
<div><span>1</span></div>
<div><span>2</span></div>
<div><span>3</span></div>
<div><span>4</span></div>
<div><span>5</span></div>
<div><span>6</span></div>
<div><span>7</span></div>
<div><span>8</span></div>
</div>
<!--时针-->
<div id="hour" class="number"></div>
<!--分针-->
<div id="minute" class="number"></div>
<!--秒针-->
<div id="second" class="number"></div>
</div>
</div>
</body>
<script type="text/javascript">
var bNumber = document.getElementById('bNumber');
var divs = bNumber.getElementsByTagName('div');
var spans = bNumber.getElementsByTagName('span')
//通过旋转,重置钟点数的位置
for(var i = 0;i < divs.length;i++){
divs[i].style.transform = 'rotate(' + i * 30 + 'deg)';
}
//通过旋转,重置字的位置
for(var i = 0;i < divs.length;i++){
spans[i].style.transform = 'rotate(' + i * -30 + 'deg)';
}
var hour = document.getElementById('hour');
var minute = document.getElementById('minute');
var second = document.getElementById('second');
function colorNumber (){
//获取系统时间
var nowTime = new Date();
var nowHour = nowTime.getHours();
var nowMinute = nowTime.getMinutes();
var nowSecond = nowTime.getSeconds();
var minuteDeg = nowMinute / 60 * 30;
//时针旋转角度
hour.style.transform = 'rotate(' + (nowHour * 30 + minuteDeg) + 'deg)';
var secondDeg = nowSecond / 60 * 6;
//分针旋转角度
minute.style.transform = 'rotate(' + (nowMinute * 6 + secondDeg) + 'deg)';
//秒针旋转角度
second.style.transform = 'rotate(' + nowSecond * 6 + 'deg)';
}
colorNumber();
setInterval('colorNumber()',1000)
</script>
</html>
点击查看更多内容
31人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦