当前位置:首页 > CSS

css制作时钟

2026-02-12 11:51:35CSS

使用CSS制作时钟

通过CSS和少量JavaScript可以创建一个动态时钟。以下是实现步骤:

css制作时钟

基本HTML结构

<div class="clock">
  <div class="hour-hand"></div>
  <div class="minute-hand"></div>
  <div class="second-hand"></div>
  <div class="center-dot"></div>
</div>

基础CSS样式

.clock {
  width: 200px;
  height: 200px;
  border: 10px solid #333;
  border-radius: 50%;
  position: relative;
  margin: 50px auto;
  background: #fff;
}

.hour-hand, .minute-hand, .second-hand {
  position: absolute;
  background: #333;
  transform-origin: bottom center;
  left: 50%;
  bottom: 50%;
}

.hour-hand {
  width: 6px;
  height: 60px;
  margin-left: -3px;
}

.minute-hand {
  width: 4px;
  height: 80px;
  margin-left: -2px;
}

.second-hand {
  width: 2px;
  height: 90px;
  margin-left: -1px;
  background: red;
}

.center-dot {
  width: 12px;
  height: 12px;
  background: #333;
  border-radius: 50%;
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -6px;
  margin-top: -6px;
  z-index: 10;
}

JavaScript动态更新

function updateClock() {
  const now = new Date();
  const hours = now.getHours() % 12;
  const minutes = now.getMinutes();
  const seconds = now.getSeconds();

  const hourDegrees = (hours * 30) + (minutes * 0.5);
  const minuteDegrees = minutes * 6;
  const secondDegrees = seconds * 6;

  document.querySelector('.hour-hand').style.transform = `rotate(${hourDegrees}deg)`;
  document.querySelector('.minute-hand').style.transform = `rotate(${minuteDegrees}deg)`;
  document.querySelector('.second-hand').style.transform = `rotate(${secondDegrees}deg)`;
}

setInterval(updateClock, 1000);
updateClock(); // 立即执行一次

添加刻度标记

/* 添加刻度标记 */
.clock::before {
  content: '';
  position: absolute;
  width: 4px;
  height: 15px;
  background: #333;
  left: 50%;
  top: 10px;
  margin-left: -2px;
  transform-origin: 50% 90px;
}

/* 创建12个刻度 */
.clock {
  position: relative;
}

.clock::before {
  content: '';
  position: absolute;
  width: 4px;
  height: 15px;
  background: #333;
  left: 50%;
  top: 10px;
  margin-left: -2px;
  transform-origin: 50% 90px;
}

/* 使用伪元素创建所有刻度 */
.clock::after {
  content: '';
  position: absolute;
  width: 2px;
  height: 8px;
  background: #666;
  left: 50%;
  top: 15px;
  margin-left: -1px;
  transform-origin: 50% 85px;
}

/* 创建60个刻度 */
.clock {
  counter-reset: ticks 60;
}

.clock::after {
  counter-increment: ticks -1;
  transform: rotate(calc(var(--i) * 6deg));
}

/* 需要JavaScript动态添加刻度 */

完整实现建议

  1. 将上述HTML、CSS和JavaScript代码整合到一个文件中
  2. 可以根据需要调整时钟大小、颜色和样式
  3. 考虑添加数字标记或更精美的设计
  4. 可以添加日期显示功能

这种方法创建的时钟会每秒更新一次,显示当前时间。CSS负责外观样式,JavaScript处理动态旋转逻辑。

标签: 时钟css
分享给朋友:

相关文章

怎么制作css

怎么制作css

创建CSS文件 新建一个文本文件,将文件扩展名改为.css。例如styles.css。确保文件名简洁且能反映其用途。 编写CSS基础结构 CSS由选择器和声明块组成。选择器用于指定要样式化的HTML…

css 制作按钮

css 制作按钮

基础按钮样式 使用CSS创建一个基础按钮需要定义padding、background-color、border和border-radius等属性。以下是一个简单示例: .button { pad…

css如何制作六边形

css如何制作六边形

使用CSS制作六边形 六边形可以通过CSS的clip-path属性或伪元素结合旋转和定位来实现。以下是两种常见方法: 方法一:使用clip-path属性 clip-path允许直接裁剪元素为六边形形…

制作css开关

制作css开关

使用纯CSS创建开关 HTML结构需要包含一个隐藏的复选框和一个关联的标签元素: <label class="switch"> <input type="checkbox"&g…

css制作春联

css制作春联

使用CSS制作春联的方法 基础布局与样式 春联通常由一对垂直排列的红色对联和顶部的横批组成。使用CSS的flexbox布局可以轻松实现这种结构。创建一个父容器,内部包含三个子元素:横批、上联和下联。…

css制作箭头

css制作箭头

使用边框制作箭头 通过设置元素的 border 属性,利用透明边框和实色边框的组合生成箭头。例如,创建一个向右的箭头: .arrow-right { width: 0; height:…