探索 HTML 和 CSS 实现的模拟时钟

效果演示

这段代码是一个模拟时钟的 HTML 和 CSS 代码。它创建了一个简单的数字时钟界面,包括时针、分针和秒针。

HTML

html 复制代码
<div class="face">
    <p class="v-index">II
    </p>
    <p class="h-index">II
    </p>
    <div class="hand">
        <div class="hand">
            <div class="hour"></div>
            <div class="minute"></div>
            <div class="second"></div>
        </div>
    </div>
</div>
  • face:这是时钟的主体部分,包含了时钟的所有元素。
  • v-index和h-index:这些是时钟的刻度,分别表示垂直(V)和水平(H)方向的刻度。这里的"II"可能是罗马数字2,但通常时钟的刻度会用数字或点来表示,这里可能是一个示例。
  • hand:这个容器包含了时钟的指针。
  • hour:时针。
  • minute:分针。
  • second:秒针。

CSS

css 复制代码
.face {
  position: relative;
  width: 180px;
  height: 180px;
  border-radius: 50%;
  outline: 10px solid #333;
  background: repeating-radial-gradient(circle at 50% 50%,
  rgba(200,200,200,.2) 0%, rgba(200,200,200,.2) 2%,
  transparent 2%, transparent 3%, rgba(200,200,200,.2) 3%,
  transparent 3%), conic-gradient(white 0%, silver 10%,
  white 35%, silver 45%, white 60%, silver 70%,
  white 80%, silver 95%, white 100%);
  box-shadow: inset 0 0 20px #0007;
}

.hour {
  position: absolute;
  width: 5px;
  height: 60px;
  background: #aaa;
  left: 87.5px;
  top: 43px;
  border-radius: 3px 3px 1px 1px;
  transform-origin: 2px 47px;
  box-shadow: 0 0 5px #0005,inset 1.5px 3px 0px #333, inset -1.5px -3px 0px #333;
  z-index: 1;
  animation: watch 43200s linear infinite;
}

.minute {
  position: absolute;
  width: 4px;
  height: 78px;
  background: #aaa;
  left: 88px;
  top: 25px;
  border-radius: 3px 3px 1px 1px;
  transform-origin: 2px 65px;
  box-shadow: 0 0 5px #0005, inset 1.5px 3px 0px #333, inset -1.5px -3px 0px #333;
  z-index: 2;
  animation: watch 3600s linear infinite;
}

.second {
  position: absolute;
  width: 10px;
  height: 10px;
  background: red;
  left: 85px;
  top: 85px;
  border-radius: 50%;
  border: 1px solid #eee;
  z-index: 3;
  animation: watch 60s steps(60, end) 0s infinite;
}

.second::before {
  content: "";
  position: absolute;
  width: 1px;
  height: 85px;
  left: 3px;
  bottom: -10px;
  background: red;
  border-radius: 2px;
  box-shadow: 5px 0 2px rgba(128, 128, 128, 0.2);
}

.second::after {
  content: "";
  position: absolute;
  width: 4px;
  height: 4px;
  left: 2px;
  top: 2px;
  background: #555;
  border-radius: 50%;
}

.v-index {
  position: absolute;
  color: #333;
  font-size: 24px;
  left: 83.5px;
  top: -3px;
  text-shadow: 0 157px 0 #333;
  z-index: 1
}

.h-index {
  position: absolute;
  color: #333;
  font-size: 24px;
  top: 72px;
  left: 5px;
  transform: rotate(-90deg);
  text-shadow: 0 158px 0 #333;
  z-index: 1;
}

@keyframes watch {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}
  • .face:定义时钟的主体样式。它是一个相对定位的元素,宽高都是180px,并且有50%的圆角,使其看起来像一个圆形。边框是10px的实线,颜色为深灰色。背景是一个复杂的渐变效果,包括重复的径向渐变和圆锥渐变,以及一个内阴影效果。
  • .hour、.minute 和 .second:这些类定义了时钟指针的基本样式。它们都是绝对定位的元素,有不同的宽度、高度和位置。它们都有圆角和阴影效果,以及一个transform-origin属性,这决定了旋转的中心点。.hour和.minute的动画是watch,分别持续43200秒(12小时)和3600秒(1小时),模拟时针和分针的运动。
  • .second:定义了秒针的样式,它是一个红色的圆形,有一个小的黑色圆点在顶部,模拟秒针的尖端。它的动画是watch,持续60秒,模拟秒针的运动。
  • @keyframes watch:定义了一个关键帧动画watch,它使元素从0度旋转到360度,形成一个完整的圆周运动。
相关推荐
恋猫de小郭11 分钟前
移动端开发稳了?AI 目前还无法取代客户端开发,小红书的论文告诉你数据
前端·flutter·ai编程
文心快码BaiduComate29 分钟前
百度云与光本位签署战略合作:用AI Agent 重构芯片研发流程
前端·人工智能·架构
闲云一鹤1 小时前
nginx 快速入门教程 - 写给前端的你
前端·nginx·前端工程化
QCY1 小时前
「完全理解」1 分钟实现自己的 Coding Agent
前端·agent·claude
一拳不是超人2 小时前
Electron主窗口弹框被WebContentView遮挡?独立WebContentView弹框方案详解!
前端·javascript·electron
anyup2 小时前
🔥2026最推荐的跨平台方案:H5/小程序/App/鸿蒙,一套代码搞定
前端·uni-app·harmonyos
雮尘2 小时前
如何在非 Claude IDE (TARE、 Cursor、Antigravity 等)下使用 Agent Skills
前端·agent·ai编程
icebreaker2 小时前
Weapp-vite:原生模式之外,多一种 Vue SFC 选择
前端·vue.js·微信小程序
icebreaker3 小时前
重走 Vue 长征路 Weapp-vite:编译链路与 Wevu 运行时原理拆解
前端·vue.js·微信小程序
wuhen_n3 小时前
代码生成:从AST到render函数
前端·javascript·vue.js