当前位置:首页 > 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;
}

添加刻度标记

为时钟添加小时刻度:

css怎么制作时钟

.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 属性,利用透明边框和实色边框的组合生成箭头。例如,创建一个向右的箭头: .arrow-right { width: 0; hei…

用css制作网页

用css制作网页

创建HTML文件结构 新建一个index.html文件,写入基础HTML5结构。通过<link>标签引入外部CSS文件,示例结构如下: <!DOCTYPE html> <…

友情链接css制作

友情链接css制作

友情链接CSS制作 友情链接是网站之间互相推广的一种方式,通常以文字或图片形式展示。通过CSS可以美化友情链接的样式,使其更符合网站整体设计风格。 文字链接样式 设置文字链接的基础样式,包括颜色、…

css如何制作响应网页

css如何制作响应网页

响应式网页设计基础 使用CSS制作响应式网页的核心是通过媒体查询(Media Queries)和弹性布局(Flexbox/Grid)实现。媒体查询允许根据设备特性(如屏幕宽度)应用不同的样式规则。…

css如何制作未读图标

css如何制作未读图标

未读图标实现方法 通过CSS可以创建多种未读图标的样式,以下是几种常见的实现方式: 使用伪元素创建小红点 .unread-badge { position: relative; disp…

css 制作表格

css 制作表格

CSS 制作表格的方法 使用 CSS 制作表格可以通过多种方式实现,包括原生 HTML 表格样式、Flexbox 布局或 Grid 布局。以下是几种常见的方法: 原生 HTML 表格样式 HTML…