&…">
当前位置:首页 > CSS

css时钟制作

2026-01-28 01:47:20CSS

CSS时钟制作方法

制作一个纯CSS时钟无需JavaScript,利用CSS动画和伪元素实现时间显示。以下是具体实现步骤:

HTML结构

<div class="clock">
  <div class="hour"></div>
  <div class="minute"></div>
  <div class="second"></div>
</div>

CSS样式

.clock {
  width: 300px;
  height: 300px;
  border-radius: 50%;
  background: #fff;
  border: 10px solid #333;
  position: relative;
  box-shadow: 0 0 20px rgba(0,0,0,0.2);
}

.hour, .minute, .second {
  position: absolute;
  left: 50%;
  bottom: 50%;
  transform-origin: 50% 100%;
}

.hour {
  width: 8px;
  height: 80px;
  background: #333;
  margin-left: -4px;
  animation: rotate 43200s linear infinite;
}

.minute {
  width: 4px;
  height: 120px;
  background: #666;
  margin-left: -2px;
  animation: rotate 3600s linear infinite;
}

.second {
  width: 2px;
  height: 140px;
  background: red;
  margin-left: -1px;
  animation: rotate 60s linear infinite;
}

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

刻度添加

.clock::before {
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  background: #333;
  border-radius: 50%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 10;
}

/* 添加12个刻度 */
.clock::after {
  content: '';
  position: absolute;
  width: 5px;
  height: 15px;
  background: #333;
  top: 10px;
  left: 50%;
  margin-left: -2.5px;
  transform-origin: 50% 140px;
}

/* 创建12个刻度 */
.clock::after {
  box-shadow: 
    0 60px 0 0 #333,
    0 120px 0 0 #333,
    0 180px 0 0 #333,
    0 240px 0 0 #333,
    0 300px 0 0 #333;
}

/* 添加小时数字(可选) */
.clock span {
  position: absolute;
  font-family: Arial;
  font-size: 24px;
  font-weight: bold;
  color: #333;
}

动态时钟实现

要使时钟显示真实时间,需要结合JavaScript:

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

  const hourDeg = (hour * 30) + (minute * 0.5);
  const minuteDeg = minute * 6;
  const secondDeg = second * 6;

  document.querySelector('.hour').style.transform = `rotate(${hourDeg}deg)`;
  document.querySelector('.minute').style.transform = `rotate(${minuteDeg}deg)`;
  document.querySelector('.second').style.transform = `rotate(${secondDeg}deg)`;
}

setInterval(updateClock, 1000);
updateClock();

响应式调整

添加媒体查询使时钟适应不同屏幕:

@media (max-width: 600px) {
  .clock {
    width: 200px;
    height: 200px;
  }
  .hour { height: 50px; }
  .minute { height: 80px; }
  .second { height: 90px; }
}

高级样式优化

添加阴影和渐变效果:

css时钟制作

.clock {
  background: radial-gradient(circle, #fff 0%, #eee 100%);
  box-shadow: 
    inset 0 0 15px rgba(0,0,0,0.1),
    0 0 30px rgba(0,0,0,0.2);
}

.hour, .minute {
  box-shadow: 0 0 5px rgba(0,0,0,0.3);
}

以上代码组合可创建一个美观且功能完整的CSS时钟,纯CSS版本适合展示动画原理,结合JavaScript的版本可实现真实时间显示。

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

相关文章

css制作表单

css制作表单

使用CSS制作表单的基本方法 创建一个美观且功能完善的表单需要结合HTML结构和CSS样式。以下是关键步骤和示例代码: HTML基础结构 <form class="my-form">…

css制作tab菜单

css制作tab菜单

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

用css制作下拉菜单

用css制作下拉菜单

使用纯CSS制作下拉菜单 HTML结构 基础的下拉菜单需要嵌套的HTML结构,通常使用<ul>和<li>标签实现层级关系: <div class="dropdown"&…

css字体制作

css字体制作

使用 @font-face 自定义字体 通过 @font-face 规则引入自定义字体文件(如 .woff, .woff2, .ttf)。需定义字体名称和文件路径: @font-face {…

css页脚制作

css页脚制作

CSS页脚制作方法 基础页脚结构 在HTML中创建页脚的基本结构,使用<footer>标签包裹内容: <footer> <div class="footer-con…

css怎么制作三角形

css怎么制作三角形

使用边框制作三角形 通过设置元素的 border 属性,利用边框交汇处的斜切特性生成三角形。将元素的宽度和高度设为 0,仅通过边框的宽度和颜色控制形状。 .triangle { width: 0…