用css制作时间
使用CSS制作时钟
通过CSS和少量JavaScript可以创建一个动态的时钟效果。以下是实现方法:
HTML结构

<div class="clock">
<div class="hour-hand"></div>
<div class="minute-hand"></div>
<div class="second-hand"></div>
<div class="center"></div>
</div>
CSS样式
.clock {
width: 200px;
height: 200px;
border: 10px solid #333;
border-radius: 50%;
position: relative;
background: white;
}
.hour-hand, .minute-hand, .second-hand {
position: absolute;
left: 50%;
bottom: 50%;
transform-origin: bottom center;
}
.hour-hand {
width: 6px;
height: 60px;
background: #333;
margin-left: -3px;
}
.minute-hand {
width: 4px;
height: 80px;
background: #666;
margin-left: -2px;
}
.second-hand {
width: 2px;
height: 90px;
background: red;
margin-left: -1px;
}
.center {
width: 12px;
height: 12px;
background: #333;
border-radius: 50%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
JavaScript动态效果

function updateClock() {
const now = new Date();
const hours = now.getHours() % 12;
const minutes = now.getMinutes();
const seconds = now.getSeconds();
const hourDeg = (hours * 30) + (minutes * 0.5);
const minuteDeg = minutes * 6;
const secondDeg = seconds * 6;
document.querySelector('.hour-hand').style.transform = `rotate(${hourDeg}deg)`;
document.querySelector('.minute-hand').style.transform = `rotate(${minuteDeg}deg)`;
document.querySelector('.second-hand').style.transform = `rotate(${secondDeg}deg)`;
}
setInterval(updateClock, 1000);
updateClock();
纯CSS动画时钟
如果希望不使用JavaScript,可以使用CSS动画创建简化版时钟:
.clock {
/* 基础样式同上 */
}
.second-hand {
animation: rotate 60s linear infinite;
}
.minute-hand {
animation: rotate 3600s linear infinite;
}
.hour-hand {
animation: rotate 43200s linear infinite;
}
@keyframes rotate {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
添加时钟刻度
为时钟添加刻度标记:
.clock::before {
content: '';
position: absolute;
width: 100%;
height: 100%;
border-radius: 50%;
}
.clock::after {
content: '';
position: absolute;
width: 100%;
height: 100%;
border-radius: 50%;
transform: rotate(30deg);
}
/* 添加12个刻度点 */
.clock::before,
.clock::after {
background:
linear-gradient(0deg, transparent 45%, #333 45%, #333 55%, transparent 55%),
linear-gradient(30deg, transparent 45%, #333 45%, #333 55%, transparent 55%),
linear-gradient(60deg, transparent 45%, #333 45%, #333 55%, transparent 55%),
linear-gradient(90deg, transparent 45%, #333 45%, #333 55%, transparent 55%),
linear-gradient(120deg, transparent 45%, #333 45%, #333 55%, transparent 55%),
linear-gradient(150deg, transparent 45%, #333 45%, #333 55%, transparent 55%);
}
这些方法可以创建出不同风格的CSS时钟效果,根据需求选择适合的实现方式。






