当前位置:首页 > CSS

用css制作时钟

2026-03-12 04:14:12CSS

使用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"></div>
</div>

CSS样式

.clock {
  width: 200px;
  height: 200px;
  border: 10px solid #333;
  border-radius: 50%;
  position: relative;
  margin: 50px auto;
  background: white;
}

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

添加时钟刻度

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

.clock::after {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  transform: rotate(30deg);
}

.clock::before, .clock::after {
  background: linear-gradient(
    to right,
    transparent 48%,
    #333 48%,
    #333 52%,
    transparent 52%
  );
}

优化版本(使用CSS变量)

:root {
  --clock-size: 200px;
  --clock-border: 10px;
}

.clock {
  width: var(--clock-size);
  height: var(--clock-size);
  border: var(--clock-border) solid #333;
  /* 其余样式保持不变 */
}

纯CSS时钟(无JavaScript)

虽然无法完全动态更新,但可以使用CSS动画模拟:

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

以上代码组合可以创建一个功能完整的CSS时钟。HTML提供结构,CSS负责样式和基本动画,JavaScript实现动态时间更新。可以根据需要调整尺寸、颜色和样式细节。

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

相关文章

css导航制作

css导航制作

基础导航栏制作 使用HTML的无序列表 <ul> 和 <li> 构建导航结构,CSS清除默认样式并横向排列: <nav> <ul class="navb…

css 字体库制作

css 字体库制作

创建 CSS 字体库的步骤 准备字体文件 确保拥有字体文件的合法使用权,常见的格式包括 .woff、.woff2、.ttf 或 .otf。推荐使用 woff2 格式,因其压缩率更高且现代浏览器广泛支持…

css雪碧图制作

css雪碧图制作

CSS雪碧图制作方法 CSS雪碧图(CSS Sprite)是一种将多个小图标或背景图像合并到一张大图中的技术,通过减少HTTP请求提升网页性能。以下是制作和使用雪碧图的详细方法: 准备图像素材 收集…

css制作锁屏页面

css制作锁屏页面

使用CSS制作锁屏页面 锁屏页面通常包含一个背景、时间显示以及可能的解锁按钮或输入框。以下是实现锁屏页面的关键CSS代码和结构。 HTML结构 <!DOCTYPE html> <…

css如何制作六边形

css如何制作六边形

使用CSS制作六边形 六边形可以通过CSS的clip-path属性或伪元素结合旋转和定位来实现。以下是两种常见方法: 方法一:使用clip-path属性 clip-path允许直接裁剪元素为六边形形…

css 制作

css 制作

CSS 制作基础 CSS(层叠样式表)用于控制网页的样式和布局。通过CSS,可以定义字体、颜色、间距、背景等视觉效果,使HTML内容更具吸引力。 内联样式 直接在HTML元素的style属性中编写C…