帮我写一段罗盘时钟的html代码
时间: 2023-08-08 21:09:49 浏览: 96
以下是一个简单的罗盘时钟的 HTML 代码:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>罗盘时钟</title>
<style>
#clock {
width: 400px;
height: 400px;
border-radius: 50%;
background-color: #f5f5f5;
position: relative;
}
#hour-hand, #minute-hand, #second-hand {
width: 6px;
height: 180px;
background-color: black;
position: absolute;
left: 50%;
top: 50%;
transform-origin: bottom center;
}
#hour-hand {
transform: rotate(0deg);
}
#minute-hand {
transform: rotate(0deg);
}
#second-hand {
width: 2px;
height: 190px;
background-color: red;
transform: rotate(0deg);
z-index: 2;
}
#center {
width: 12px;
height: 12px;
border-radius: 50%;
background-color: black;
position: absolute;
left: 50%;
top: 50%;
margin-left: -6px;
margin-top: -6px;
z-index: 3;
}
.mark {
width: 6px;
height: 20px;
background-color: black;
position: absolute;
left: 50%;
top: 10px;
transform-origin: bottom center;
}
.mark:nth-child(5) {
height: 30px;
}
.mark:nth-child(10) {
height: 40px;
}
.mark:nth-child(15) {
height: 50px;
}
.mark:nth-child(20) {
height: 60px;
}
.mark:nth-child(25) {
height: 70px;
}
.mark:nth-child(30) {
height: 80px;
}
.mark:nth-child(35) {
height: 90px;
}
.mark:nth-child(40) {
height: 100px;
}
.mark:nth-child(45) {
height: 110px;
}
.mark:nth-child(50) {
height: 120px;
}
.mark:nth-child(55) {
height: 130px;
}
.mark:nth-child(60) {
height: 140px;
}
</style>
</head>
<body>
<div id="clock">
<div id="hour-hand"></div>
<div id="minute-hand"></div>
<div id="second-hand"></div>
<div id="center"></div>
<div class="mark" style="transform: rotate(30deg);"></div>
<div class="mark" style="transform: rotate(60deg);"></div>
<div class="mark" style="transform: rotate(90deg);"></div>
<div class="mark" style="transform: rotate(120deg);"></div>
<div class="mark" style="transform: rotate(150deg);"></div>
<div class="mark" style="transform: rotate(180deg);"></div>
<div class="mark" style="transform: rotate(210deg);"></div>
<div class="mark" style="transform: rotate(240deg);"></div>
<div class="mark" style="transform: rotate(270deg);"></div>
<div class="mark" style="transform: rotate(300deg);"></div>
<div class="mark" style="transform: rotate(330deg);"></div>
<div class="mark" style="transform: rotate(360deg);"></div>
</div>
<script>
function updateClock() {
var now = new Date();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
var hourAngle = (hour % 12) * 30 + minute / 2;
var minuteAngle = minute * 6;
var secondAngle = second * 6;
document.getElementById("hour-hand").style.transform = "rotate(" + hourAngle + "deg)";
document.getElementById("minute-hand").style.transform = "rotate(" + minuteAngle + "deg)";
document.getElementById("second-hand").style.transform = "rotate(" + secondAngle + "deg)";
}
setInterval(updateClock, 1000);
</script>
</body>
</html>
```
该代码通过 HTML 和 CSS 实现了罗盘时钟的外观,通过 JavaScript 实现了时针、分针、秒针的动态变化。其中 HTML 代码包括一个 id 为 "clock" 的 div 元素,以及一系列 class 为 "mark" 的 div 元素用于显示刻度。CSS 代码通过设置元素的宽度、高度、背景色、位置等属性来实现罗盘时钟的外观。JavaScript 代码通过 setInterval 函数每秒钟更新时针、分针、秒针的角度,从而实现罗盘时钟的动态效果。
阅读全文