以下是一个使用 JavaScript 实现的圆形走动数字时钟的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>圆形走动数字时钟</title>
<style>
#clock {
width: 200px;
height: 200px;
border-radius: 50%;
background-color: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
font-size: 30px;
font-family: 'Arial', sans-serif;
}
</style>
</head>
<body>
<div id="clock"></div>
<script>
function updateClock() {
const now = new Date();
const hours = now.getHours().toString().padStart(2, '0');
const minutes = now.getMinutes().toString().padStart(2, '0');
const seconds = now.getSeconds().toString().padStart(2, '0');
const timeString = `${hours}:${minutes}:${seconds}`;
const clockElement = document.getElementById('clock');
clockElement.textContent = timeString;
// 计算角度并旋转数字
const angle = (new Date().getSeconds() / 60) * 360;
clockElement.style.transform = `rotate(${angle}deg)`;
}
setInterval(updateClock, 1000);
updateClock(); // 初始化时钟
</script>
</body>
</html>
基础概念:
Date
对象用于获取当前的时间。setInterval
函数用于每隔一定时间执行指定的函数。优势:
类型:
应用场景:
常见问题及解决方法:
updateClock
函数进行初始化。领取专属 10元无门槛券
手把手带您无忧上云