前言
今天我们来聊聊如何使用JS来创建一个实时的小闹钟,这个小闹钟十分的有趣,小伙伴们可以运行一下,看看跟你电脑上的时间是否对的上呢?
我们先来看一下实现的效果
结构设计
我们先来观察一下这个小闹钟
我们先将它整体命名为clock,这个闹钟可以分为两个部分,一个外表盘,一个内表盘,我们将它们命名为outer-clock-face和inner-clock-face。
接下来我们先来观察外表盘,发现有刻度,我们用marking marking-one、marking marking-two、marking marking-three、marking marking-four四个容器将它们表示出来。
接下来我们来看内表盘,发现里面有时针、秒针、分针这三根针,我们用<div class="hand hour-hand">,<div class="hand min-hand">,和<div class="hand second-hand">,来代表它们,最后使用JS使它们呈现动态的效果。
接下来我们来看html部分的代码
html
复制代码
<html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="./style.css"> </head> <body> <div class="clock"> <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 class="inner-clock-face"> <div class="hand hour-hand"></div> <div class="hand min-hand"></div> <div class="hand second-hand"></div> </div> </div> </div> <script src="./index.js"></script> </body> </html>
CSS
首先我们附上代码
html { background: #CCCCFF; font-size: 10px; } body { height: 100vh; margin: 0; font-size: 2rem; display: flex; justify-content: center; align-items: center; } .clock { width: 30rem; height: 30rem; border: 7px solid #ffebdb; border-radius: 50%; box-shadow: -4px -4px 10px rgba(67, 67, 67, 0.1), inset 4px 4px 10px rgba(168, 145, 128, 0.6), inset -4px -4px 10px rgba(201, 175, 155, 0.2), 4px 4px 10px rgba(168, 145, 128, 0.6); background-image: url("https://p26-passport.byteacctimg.com/img/user-avatar/6836faf9f43e8c77ef339218ab8b601b~130x130.awebp"); background-size: 108%; padding: 2rem; } .outer-clock-face { width: 100%; height: 100%; border-radius: 50%; position: relative; display: flex; justify-content: center; align-items: center; } .outer-clock-face::before, .outer-clock-face::after { content: ''; width: 10px; height: 100%; background: #596230; position: absolute; border-radius: 8px; } .outer-clock-face::after { transform: rotate(90deg); } .marking { width: 3px; height: 100%; background: #596230; position: absolute; } /*通过旋转实现背景图上六根分隔线将时钟分隔的效果*/ .marking-one { transform: rotateZ(30deg); } .marking-two { transform: rotateZ(60deg); } .marking-three { transform: rotateZ(120deg); } .marking-four { transform: rotateZ(150deg); } .inner-clock-face { position: absolute; top: 10%; left: 10%; width: 80%; height: 80%; background-color: #fffebd; z-index: 2; border-radius: 50%; /*导入外部图片,也就是时钟的背景图*/ background-image: url("https://p26-passport.byteacctimg.com/img/user-avatar/6836faf9f43e8c77ef339218ab8b601b~130x130.awebp"); background-size: 108%; } .inner-clock-face::after { content: ''; position: absolute; width: 16px; height: 16px; border-radius: 50%; background: #4d4b63; top: 50%; left: 50%; transform: translate(-50%, -50%); } .hand { width: 50%; height: 6px; background: red; border-radius: 6px; position: absolute; top: 50%; right: 50%; margin-top: -3px; /*transform-origin修改旋转中心*/ transform-origin: 100% 50%; transform: rotate(90deg); } .hour-hand { width: 30%; } .min-hand { width: 40%; height: 3px; } .second-hand { background: #b3b3b3; width: 45%; height: 2px; }
- 全局样式(html和body) :
html选择器用于设置整个HTML文档的全局样式。background属性设置了背景颜色为浅紫色。font-size属性设置了字体大小为10px。
- Body样式:
body选择器用于设置整个网页的主体样式。height属性设置页面高度为视口高度(100vh)。margin属性设置边距为0,以确保没有默认边距。font-size属性设置字体大小为2rem,相对于根元素的字体大小。display: flex和justify-content以及align-items属性用于将页面内容垂直和水平居中显示。
- 时钟容器样式 (.clock) :
width和height属性设置了时钟的宽度和高度,使其呈现为一个圆形。border属性定义了时钟的边框样式。border-radius属性设置边框为圆形。box-shadow属性添加了阴影效果。background-image属性设置了时钟的背景图像。background-size属性控制了背景图像的大小。padding属性为时钟周围添加了内边距。
- 外部表盘样式 (.outer-clock-face) :
width、height和border-radius属性定义了外部表盘的样式。::before和::after伪元素用于创建两个垂直的短条形元素。transform属性用于旋转::after伪元素,实现垂直排列。
- 刻度样式 (.marking, .marking-one, .marking-two, .marking-three, .marking-four) :
- 这些类定义了表盘上的标记或刻度的样式。
- 通过
transform属性的旋转来将标记放置在不同的位置,实现分隔线的效果。
- 内部表盘样式 (.inner-clock-face) :
width、height、border-radius属性设置内部表盘的样式。background-image属性设置内部表盘的背景图像。
- 内部表盘中心点样式 (.inner-clock-face::after) :
- 伪元素
::after用于创建时钟内部的中心点。 - 它通过
width、height、border-radius等属性定义了一个小圆形。
- 指针样式 (.hand, .hour-hand, .min-hand, .second-hand) :
- 这些类定义了不同类型的时钟指针的样式。
- 它们设置了指针的宽度、高度、颜色、圆角、位置以及旋转中心。
JS
const secondHand = document.querySelector('.second-hand') const minHand = document.querySelector('.min-hand') const hourHand = document.querySelector('.hour-hand') // console.log(secondHand); function setTime() { const now = new Date() // 获取当前的秒数 const seconds = now.getSeconds() // 30 const secondsDegrees = seconds * 6 + 90 secondHand.style.transform = `rotate(${secondsDegrees}deg)` // 获取到分数 const mins = now.getMinutes() // 40 const minsDegrees = mins * 6 + 90 minHand.style.transform = `rotate(${minsDegrees}deg)` // 获取到时 const hour = now.getHours() // 21 const hourDegrees = hour * 30 + 90 + (mins / 60) * 30 hourHand.style.transform = `rotate(${hourDegrees}deg)` } setTime() setInterval(setTime, 1000)
const secondHand = document.querySelector('.second-hand')、const minHand = document.querySelector('.min-hand')和const hourHand = document.querySelector('.hour-hand'):这些代码行通过document.querySelector方法获取了页面中具有特定类名的HTML元素,分别表示秒针、分针和时针的指针元素。这些元素将用于通过改变其style.transform属性来旋转它们以模拟时钟的运行。function setTime():这是一个用于更新时钟指针位置的JavaScript函数。函数内部执行以下操作:
- 获取当前的日期和时间:通过
new Date()创建一个Date对象,然后使用getSeconds()、getMinutes()和getHours()方法分别获取当前的秒、分和时。 - 计算秒针的旋转角度:将秒数乘以6并加上90度,这是因为12点位置对应90度,而每秒钟对应6度。然后将计算得到的角度赋值给
secondHand的style.transform属性,通过rotate(deg)来旋转秒针。 - 计算分针的旋转角度:类似地,将分钟数乘以6并加上90度,然后将计算得到的角度赋值给
minHand的style.transform属性。 - 计算时针的旋转角度:将小时数乘以30并加上90度,同时考虑分钟数对时针的微调,然后将计算得到的角度赋值给
hourHand的style.transform属性。
setTime():首次调用setTime函数来初始化时钟的指针位置。setInterval(setTime, 1000):使用setInterval函数,每隔1秒(1000毫秒)调用一次setTime函数,以不断更新时钟指针的位置,从而实现模拟时钟的运行效果。
这段代码的目的是通过JavaScript实现了一个简单的时钟模拟,每秒钟更新一次时、分和秒针的位置,以显示当前的时间。通过获取当前时间并计算相应的旋转角度,它实现了时钟指针的动态运行效果。
小伙伴们赶快去试试吧~
今天的内容就到这啦,如果你觉得小编写的还不错的话,或者对你有所启发,请给小编一个辛苦的赞吧