…">
当前位置:首页 > 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制作菜单

水平菜单制作 使用display: inline-block或flexbox布局创建水平菜单。设置list-style: none移除默认列表样式,添加padding和margin调整间距。 <…

div css制作导航

div css制作导航

使用div和CSS制作导航栏 导航栏是网站的重要组成部分,用于帮助用户快速找到所需内容。以下是使用div和CSS制作导航栏的方法: HTML结构 创建一个基本的导航栏HTML结构,使用div和ul元…

dw制作css

dw制作css

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

css制作扑克牌

css制作扑克牌

使用CSS制作扑克牌 扑克牌的样式可以通过CSS的border-radius、box-shadow和transform等属性实现。以下是一个简单的扑克牌设计示例: <div class="po…

css制作网页

css制作网页

CSS 网页设计基础 CSS(层叠样式表)用于控制网页的视觉呈现,包括布局、颜色、字体等。以下为关键方法和示例: 基础结构 HTML 文件需链接 CSS: <link rel="styles…

css制作导航

css制作导航

CSS 制作导航的方法 基础水平导航栏 使用无序列表(<ul>)和浮动(float)或 Flexbox 布局实现水平导航: <nav> <ul class="ho…