当前位置:首页 > CSS

css怎么制作时钟

2026-01-08 18:40:36CSS

使用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;
}

.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 {
  position: absolute;
  width: 12px;
  height: 12px;
  background: #333;
  border-radius: 50%;
  top: 50%;
  left: 50%;
  margin: -6px 0 0 -6px;
}

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 {
  /* 基础样式同上 */
}

.clock::before {
  content: '';
  position: absolute;
  width: 6px;
  height: 60px;
  background: #333;
  left: 50%;
  bottom: 50%;
  margin-left: -3px;
  transform: rotate(30deg); /* 指向3点 */
  transform-origin: bottom center;
}

.clock::after {
  content: '';
  position: absolute;
  width: 4px;
  height: 80px;
  background: #333;
  left: 50%;
  bottom: 50%;
  margin-left: -2px;
  transform: rotate(180deg); /* 指向6点 */
  transform-origin: bottom center;
}

添加刻度标记

为时钟添加小时刻度:

.clock .mark {
  position: absolute;
  width: 3px;
  height: 10px;
  background: #333;
  left: 50%;
  bottom: 100%;
  margin-left: -1.5px;
  transform-origin: bottom center;
}

/* 通过旋转创建12个刻度 */
.clock .mark:nth-child(1) { transform: rotate(30deg); }
.clock .mark:nth-child(2) { transform: rotate(60deg); }
/* ...继续添加其他刻度 */

这些方法可以组合使用,创建出不同风格的CSS时钟效果。动态版本需要JavaScript配合更新指针角度,而静态版本则适合展示固定时间。

css怎么制作时钟

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

相关文章

css如何制作圆

css如何制作圆

使用 border-radius 属性制作圆 通过设置 border-radius 属性为元素添加圆角效果。当 border-radius 的值等于元素宽度和高度的一半时,元素会呈现圆形。 .ci…

css表格制作表格

css表格制作表格

CSS 表格制作方法 使用 CSS 创建表格可以通过多种方式实现,包括原生 HTML 表格元素结合 CSS 样式,或者使用纯 CSS 布局技术(如 Flexbox 或 Grid)模拟表格效果。以下是常…

css制作图标

css制作图标

使用CSS制作图标的方法 使用伪元素和边框 通过CSS的::before和::after伪元素结合border属性可以创建简单的几何形状图标。例如制作一个三角形: .triangle { w…

纯css制作tab菜单

纯css制作tab菜单

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

css制作tab菜单

css制作tab菜单

CSS制作Tab菜单的方法 HTML结构 使用无序列表和div容器构建基础结构,列表项为标签页标题,div内放置对应内容块。 <ul class="tab-menu"> <li…

css制作扇形图

css制作扇形图

使用CSS制作扇形图 方法一:使用clip-path和transform 通过CSS的clip-path和transform属性可以快速实现扇形效果。定义一个圆形元素,然后裁剪出扇形部分。 .fan…