当前位置:首页 > CSS

css手工制作钟

2026-04-02 12:26:08CSS

使用CSS制作钟表

通过CSS和少量HTML可以创建一个模拟钟表,利用动画效果实现指针的旋转。以下是实现步骤:

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样式

css手工制作钟

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

.hour-hand {
  width: 6px;
  height: 50px;
  background: #333;
  position: absolute;
  top: 50px;
  left: 97px;
  transform-origin: bottom center;
  animation: rotate-hour 43200s linear infinite;
}

.minute-hand {
  width: 4px;
  height: 70px;
  background: #666;
  position: absolute;
  top: 30px;
  left: 98px;
  transform-origin: bottom center;
  animation: rotate-minute 3600s linear infinite;
}

.second-hand {
  width: 2px;
  height: 80px;
  background: #f00;
  position: absolute;
  top: 20px;
  left: 99px;
  transform-origin: bottom center;
  animation: rotate-second 60s linear infinite;
}

.center-dot {
  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::before {
  content: "";
  position: absolute;
  width: 8px;
  height: 15px;
  background: #333;
  top: 10px;
  left: 96px;
}

.clock::after {
  content: "";
  position: absolute;
  width: 8px;
  height: 15px;
  background: #333;
  bottom: 10px;
  left: 96px;
}

实现实时时间同步

通过JavaScript使钟表显示当前真实时间:

css手工制作钟

function updateClock() {
  const now = new Date();
  const hour = now.getHours() % 12;
  const minute = now.getMinutes();
  const second = now.getSeconds();

  document.querySelector('.hour-hand').style.transform = 
    `rotate(${(hour * 30) + (minute * 0.5)}deg)`;
  document.querySelector('.minute-hand').style.transform = 
    `rotate(${minute * 6}deg)`;
  document.querySelector('.second-hand').style.transform = 
    `rotate(${second * 6}deg)`;
}

setInterval(updateClock, 1000);
updateClock();

优化指针动画

使用steps()函数使秒针跳动式移动:

.second-hand {
  animation: rotate-second 60s steps(60) infinite;
}

添加数字刻度

在HTML中添加数字元素:

<div class="number number1">1</div>
<div class="number number2">2</div>
<!-- 其他数字... -->

对应的CSS定位:

.number {
  position: absolute;
  width: 20px;
  height: 20px;
  text-align: center;
  font-weight: bold;
}

.number1 { top: 20px; right: 40px; }
.number2 { top: 50px; right: 20px; }
<!-- 其他数字定位... -->

以上代码组合后可创建一个功能完整的CSS钟表,既可以是纯装饰性动画,也可以同步显示真实时间。

标签: 手工制作css
分享给朋友:

相关文章

css网页制作

css网页制作

CSS网页制作基础 CSS(层叠样式表)用于控制网页的视觉表现,包括布局、颜色、字体等。以下是核心知识点: 选择器与样式规则 /* 标签选择器 */ p { color: #333; fo…

css制作tab菜单

css制作tab菜单

CSS制作Tab菜单的方法 HTML结构 使用无序列表和div容器构建基础结构,列表项为标签页标题,div内放置对应内容块。 <ul class="tab-menu"> <li…

css3手工制作图片

css3手工制作图片

使用CSS3手工制作图片 CSS3可以通过各种属性如box-shadow、border-radius、gradient等来手工绘制简单的图形或图片效果。以下是几种常见的方法: 绘制圆形 通过bord…

css 制作箭头

css 制作箭头

使用 CSS 边框制作箭头 通过调整元素的边框宽度和颜色,利用透明边框的特性可以创建箭头效果。例如,制作一个向右的箭头: .arrow-right { width: 0; height: 0…

制作css开关

制作css开关

使用纯CSS创建开关 HTML结构需要包含一个隐藏的复选框和一个关联的标签元素: <label class="switch"> <input type="checkbox"&g…

css奖状制作

css奖状制作

使用CSS制作奖状 奖状制作可以通过CSS结合HTML实现,适用于网页展示或打印。以下是几种常见的设计方法: 基础奖状结构 HTML框架用于定义奖状内容,CSS负责样式设计。 <div cl…