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

css时钟制作

2026-04-01 04:21:10CSS

使用CSS和HTML创建时钟

创建一个CSS时钟需要结合HTML、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样式

css时钟制作

.clock {
  width: 300px;
  height: 300px;
  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;
}

.hour-hand {
  width: 8px;
  height: 80px;
  top: 70px;
  left: 146px;
}

.minute-hand {
  width: 4px;
  height: 120px;
  top: 30px;
  left: 148px;
}

.second-hand {
  width: 2px;
  height: 140px;
  top: 10px;
  left: 149px;
  background: red;
}

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

JavaScript动态更新

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

  const hourDegrees = (hour * 30) + (minute * 0.5);
  const minuteDegrees = minute * 6;
  const secondDegrees = second * 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时钟制作

.clock::before {
  content: '';
  position: absolute;
  width: 280px;
  height: 280px;
  border-radius: 50%;
  top: 10px;
  left: 10px;
}

.clock::after {
  content: '';
  position: absolute;
  width: 260px;
  height: 260px;
  border-radius: 50%;
  top: 20px;
  left: 20px;
}

/* 添加刻度 */
.clock .mark {
  position: absolute;
  width: 4px;
  height: 15px;
  background: #333;
  top: 0;
  left: 148px;
  transform-origin: bottom center;
}

/* 添加数字 */
.clock .number {
  position: absolute;
  font-size: 24px;
  font-weight: bold;
  color: #333;
  transform-origin: center center;
}

使用CSS动画实现平滑移动

为了让指针移动更平滑,可以添加CSS过渡效果:

.hour-hand, .minute-hand, .second-hand {
  transition: transform 0.3s cubic-bezier(0.4, 2.3, 0.3, 1);
}

纯CSS时钟(无JavaScript)

使用CSS动画也可以创建时钟,但精度较低:

.hour-hand {
  animation: rotate-hour 43200s infinite linear;
}

.minute-hand {
  animation: rotate-minute 3600s infinite linear;
}

.second-hand {
  animation: rotate-second 60s infinite linear;
}

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

响应式设计

确保时钟在不同设备上都能正常显示:

@media (max-width: 600px) {
  .clock {
    width: 200px;
    height: 200px;
  }

  .hour-hand {
    height: 50px;
    top: 50px;
  }

  .minute-hand {
    height: 80px;
    top: 20px;
  }

  .second-hand {
    height: 90px;
    top: 10px;
  }
}

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

相关文章

css立体相册制作

css立体相册制作

制作CSS立体相册 使用CSS3的3D变换和过渡效果可以创建一个立体相册。以下是一个基本的实现方法。 HTML结构 <div class="photo-album"> <di…

css制作时钟

css制作时钟

CSS 制作时钟的方法 使用纯CSS和少量JavaScript可以创建一个动态的时钟。以下是实现步骤: HTML结构 <div class="clock"> <div cla…

jquery css

jquery css

jQuery 操作 CSS 的方法 jQuery 提供了多种方法来操作元素的 CSS 样式,包括获取、设置、添加或移除样式。以下是常用的方法: 获取 CSS 属性值 使用 .css() 方法获取元素…

css制作导航条

css制作导航条

导航条基础结构 使用HTML构建导航条的基本结构,通常包含一个<nav>标签和多个<a>标签作为导航链接。示例代码如下: <nav class="navbar">…

css制作图标

css制作图标

使用CSS制作图标的方法 使用伪元素和边框 通过CSS的伪元素和边框属性可以创建简单的几何形状图标。例如,制作一个三角形图标: .triangle { width: 0; heigh…

css精灵图制作

css精灵图制作

CSS精灵图制作方法 将多个小图标或背景图像合并到一张大图中,通过CSS的background-position属性定位显示特定部分,减少HTTP请求次数,提升页面加载性能。 准备素材 收集需要合并…