当前位置:首页 > CSS

css制作时钟

2026-02-12 11:51:35CSS

使用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-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基础语法 CSS规则由选择器和声明块组成。选择器指向需要设置样式的HTML元素,声明块包含一个或多个用分号分隔的声明。每个声明由属性和值组成,用冒号分隔。 选择器 { 属性: 值;…

css制作下拉菜单

css制作下拉菜单

CSS 制作下拉菜单的方法 使用纯 CSS 可以创建简单的下拉菜单效果,主要依赖 :hover 伪类和 display 属性控制子菜单的显示与隐藏。 基础 HTML 结构 <div c…

纯css制作tab菜单

纯css制作tab菜单

纯CSS制作Tab菜单的方法 使用radio input控制切换 通过<input type="radio">和<label>元素配合实现无JavaScript的Tab切换效果…

css制作详情页

css制作详情页

CSS制作详情页的关键步骤 布局规划 确定详情页的整体布局结构,通常包括头部、商品展示区、参数区、评价区等模块。使用Flexbox或Grid布局实现响应式设计,确保在不同设备上正常显示。 样式设计…

css制作圆

css制作圆

使用CSS制作圆形元素 通过CSS的border-radius属性可以轻松将元素变为圆形。以下是具体实现方法: 基本圆形实现 .circle { width: 100px; height:…

css怎么制作时钟

css怎么制作时钟

使用CSS制作时钟 通过CSS和少量JavaScript可以创建一个动态时钟。以下是实现方法: HTML结构 <div class="clock"> <div class…