当前位置:首页 > CSS

css3制作时钟样式

2026-01-27 22:32:50CSS

使用CSS3制作时钟样式

制作一个CSS3时钟样式可以通过伪元素、动画和变换来实现。以下是一个完整的实现方法:

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

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

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

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

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

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

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

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

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

添加时钟刻度

为时钟添加刻度可以增强真实感:

.clock::before {
  content: '';
  position: absolute;
  width: 8px;
  height: 8px;
  background: #333;
  border-radius: 50%;
  top: 10px;
  left: 96px;
}

.clock::after {
  content: '';
  position: absolute;
  width: 8px;
  height: 8px;
  background: #333;
  border-radius: 50%;
  bottom: 10px;
  left: 96px;
}

.clock span {
  position: absolute;
  width: 8px;
  height: 8px;
  background: #333;
  border-radius: 50%;
}

.clock span:nth-child(1) {
  top: 96px;
  right: 10px;
}

.clock span:nth-child(2) {
  top: 96px;
  left: 10px;
}

使用JavaScript实现实时时间

为让时钟显示真实时间,可以添加JavaScript:

function updateClock() {
  const now = new Date();
  const hours = now.getHours() % 12;
  const minutes = now.getMinutes();
  const seconds = now.getSeconds();

  const hourDegrees = (hours * 30) + (minutes * 0.5);
  const minuteDegrees = minutes * 6;
  const secondDegrees = seconds * 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();

优化时钟样式

添加更多样式细节提升视觉效果:

.clock {
  /* 原有样式 */
  box-shadow: 
    inset 0 0 15px rgba(0,0,0,0.1),
    0 5px 15px rgba(0,0,0,0.3);
}

.hour-hand {
  /* 原有样式 */
  box-shadow: 0 0 5px rgba(0,0,0,0.3);
}

.minute-hand {
  /* 原有样式 */
  box-shadow: 0 0 5px rgba(0,0,0,0.3);
}

.second-hand {
  /* 原有样式 */
  box-shadow: 0 0 5px rgba(0,0,0,0.3);
}

通过以上方法可以创建一个功能完整、视觉效果良好的CSS3时钟。可以根据需要调整尺寸、颜色和动画效果来满足不同设计需求。

css3制作时钟样式

标签: 时钟样式
分享给朋友:

相关文章

vue实现收藏样式

vue实现收藏样式

vue实现收藏样式 使用图标库实现收藏样式 安装图标库如font-awesome或element-ui的图标组件,通过v-bind动态绑定类名或样式实现收藏与未收藏状态切换。 <templat…

css怎么制作时钟

css怎么制作时钟

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

vue实现适老化样式

vue实现适老化样式

Vue 实现适老化样式的关键方法 使用大字体和高对比度 在全局 CSS 或组件样式中设置基础字号为 16px 以上,推荐 18-20px。通过 CSS 变量控制字体大小,便于全局调整: :root…

vue实现手机预览样式

vue实现手机预览样式

实现手机预览样式的方法 在Vue项目中实现手机预览样式,可以通过以下几种方式实现: 使用响应式布局 通过CSS媒体查询设置不同屏幕尺寸的样式,确保页面在手机端正常显示: @media (max-w…

uniapp布局样式

uniapp布局样式

uniapp布局样式基础 uniapp基于Vue.js框架,支持多种布局方式,包括Flex布局、Grid布局和传统盒模型布局。样式编写遵循CSS规范,同时支持rpx响应式单位。 Flex布局示例…

vue 实现样式切换

vue 实现样式切换

实现 Vue 样式切换的方法 在 Vue 中实现样式切换可以通过多种方式完成,以下是几种常见的实现方法: 使用动态类名绑定 通过 v-bind:class 或简写 :class 动态绑定类名,根据条…