当前位置:首页 > 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
分享给朋友:

相关文章

react 如何引入css

react 如何引入css

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

css制作

css制作

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

css制作三角形

css制作三角形

使用 border 属性创建三角形 通过设置元素的 border 属性可以实现三角形效果。原理是利用边框的交界处形成的斜边。 .triangle { width: 0; height: 0…

css网页制作教程

css网页制作教程

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

制作css

制作css

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

css 制作导航

css 制作导航

基础导航栏制作 使用HTML和CSS创建一个水平导航栏。HTML结构通常使用<ul>和<li>标签,CSS负责样式布局。 <nav> <ul clas…