使用 HTML、CSS 和 JavaScript 的简单模拟时钟

正如你在上面的演示中看到的,我在这个手表周围使用了一个边框来制作代码边框:7px solid #282828。我使用 box-shadow 使其更清晰。border-radius 50%使这款手表呈圆形。我还使用了高度和宽度 30 rem。如果你想让这款手表更大,你可以增加它的尺寸。


<div class="clock">

       

</div>




 html {

  background: #282828;

  text-align: center;

  font-size: 10px;

}



body {

  margin: 0;

  font-size: 2rem;

  display: flex;

  flex: 1;

  min-height: 100vh;

  align-items: center;

}



.clock {

  width: 30rem;

  height: 30rem;

  border: 7px solid #282828;

  box-shadow: -4px -4px 10px rgba(67,67,67,0.5),

                inset 4px 4px 10px rgba(0,0,0,0.5),

                inset -4px -4px 10px rgba(67,67,67,0.5),

                4px 4px 10px rgba(0,0,0,0.3);

  border-radius: 50%;

  margin: 50px auto;

  position: relative;

  padding: 2rem;

 

}



演示效果:

在这里插入图片描述

🥇 第 2 步:在时钟上标记 1 到 12



<div class="outer-clock-face">

	<div class="marking marking-one"></div>

	<div class="marking marking-two"></div>

	<div class="marking marking-three"></div>

	<div class="marking marking-four"></div>          

</div>




.outer-clock-face {

  position: relative;

  width: 100%;

  height: 100%;

  border-radius: 100%;

  background: #282828;

  

 

  overflow: hidden;

}



.outer-clock-face::after {

  -webkit-transform: rotate(90deg);

  -moz-transform: rotate(90deg);

  transform: rotate(90deg)

}



.outer-clock-face::before,

.outer-clock-face::after,

.outer-clock-face .marking{

  content: '';

  position: absolute;

  width: 5px;

  height: 100%;

  background: #1df52f;

  z-index: 0;

  left: 49%;

}



演示效果:

在这里插入图片描述


.outer-clock-face .marking {

  background: #bdbdcb;

  width: 3px;

}



.outer-clock-face .marking.marking-one {

  transform: rotate(30deg)

}



.outer-clock-face .marking.marking-two {

  transform: rotate(60deg)

}



.outer-clock-face .marking.marking-three {

  transform: rotate(120deg)

}



.outer-clock-face .marking.marking-four {

  transform: rotate(150deg)

}



演示效果:

在这里插入图片描述

我使用下面的 HTML 和 CSS 代码制作了一个圆圈。结果,长线的中间被覆盖,并且它具有完整的 1 到 12 个标记大小。

HTML:


<div class="inner-clock-face">

         

 </div>



CSS


.inner-clock-face {

  position: absolute;

  top: 10%;

  left: 10%;

  width: 80%;

  height: 80%;

  background: #282828;

  -webkit-border-radius: 100%;

  -moz-border-radius: 100%;

  border-radius: 100%;

  z-index: 1;

}



.inner-clock-face::before {

  content: '';

  position: absolute;

  top: 50%;

  border-radius: 18px;

  margin-left: -9px;

  margin-top: -6px;

  left: 50%;

  width: 16px;

  height: 16px;

  background: #4d4b63;

  z-index: 11;

}



演示效果:在这里插入图片描述

🎪 第 3 步:制作三只指针来指示时间


在这个单元格中,我使用了三只手,它们是使用下面的 HTML 和 CSS 代码制作的。

HTML:


<div class="hand hour-hand"></div>

<div class="hand min-hand"></div>

<div class="hand second-hand"></div>



CSS:


.hand {

  width: 50%;

  right: 50%;

  height: 6px;

  background: #61afff;

  position: absolute;

  top: 50%;

  border-radius: 6px;

  transform-origin: 100%;

  transform: rotate(90deg);

  transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);

}



.hand.hour-hand {

  width: 30%;

  z-index: 3;

}



.hand.min-hand {

  height: 3px;

  z-index: 10;

  width: 40%;

}



.hand.second-hand {

  background: #ee791a;

  width: 45%;

  height: 2px;

}



演示效果:

在这里插入图片描述

⏰ 第 4 步:使用 JavaScript 代码激活时钟


上面我们设计了整只手表,但这款手表还没有功能。这意味着这款手表的指针没有任何功能,也没有显示准​​确的时间。为此,我们需要使用 JavaScript 代码。

使用下面的 JavaScript,我已经给出了如何旋转这些手的说明。如果你了解基本的 JavaScript,你肯定会理解它。

我已经在下面充分解释了这段 JavaScript 代码是如何工作的。


const secondHand = document.querySelector('.second-hand');

const minsHand = document.querySelector('.min-hand');

