当前位置:首页 > CSS

css怎么制作钟

2026-04-01 15:31:24CSS

使用CSS制作钟表

通过CSS和少量JavaScript可以创建一个动态钟表。以下是实现步骤:

HTML结构

创建钟表的基本HTML结构,包含时、分、秒指针和钟面刻度:

<div class="clock">
  <div class="clock-face">
    <div class="hand hour-hand"></div>
    <div class="hand min-hand"></div>
    <div class="hand sec-hand"></div>
  </div>
</div>

CSS样式

设置钟表的外观和指针动画效果:

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

.clock-face {
  position: relative;
  width: 100%;
  height: 100%;
}

.hand {
  width: 50%;
  height: 6px;
  background: #333;
  position: absolute;
  top: 50%;
  transform-origin: 100%;
  transform: rotate(90deg);
  transition: all 0.05s;
  transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
}

.hour-hand {
  width: 30%;
  left: 20%;
}

.min-hand {
  width: 40%;
  left: 10%;
}

.sec-hand {
  height: 2px;
  background: red;
}

JavaScript功能

添加动态更新指针位置的代码:

function setDate() {
  const now = new Date();

  const seconds = now.getSeconds();
  const secondsDegrees = ((seconds / 60) * 360) + 90;
  document.querySelector('.sec-hand').style.transform = `rotate(${secondsDegrees}deg)`;

  const mins = now.getMinutes();
  const minsDegrees = ((mins / 60) * 360) + ((seconds/60)*6) + 90;
  document.querySelector('.min-hand').style.transform = `rotate(${minsDegrees}deg)`;

  const hour = now.getHours();
  const hourDegrees = ((hour / 12) * 360) + ((mins/60)*30) + 90;
  document.querySelector('.hour-hand').style.transform = `rotate(${hourDegrees}deg)`;
}

setInterval(setDate, 1000);
setDate();

添加钟表刻度

为钟表添加12小时刻度:

css怎么制作钟

.clock-face::after {
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  background: #333;
  border-radius: 50%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 10;
}

.marking {
  position: absolute;
  width: 3px;
  height: 15px;
  background: #333;
  left: 50%;
  top: 0;
  transform-origin: bottom center;
}

/* 添加12个刻度 */
.marking:nth-child(1) { transform: rotate(30deg) translateY(-140px); }
.marking:nth-child(2) { transform: rotate(60deg) translateY(-140px); }
/* 继续添加其他刻度... */

完整实现要点

  • 使用transform-origin属性设置指针旋转中心点
  • 通过transition实现指针平滑移动效果
  • JavaScript计算当前时间对应的旋转角度
  • 每隔1秒更新一次指针位置
  • 使用cubic-bezier函数创建弹性过渡效果

这种方法创建的钟表会实时显示当前时间,且具有平滑的动画效果。可以根据需要调整大小、颜色和样式来满足不同设计需求。

标签: css
分享给朋友:

相关文章

制作css

制作css

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

css导航条制作

css导航条制作

基础导航条制作 使用HTML的无序列表<ul>和CSS的display: flex属性可以快速创建横向导航条。HTML结构通常包含导航链接列表,CSS负责样式和布局。 <nav&g…

css 制作字体

css 制作字体

使用CSS自定义字体 在CSS中,可以通过@font-face规则引入自定义字体,并使用font-family属性应用这些字体。 @font-face { font-family: 'MyCus…

css与html制作

css与html制作

CSS与HTML制作基础 CSS与HTML是构建网页的核心技术,HTML负责结构,CSS负责样式。以下是关键要点: HTML基础结构 <!DOCTYPE html> <html&…

css制作滑动页面

css制作滑动页面

使用CSS实现滑动页面 通过CSS可以实现多种滑动页面效果,包括横向滑动、纵向滑动以及视差滚动等。以下是几种常见的实现方法: 横向滑动效果 横向滑动通常用于展示一组卡片或图片,可以通过CSS的ove…

css尾部制作

css尾部制作

如何制作CSS尾部(页脚) 固定定位法 将页脚固定在页面底部,无论内容多少。使用position: fixed结合bottom: 0实现: footer { position: fixed;…