当前位置:首页 > CSS

用css制作时钟

2026-04-02 00:17:57CSS

使用CSS制作时钟

以下是一个使用HTML和CSS制作简单时钟的方法。该方法利用CSS的transformanimation属性来实现时钟指针的旋转效果。

HTML结构

<div class="clock">
  <div class="hour-hand"></div>
  <div class="minute-hand"></div>
  <div class="second-hand"></div>
  <div class="center"></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;
}

.hour-hand {
  width: 6px;
  height: 60px;
  top: 50px;
  left: 97px;
  animation: rotate-hour 43200s linear infinite;
}

.minute-hand {
  width: 4px;
  height: 80px;
  top: 30px;
  left: 98px;
  animation: rotate-minute 3600s linear infinite;
}

.second-hand {
  width: 2px;
  height: 90px;
  top: 20px;
  left: 99px;
  background: red;
  animation: rotate-second 60s linear infinite;
}

.center {
  width: 12px;
  height: 12px;
  background: #333;
  border-radius: 50%;
  position: absolute;
  top: 94px;
  left: 94px;
}

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

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

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

说明

  • .clock定义了时钟的外框,包括大小、边框和圆角。
  • .hour-hand.minute-hand.second-hand分别定义了时针、分针和秒针的样式和位置。
  • transform-origin: bottom center确保指针围绕底部中心旋转。
  • animation属性通过@keyframes定义了指针的旋转动画,时间分别为12小时、60分钟和60秒。

改进建议

如果需要更精确的时间显示,可以结合JavaScript动态更新指针的角度,而不是依赖CSS动画。例如:

用css制作时钟

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动画的精确性。

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

相关文章

css如何制作三角形

css如何制作三角形

使用 border 属性制作三角形 通过设置元素的 border 属性,利用边框的透明特性可以创建三角形。将一个元素的宽度和高度设为 0,然后通过设置不同方向的边框宽度和颜色来实现。 .triang…

css制作开关

css制作开关

使用纯CSS制作开关 通过CSS的伪元素和input[type="checkbox"]可以创建自定义开关样式。关键点在于隐藏原生复选框,用CSS重新设计外观。 <label class="sw…

css右导航栏制作

css右导航栏制作

CSS右导航栏制作方法 固定定位法 使用position: fixed将导航栏固定在视窗右侧,结合right和top属性控制位置。示例代码: .right-navbar { position:…

css表格制作

css表格制作

CSS表格制作方法 使用CSS创建表格可以通过多种方式实现,以下是常见的几种方法: 使用HTML原生表格标签结合CSS样式 <table class="styled-table">…

css表单制作

css表单制作

表单基础结构 使用HTML创建表单容器,包含<form>标签及输入字段。表单通常需要<input>、<label>、<textarea>等元素。…

css 字体库制作

css 字体库制作

字体库制作方法 使用 @font-face 规则在 CSS 中嵌入自定义字体,确保网页显示特定字体。 基本语法 @font-face { font-family: 'MyCustomFont'…