当前位置:首页 > CSS

css制作时钟

2026-01-13 12:22:11CSS

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;
  left: 50%;
  bottom: 50%;
  transform-origin: bottom center;
}

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

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

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

.center-dot {
  width: 12px;
  height: 12px;
  background: #333;
  border-radius: 50%;
  position: absolute;
  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 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();

纯CSS时钟(无JavaScript)

使用CSS动画也可以创建时钟,但需要预先定义所有可能的位置:

.second-hand {
  animation: rotate 60s linear infinite;
}

.minute-hand {
  animation: rotate 3600s linear infinite;
}

.hour-hand {
  animation: rotate 43200s linear infinite;
}

@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

添加时钟刻度

.clock::before {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 50%;
}

.clock::after {
  content: '';
  position: absolute;
  width: 90%;
  height: 90%;
  top: 5%;
  left: 5%;
  border-radius: 50%;
  background: #fff;
}

/* 添加12个刻度 */
.clock .tick {
  position: absolute;
  width: 2px;
  height: 10px;
  background: #333;
  left: 50%;
  top: 0;
  margin-left: -1px;
  transform-origin: bottom center;
}

/* 创建12个刻度元素 */
<div class="clock">
  <!-- 之前的指针元素 -->
  <div class="tick" style="transform: rotate(0deg)"></div>
  <div class="tick" style="transform: rotate(30deg)"></div>
  <!-- 继续添加其他刻度... -->
</div>

这些方法可以创建一个功能完整的CSS时钟,第一种方法使用JavaScript实现实时更新,第二种方法使用纯CSS动画但时间不会与实际时间同步。可以根据需求选择适合的实现方式。

css制作时钟

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

相关文章

react 如何引入css

react 如何引入css

在 React 中引入 CSS 的方法 React 提供了多种引入 CSS 的方式,可以根据项目需求选择合适的方法。以下是常见的几种方式: 内联样式 内联样式直接在组件中通过 style 属性定义…

css制作

css制作

CSS 基础语法 CSS(层叠样式表)用于控制网页的样式和布局。其基本语法由选择器和声明块组成。选择器指定要样式化的HTML元素,声明块包含一个或多个属性-值对,用花括号 {} 包裹。 select…

css网页制作教程

css网页制作教程

CSS网页制作教程 CSS(层叠样式表)是用于控制网页外观和布局的核心技术。以下是CSS网页制作的关键步骤和技巧: 基础语法与引入方式 CSS规则由选择器和声明块组成: 选择器 { 属性:…

css如何制作圆

css如何制作圆

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

网页制作 css

网页制作 css

CSS 基础语法 CSS(层叠样式表)用于控制网页的样式和布局。基本语法由选择器和声明块组成: 选择器 { 属性: 值; } 例如设置段落文字颜色为红色: p { color: red…

css表格制作表格

css表格制作表格

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