趣味前端之使用 HTML、CSS 和 JS 制作炫酷黑色时钟
面试官可能经常会问到:你都做过哪些项目啊,说说都用了哪些技术?可能对于很多人来说创建一个完整且功能丰富的 Web 项目比较困难,尤其是对于初学者而言。但不用担心,我们可以从简单的项目开始,基础打牢慢慢去尝试构建更复杂的项目。
本篇文章我将带大家使用 JavaScript 制作一个模拟时钟。我们生活中常见的时钟有两种,一种是模拟的,一种是数字的。虽然数字时钟被广泛使用,但模拟时钟也被很多人所喜爱。
正如你在上图中所看到的,这里我借助 HTML、CSS 和 JavaScript 制作了一个简单的模拟时钟。
众所周知,模拟时钟有三个指针和从 1 到 12 的数字。在这里,我也在每个地方使用了符号,不过并没有使用从 1 到 12 的数字。
🎡 JavaScript 模拟时钟 [现场演示]
如果你想了解这个模拟时钟是如何工作的,那么你可以观看下面的演示。在这里,我提供了所需的源代码,以便你可以复制代码并在你自己的学习或者工作中使用它。
演示:https://haiyong.site/demo/clock2.html
代码:https://github.com/wanghao221/moyu
🎯 使用 HTML、CSS 和 JavaScript 的简单模拟时钟
希望大家喜欢这个设计。我在下面分享了有关我如何进行此设计的完整教程。希望下面的教程能帮到你。
为此,首先你必须创建一个 HTML 和 CSS 文件。
🎬 第 1 步:创建时钟的基本结构
这段 HTML 代码基本上就是这个模拟时钟的基本结构。我使用了一些 CSS 代码来设计这款时钟的背景和形状。正如大家在上图中所看到的,它采用了新形态设计的形式。在这里,我们使用 CSS 代码来实现 Neumorphism 设计。
正如大家在上面的演示中看到的,我在这个时钟周围使用了一个边框来制作代码边框: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 步:制作三只指针来指示时间
在这个单元格中,我使用了三个 hand
,它们是使用下面的 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 库。即使你是初学者,也能理解这种设计。在每个代码下方,我都完整地解释了我为什么使用该代码。
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(); // 秒针旋转 const secondsDegrees = ((seconds / 60) * 360) + 90; secondHand.style.transform = `rotate(${secondsDegrees}deg)`; const mins = now.getMinutes(); // 分针旋转 const minsDegrees = ((mins / 60) * 360) + ((seconds/60)*6) + 90; minsHand.style.transform = `rotate(${minsDegrees}deg)`; const hour = now.getHours(); // 时针旋转 const hourDegrees = ((hour / 12) * 360) + ((mins/60)*30) + 90; hourHand.style.transform = `rotate(${hourDegrees}deg)`; }
🔮 JavaScript 代码详解
🖍 关于秒针
const seconds = now.getSeconds(); // 秒针旋转 const secondsDegrees = ((seconds / 60) * 360) + 90; secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
🌟 我们将秒针如何旋转存储在secondsDegrees
中
🌟 然后使用rotate (${secondsDegrees} deg)
来旋转秒针
🌟 1 分钟等于 60 秒所以除以 60
🌟 圆的一周是360 度,所以乘以 360
🖌 关于分针
const mins = now.getMinutes(); // 分针旋转 const minsDegrees = ((mins / 60) * 360) + ((seconds/60)*6) + 90; minsHand.style.transform = `rotate(${minsDegrees}deg)`;
🌟 我在minsDegrees
中存储了如何转动分钟的指针
🌟 然后使用(${minsDegrees]deg)
来旋转分针
🌟 1 小时等于 60 分钟所以除以 60
🌟 添加了带分钟的秒针位置,因为分针在正确的位置取决于秒
const hour = now.getHours(); const hourDegrees = ((hour / 12) * 360) + ((mins/60)*30) + 90; hourHand.style.transform = `rotate(${hourDegrees}deg)`; }
setInterval(setDate, 1000); setDate();
➤ 我们需要rotate()
每 1 秒调用一次此函数 (1000 milliseconds)。
最终的 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(); const secondsDegrees = ((seconds / 60) * 360) + 90; secondHand.style.transform = `rotate(${secondsDegrees}deg)`; const mins = now.getMinutes(); const minsDegrees = ((mins / 60) * 360) + ((seconds/60)*6) + 90; minsHand.style.transform = `rotate(${minsDegrees}deg)`; const hour = now.getHours(); const hourDegrees = ((hour / 12) * 360) + ((mins/60)*30) + 90; hourHand.style.transform = `rotate(${hourDegrees}deg)`; } setInterval(setDate, 1000); setDate();
完整代码已上传至GitHub:https://github.com/wanghao221/moyu
写在最后
希望大家从本篇教程中学会了我是怎样制作这个模拟时钟的。如果大家还有任何问题,可以通过评论问我。当然,如果我做错了什么你也可以告诉我。
都看到这里了,不妨给个点赞和收藏吧,关注我带你体验 Web 编程的乐趣👉 海拥