暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

我用HTML、CSS和JavaScript实现了简单的模拟时钟

前端新世界 2022-02-16
320
喜欢就关注我们吧


本文翻译自:
https://dev.to/code_mystery/simple-analog-clock-using-html-css-javascript-2c6a

在本文中,我将向大家展示如何使用HTML、CSS和JavaScript代码制作模拟时钟。我之前设计过很多类型的模拟时钟。这一款的形状采用黑色新拟物风格设计。用三个指针来表示小时、分钟和秒。此时钟盘面使用了符号而不是1到12的数字。

在我使用了新拟物风格设计制作之后,时钟的背景和页面的背景我也使用了相同的颜色。首先,我在网页上制作一个30rem
宽和30rem
高的盒子。border-radius: 50%
用于环绕此框。我在这里使用了box-shadow
来实现新拟物风格设计。

是不是感觉很简单?下面我将一步步展示如何制作模拟时钟。首先创建一个HTML和CSS文件。确保将你的CSS文件附加到html文件中。

第1步:设计和创建时钟的基本结构

我使用以下HTML和CSS代码来制作这个模拟时钟的背景。使用代码<div class = "clock"></div>
创建这个时钟的结构。我先在CSS代码中给出页面的背景颜色(background: #282828;
)。

<div class="clock">

</div>

html {
  background#282828;
  text-align: center;
  font-size10px;
}

body {
  margin0;
  font-size2rem;
  display: flex;
  flex1;
  min-height100vh;
  align-items: center;
}

.clock {
  width30rem;
  height30rem;
  position: relative;
  padding2rem;
  border7px 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-radius50%;
  margin50px auto;

}

第2步:在时钟上画两条彩色线条

我使用以下HTML和CSS代码来制作表示时间的符号。首先,使用以下代码制作两条线。并把这两条线放置为相互成90度角。再通过background: #282828
使线条更亮。

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

</div>

.outer-clock-face {
  position: relative;
  background#282828;
  overflow: hidden;
  width100%;
  height100%;
  border-radius100%;
}

.outer-clock-face::after {
  -webkit-transformrotate(90deg);
  -moz-transformrotate(90deg);
  transformrotate(90deg)
}

.outer-clock-face::after,
.outer-clock-face::before,
.outer-clock-face .marking{
  content'';
  position: absolute;
  width5px;
  height100%;
  background#1df52f;
  z-index0;
  left49%;
}

第3步:再画4条线

我使用以下HTML和CSS代码在这个时钟上又制作了4条线。通过CSS代码,我根据需要调整了这些线的角度。我用的是白色,你也可以用其他颜色。

<div class="marking marking-one"></div>
<div class="marking marking-two"></div>
<div class="marking marking-three"></div>
<div class="marking marking-four"></div>

.outer-clock-face .marking {
  background#bdbdcb;
  width3px;
}

.outer-clock-face .marking.marking-one {
  -webkit-transformrotate(30deg);
  -moz-transformrotate(30deg);
  transformrotate(30deg)
}

.outer-clock-face .marking.marking-two {
  -webkit-transformrotate(60deg);
  -moz-transformrotate(60deg);
  transformrotate(60deg)
}

.outer-clock-face .marking.marking-three {
  -webkit-transformrotate(120deg);
  -moz-transformrotate(120deg);
  transformrotate(120deg)
}

.outer-clock-face .marking.marking-four {
  -webkit-transformrotate(150deg);
  -moz-transformrotate(150deg);
  transformrotate(150deg)
}

第4步:在时钟中间画一个圆圈

我在这个钟的中间制作了一个圆圈。这个圆的圆心位于预先绘制的这些线条的交汇处。由此产生的线条作为符号。

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

</div>

.inner-clock-face {
  position: absolute;
  top10%;
  left10%;
  width80%;
  height80%;
  background#282828;
  -webkit-border-radius100%;
  -moz-border-radius100%;
  border-radius100%;
  z-index1;
}

.inner-clock-face::before {
  content'';
  position: absolute;
  top50%;
  left50%;
  width16px;
  height16px;
  border-radius18px;
  margin-left: -9px;
  margin-top: -6px;
  background#4d4b63;
  z-index11;
}

第5步:制作时钟的3个指针

和普通的模拟时钟一样,我用3个指针来表示小时、分钟和秒。我使用以下HTML和CSS代码创建和设计指针。

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

.hand {
  width50%;
  right50%;
  height6px;
  background#61afff;
  position: absolute;
  top50%;
  border-radius6px;
  transform-origin100%;
  transformrotate(90deg);
  transition-timing-functioncubic-bezier(0.12.70.581);
}

.hand.hour-hand {
  width30%;
  z-index3;
}

.hand.min-hand {
  height3px;
  z-index10;
  width40%;
}

.hand.second-hand {
  background#ee791a;
  width45%;
  height2px;
}

第6步:用JavaScript代码激活模拟时钟

到现在为止,我们已经完成了时钟的设计工作。现在我们将在JavaScript的帮助下实现这个模拟时钟。这里我没有使用任何插件或JavaScript库。我只是使用JavaScript让指针运动起来。即使你是初学者,也可以理解这样的设计。我在每段代码下面对为什么我要这样使用代码给出了完整的解释。

我们将使用querySelector()
获取以下内容的引用:

.second-hand ➤
.min-hand
.hour-hand`

const secondHand = document.querySelector('.second-hand');
const minsHand = document.querySelector('.min-hand');
const hourHand = document.querySelector('.hour-hand');

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

new Date()
创建了Date
类的实例,然后我们可以从中获取各种信息,例如当前日期、小时、分钟、秒等。

const seconds = now.getSeconds();
const secondsDegrees = ((seconds / 60) * 360) + 90;
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;

➤ 将秒针如何旋转存储在secondsDegrees
中。

➤ 然后使用rotate(${secondsDegrees}deg)
旋转秒针。

➤ 除以60,因为1分钟等于60秒。

➤ 乘以360,因为圆由360度构成。

const mins = now.getMinutes();
const minsDegrees = ((mins / 60) * 360) + ((seconds/60)*6) + 90;
minsHand.style.transform = `rotate(${minsDegrees}deg)`;

setInterval(setDate, 1000);

setDate();

➤ 我们需要每1秒(1000 milliseconds)调用一次rotate()
函数。

最终的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();

你学会如何制作这款模拟时钟了吗?自己亲自试一试吧!

希望对你的开发之旅有所帮助。下次再见。

每日分享前端插件干货,欢迎关注!

点赞和在看就是最大的支持❤️

文章转载自前端新世界,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论