<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery+CSS3模拟时钟</title>
<style>
body {
background: #f5f5f5;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
}
.clock-container {
text-align: center;
}
.clock {
width: 200px;
height: 200px;
border-radius: 50%;
background: #fff;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
position: relative;
margin: 0 auto 30px;
}
.clock-face {
width: 100%;
height: 100%;
position: relative;
}
.hour-marks {
position: absolute;
width: 100%;
height: 100%;
padding: 15px 0px
;
}
.hour-mark {
position: absolute;
width: 3px;
height: 9px;
background: #333;
left: 50%;
margin-left: -1.5px;
transform-origin: 50% 85px;
}
.hand {
position: absolute;
left: 50%;
bottom: 50%;
transform-origin: 50% 100%;
transition: transform 0.3s cubic-bezier(0.4, 2.3, 0.3, 1);
}
.hour-hand {
width: 6px;
height: 50px;
margin-left: -3px;
background: #333;
border-radius: 3px;
}
.minute-hand {
width: 4px;
height: 70px;
margin-left: -2px;
background: #666;
border-radius: 2px;
}
.second-hand {
width: 2px;
height: 80px;
margin-left: -1px;
background: #e74c3c;
border-radius: 1px;
}
.center-circle {
position: absolute;
width: 12px;
height: 12px;
background: #e74c3c;
border-radius: 50%;
left: 50%;
top: 50%;
margin-left: -6px;
margin-top: -6px;
z-index: 10;
}
.digital-clock {
font-size: 24px;
color: #333;
margin-top: 20px;
}
.credit {
margin-top: 30px;
font-size: 12px;
color: #999;
}
.credit a {
color: #3498db;
text-decoration: none;
}
</style>
</head>
<body>
<div class="clock-container">
<div class="clock">
<div class="clock-face">
<div class="hour-marks" id="hour-marks"></div>
<div class="hand hour-hand" id="hour-hand"></div>
<div class="hand minute-hand" id="minute-hand"></div>
<div class="hand second-hand" id="second-hand"></div>
<div class="center-circle"></div>
</div>
</div>
<div class="digital-clock" id="digital-clock"></div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
// 创建时钟刻度
for (var i = 0; i < 12; i++) {
$('#hour-marks').append('<div class="hour-mark" style="transform: rotate(' + (i * 30) + 'deg)"></div>');
}
function updateClock() {
var now = new Date();
var hours = now.getHours() % 12;
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var milliseconds = now.getMilliseconds();
// 计算角度
var hourDegrees = (hours * 30) + (minutes * 0.5);
var minuteDegrees = (minutes * 6) + (seconds * 0.1);
var secondDegrees = (seconds * 6) + (milliseconds * 0.006);
// 应用旋转
$('#hour-hand').css('transform', 'rotate(' + hourDegrees + 'deg)');
$('#minute-hand').css('transform', 'rotate(' + minuteDegrees + 'deg)');
$('#second-hand').css('transform', 'rotate(' + secondDegrees + 'deg)');
// 更新数字时钟
var digitalTime = now.getHours().toString().padStart(2, '0') + ':' +
now.getMinutes().toString().padStart(2, '0') + ':' +
now.getSeconds().toString().padStart(2, '0');
$('#digital-clock').text(digitalTime);
// 使用requestAnimationFrame实现平滑动画
requestAnimationFrame(updateClock);
}
// 初始调用
updateClock();
});
</script>
</body>
</html>