模拟时钟的指针不能正常工作(the hands of a clock)

代码的 java 脚本部分有问题时针工作不正常。每 60 秒后旋转一次。还有,分针显示错误的时间。请检查 js 中编写的公式。我用过的公式也是 gfg 写的。请解释必要的变化。谢谢。

代码的 java 脚本部分有问题时针工作不正常。每 60 秒后旋转一次。还有,分针显示错误的时间。请检查 js 中编写的公式。我用过的公式也是 gfg 写的。请解释必要的变化。谢谢。

const secHand=document.querySelector('.second-hand');
const minhand=document.querySelector('.minute-hand');
const hrhand=document.querySelector('.hour-hand');
function setDate(){
const d=new Date();
const hr=d.getHours();
const m=d.getMinutes();
const sethrdeg= 30*hr + m/2;
hrhand.style.transform=`rotate(${sethrdeg}deg)`;
const seeg=6*m;
minhand.style.transform=`rotate(${seeg}deg)`;
const s=d.getSeconds();
const setsdeg=6*s;
secHand.style.transform=`rotate(${setsdeg}deg)`;
}
setInterval(setDate,1000);
*{
    background:url(https://images.unsplash.com/photo-1523821741446-edb2b68bb7a0?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80);
    margin: 0;
    padding: 0;
    font-family: Georgia, 'Times New Roman', Times, serif;
    background-size: cover;
}
body{
    display: flex;
    height: 100vh;
    align-items: center;
}
.clock{
    border : 3px solid black;
    border-radius: 50%;
    padding: 5px;
    position: relative;    
    left:30rem;
    width: 25rem;
    height: 25rem;
    justify-content: center;
    box-shadow:
    0 0 0 4px rgba(0,0,0,0.1),
    inset 0 0 0 3px #EFEFEF,
    inset 0 0 10px black,
    0 0 10px rgba(0,0,0,0.2);
}
.clock-face{
    position :relative;
    transform: translateY(-3px);
}
.hand{
    background: white;
    width: 48%;
    height: 1.5%;
    position: absolute;
    top :50%;
    transform-origin: 100%;
    transform: rotate(90deg);
    transition: all 0.06s;
    transition-timing-function: cubic-bezier(0.1, 1.09, 0.52, 1.26);
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta cht="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Clock</title>
    <link rel="stylesheet" href="clock.css">
</head>
<body>
    <div cl="clock">
        <div cl="clock-face"></div>
        <div cl="hand hour-hand"></div>
        <div cl="hand minute-hand"></div>
        <div cl="hand second-hand"></div>
    </div>
    <script src="clock.js"></script>
    
</body>
</html>
0

关于错误定位的手的问题是由于显示 00:00:00 所需的 90 度旋转。因此,在计算中,您始终必须添加此 90 度。

关于计算本身:
时钟有 360 度 12 小时(360 / 12)和 60 分钟 / 秒(360 / 60)。

要使时针在时针之间不断移动而不是跳到它:小时 knotch 为(360 * hr) / 12,该小时经过的分钟为(360 * m) / (12 * 60)
相同的概念适用于分钟手。

最后,秒针在从 59 传递到 0 时奇怪地跳跃。这是由于旋转从 359 degres 变为零,而不是变为 360。因此,实际上手的动画向后(counter clockwize)非常快。为了解决这个问题,我只需添加一行即可在 0 秒时删除过渡动画。

secHand.clList.toggle("hand-transition", s != 0);

查看.toggle([cl],[force])

const secHand = document.querySelector(".second-hand");
const minhand = document.querySelector(".minute-hand");
const hrhand = document.querySelector(".hour-hand");
function setDate() {
  const d = new Date();
  const hr = d.getHours();
  const m = d.getMinutes();
  const s = d.getSeconds();
  // Remove the transition at 0 sec.
  secHand.clList.toggle("hand-transition", s != 0);
  const sethrdeg = (360 * hr) / 12 + (360 * m) / (12 * 60) + 90; // 30 * hr + m / 2;
  hrhand.style.transform = `rotate(${sethrdeg}deg)`;
  const seeg = (360 * m) / 60 + (360 * s) / (60 * 60) + 90; // 6 * m;
  minhand.style.transform = `rotate(${seeg}deg)`;
  const setsdeg = (360 / 60) * s + 90; // 6 * s;
  secHand.style.transform = `rotate(${setsdeg}deg)`;
}
setInterval(setDate, 1000);
* {
  background: url(https://images.unsplash.com/photo-1523821741446-edb2b68bb7a0?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHx8&w=1000&q=80);
  margin: 0;
  padding: 0;
  font-family: Georgia, "Times New Roman", Times, serif;
  background-size: cover;
}
body {
  display: flex;
  height: 100vh;
  align-items: center;
}
.clock {
  border: 3px solid black;
  border-radius: 50%;
  padding: 5px;
  position: relative;
  left: 30rem;
  width: 25rem;
  height: 25rem;
  justify-content: center;
  box-shadow: 0 0 0 4px rgba(0, 0, 0, 0.1), inset 0 0 0 3px #efefef,
    inset 0 0 10px black, 0 0 10px rgba(0, 0, 0, 0.2);
}
.clock-face {
  position: relative;
  transform: translateY(-3px);
}
.hand {
  background: white;
  width: 48%;
  height: 1.5%;
  position: absolute;
  top: 50%;
  transform-origin: 100%;
  transform: rotate(90deg);
}
/* Specific cl for the transition, so it can be removed */
.hand-transition {
  transition: all 0.06s;
  transition-timing-function: cubic-bezier(0.1, 1.09, 0.52, 1.26);
}
.minute-hand {
  background: blue;
}
.hour-hand {
  background: red;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta cht="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Clock</title>
  <link rel="stylesheet" href="clock.css">
</head>
<body>
  <div cl="clock">
    <div cl="clock-face"></div>
    <div cl="hand hour-hand"></div>
    <div cl="hand minute-hand"></div>
    <div cl="hand second-hand"></div>
  </div>
  <script src="clock.js"></script>
</body>
</html>
CodePen

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(796)
列表 元组 集合和字典之间有什么区别 (compare list tuple set and dictionary in pyt
上一篇
Strava-具有纬度、经度和时间的团体路线接近度
下一篇

相关推荐

  • vscode code命令:# 如何使用 VSCode 命令

    VSCode Code命令是Visual Studio Code的一个内置命令,用于在本地和远程服务器上执行代码。它可以让你在不同的系统上运行代码,而不需要安装任何软件。…

    2023-01-22 09:55:35
    0 39 21
  • crv的cvt变速箱耐用吗耐用性强,提供良好的驾驶体验

    CR-V的CVT变速箱是一种非常耐用的变速箱,在正常使用情况下,它可以提供几百万英里的使用寿命。它的耐用性取决于如何使用它,建议您遵循以下指南来确保您的变速箱的最佳性能:在发动机启动时,请不要猛踩油门,以免瞬间发动机转速过快,从而破坏变速箱。…

    2023-02-16 02:02:27
    0 30 32
  • cv糖醋排骨是弯的吗弯曲的美味

    cv糖醋排骨不是弯的,它是一种制作方法,通常用来制作排骨。代码:…

    2023-01-30 08:19:31
    0 62 54
  • cv糖醋排骨甜蜜可口的家常美食

    cv糖醋排骨是一道具有浓郁中国风味的菜肴,它以排骨为主料,加入糖和醋,再加上葱、姜、蒜等调料,经过煮炖而成。代码如下:…

    2023-02-10 13:19:14
    0 54 26
  • cv树洞柒夜事件CV树洞中的秘密与惊奇

    cv树洞柒夜事件是一个有趣的计算机视觉活动,它提供了一种新的方式来利用计算机视觉技术来探索和发现景观。它始于2020年7月,由一群热爱计算机视觉的研究者和开发者发起,他们希望通过使用机器学习技术来探索景观中的精彩之处。cv树洞柒夜事件的目标是使用机器学习技术来探索景观中的精彩之处,并利用这些发现来改善景观设计。参与者需要使用机器学习技术来探索景观,并利用这些发现来改善景观设计。参与者可以使用Python,OpenCV,TensorFlow等技术来完成任务,并将其发布在GitHub上。…

    2023-01-08 12:49:50
    0 89 21
  • certification怎么读:如何获得认证,提升职业技能

    示例示例读音:/ˌsəːtɪfɪˈkeɪʃn/代码示例:…

    2023-02-21 06:27:25
    0 60 76
  • cvtdct哪个好如何提升使用cvtdct的效率

    cvtdct是一种用于图像压缩的算法,它可以有效地将图像中的信息减少到最小。它使用一种称为离散余弦变换(DCT)的算法,将图像中的信息转换为频率信息。由于DCT可以将图像中的信息转换为频率信息,因此可以有效地进行压缩。cvtdct好的原因是它可以有效地将图像中的信息减少到最小,从而减少图像文件的大小。此外,它还可以提供良好的图像质量,因为它可以把图像中的信息转换成频率信息,从而减少图像中的噪声。…

    2023-03-14 00:21:41
    0 89 50
  • cv小随:如何利用CV技术提升求职成功率

    示例示例cv小随是一种基于计算机视觉的智能技术,它可以帮助用户自动识别图像中的物体、场景和行为,从而实现自动化的图像处理。cv小随可以用于多种应用场景,如图像搜索、图像分类、图像识别、图像检测等。…

    2023-01-01 11:12:42
    0 95 40

发表评论

登录 后才能评论

评论列表(55条)