…">
当前位置:首页 > 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; }
}

高级样式优化

添加阴影和渐变效果:

.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

CSS基础语法 CSS规则由选择器和声明块组成。选择器指向需要设置样式的HTML元素,声明块包含一个或多个用分号分隔的声明。每个声明由属性和值组成,用冒号分隔。 选择器 { 属性: 值;…

css div 制作导航菜单

css div 制作导航菜单

基本导航菜单结构 使用HTML创建一个简单的导航菜单结构,通常使用<ul>和li>列表实现。每个菜单项用<a>标签包裹以实现链接功能。 <div class="n…

css 制作三角形

css 制作三角形

使用 CSS 制作三角形的方法 边框法(Border Method) 通过设置元素的宽高为0,并利用边框的透明属性来创建三角形。这是最常见且兼容性最好的方法。 向上三角形 .triangl…

css制作六边形

css制作六边形

使用CSS创建六边形的方法 通过CSS的clip-path属性可以轻松实现六边形效果。以下是一个基础示例: .hexagon { width: 100px; height: 55px;…

表格制作css

表格制作css

基础表格样式 使用CSS可以轻松地为HTML表格添加样式。以下是一个基础表格的HTML和CSS示例: <table class="basic-table"> <thead>…

dw制作css

dw制作css

使用DW(Dreamweaver)制作CSS的步骤 在Adobe Dreamweaver中创建和编辑CSS文件可以通过可视化界面或直接编写代码完成。以下是具体操作方法: 新建CSS文件 打开Dre…