const hourHand = document.querySelector('.hour-hand');




function setDate() {

  const now = new Date();



  const seconds = now.getSeconds();   // second hand rotation

  const secondsDegrees = ((seconds / 60) * 360) + 90;   

**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

**深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

**因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**

![img](https://img-blog.csdnimg.cn/img_convert/acc3313fa83f5291cd6b50d36adbd0a6.png)

 

![img](https://img-blog.csdnimg.cn/img_convert/75ee161f052f052c30ee5eea7a446c06.png)

![img](https://img-blog.csdnimg.cn/img_convert/46506ae54be168b93cf63939786134ca.png)

![img](https://img-blog.csdnimg.cn/img_convert/252731a671c1fb70aad5355a2c5eeff0.png)

![img](https://img-blog.csdnimg.cn/img_convert/6c361282296f86381401c05e862fe4e9.png)

![img](https://img-blog.csdnimg.cn/img_convert/9f49b566129f47b8a67243c1008edf79.png)

 

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

**如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注Python)**

/46506ae54be168b93cf63939786134ca.png)

![img](https://img-blog.csdnimg.cn/img_convert/252731a671c1fb70aad5355a2c5eeff0.png)

![img](https://img-blog.csdnimg.cn/img_convert/6c361282296f86381401c05e862fe4e9.png)

![img](https://img-blog.csdnimg.cn/img_convert/9f49b566129f47b8a67243c1008edf79.png)

 

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

**如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注Python)**

<img src="https://img-community.csdnimg.cn/images/fd6ebf0d450a4dbea7428752dc7ffd34.jpg" alt="img" style="zoom:50%;" />
  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是代码示例: ```html <!DOCTYPE html> <html> <head> <title>时钟特效</title> <style> body { background-color: #333; } .clock { width: 200px; height: 200px; border-radius: 50%; border: 8px solid #fff; position: relative; margin: 50px auto; box-shadow: inset 0 0 10px #000, 0 0 10px #000; } .hour-hand, .minute-hand, .second-hand { position: absolute; background-color: #fff; transform-origin: bottom center; transition: all 0.5s ease-in-out; } .hour-hand { width: 8px; height: 60px; top: 50px; left: 96px; } .minute-hand { width: 6px; height: 80px; top: 40px; left: 97px; } .second-hand { width: 4px; height: 100px; top: 30px; left: 98px; } .mark { position: absolute; background-color: #fff; border-radius: 50%; width: 10px; height: 10px; left: 95px; top: 20px; } .mark:nth-child(1) { transform: rotate(30deg); } .mark:nth-child(2) { transform: rotate(60deg); } .mark:nth-child(3) { transform: rotate(90deg); } .mark:nth-child(4) { transform: rotate(120deg); } .mark:nth-child(5) { transform: rotate(150deg); } .mark:nth-child(6) { transform: rotate(180deg); } .mark:nth-child(7) { transform: rotate(210deg); } .mark:nth-child(8) { transform: rotate(240deg); } .mark:nth-child(9) { transform: rotate(270deg); } .mark:nth-child(10) { transform: rotate(300deg); } .mark:nth-child(11) { transform: rotate(330deg); } </style> </head> <body> <div class="clock"> <div class="hour-hand"></div> <div class="minute-hand"></div> <div class="second-hand"></div> <div class="mark"></div> <div class="mark"></div> <div class="mark"></div> <div class="mark"></div> <div class="mark"></div> <div class="mark"></div> <div class="mark"></div> <div class="mark"></div> <div class="mark"></div> <div class="mark"></div> <div class="mark"></div> </div> <script> function clock() { const date = new Date(); const hour = date.getHours(); const minute = date.getMinutes(); const second = date.getSeconds(); const hourHand = document.querySelector('.hour-hand'); const minuteHand = document.querySelector('.minute-hand'); const secondHand = document.querySelector('.second-hand'); hourHand.style.transform = `rotate(${hour * 30 + minute / 2}deg)`; minuteHand.style.transform = `rotate(${minute * 6}deg)`; secondHand.style.transform = `rotate(${second * 6}deg)`; } setInterval(clock, 1000); </script> </body> </html> ``` 代码解释: 1. 使用 HTMLCSS 创建一个时钟外壳,其中 `.clock` 类表示时钟本身,`.hour-hand`、`.minute-hand` 和 `.second-hand` 类表示时针、分针和秒针,`.mark` 类表示刻度。 2. 使用 JavaScript 中的 `Date` 对象获取当前时间的小时、分和秒。 3. 使用 `querySelector` 方法选择时针、分针和秒针的元素,并使用 `transform` 属性将它们旋转到正确的位置。 4. 使用 `setInterval` 方法每秒钟调用一次 `clock` 函数,以更新时钟的指针位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值