前端js实现时钟案例

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>时钟</title>
  <style>
    body{
      background-color: black;
    }
    .box{
      width: 600px;
      height: 600px;
      border: 10px solid white;
      border-radius: 50%;
      box-sizing: border-box;
      box-shadow:inset 0 0 30px #000;
      background-color: #fff;
      margin: 50px auto;
      position: relative;
    }
    .cicle{
      width: 30px;
      height: 30px;
      border-radius: 50%;
      background-color: #000;
      position: absolute;
      left: calc(50% - 30px / 2);
      top: calc(50% - 30px / 2);
    }
    .hour,.min,.sec{
      border-top-left-radius: 50%;
      border-top-right-radius: 50%;
    }
    .hour{
      width: 6px;
      height: 200px;
      background-color: blue;

      position: absolute;
      top: 120px;
      left: calc(50% - 6px / 2);
      transform-origin: center 170px;
      animation: aa 43200s infinite linear;
      
    }
    .min{
      width: 4px;
      height: 220px;
      background-color: green;
      position: absolute;
      top: 100px;
      left: calc(50% - 4px / 2);
      transform-origin: center 190px;
      animation: aa 3600s infinite linear;
    }
    .sec{
      width: 2px;
      height: 240px;
      background-color: red;
      position: absolute;
      top: 80px;
      left: calc(50% - 2px / 2);
      transform-origin: center 210px;
      animation: aa 60s infinite steps(60);
    }
    @keyframes aa {
      0%{
        transform: rotate(0deg);
      }
      100%{
        transform: rotate(360deg);
      }
    }
    .scale {
        position: absolute;
        left: calc((100% - 4px) / 2);
        top: calc((100% - 20px) / 2);
        width: 2px;
        height: 20px;
        background-color: #000;
        transform-origin: center center;
      }
      .num {
        position: absolute;
        left: calc((100% - 50px) / 2);
        top: calc((100% - 50px) / 2);
        width: 50px;
        height: 50px;
        font-size: 40px;
        text-align: center;
        line-height: 50px;
        user-select: none;
      }
  </style>
</head>
<body>
  <div class="box">
    <div class="cicle"></div>
    <div class="hour"></div>
    <div class="min"></div>
    <div class="sec"></div>
    <div class="cicle"></div>
  </div>

  <script>
    var box = document.querySelector(".box");
    var hours = document.querySelector(".hour");
    var min = document.querySelector(".min");
    var sec = document.querySelector(".sec");

    var date = new Date();
    var h = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();

    sec.style.animationDelay = -s + 's';
    min.style.animationDelay = -(s + m * 60) + "s";
    hours.style.animationDelay = -(s + m * 60 + h * 3600) + "s";

    for(var i=0;i<60;i++){
      var scale = document.createElement('div');
      scale.classList.add('scale');
      scale.style.transform = "rotate(" + i * 6 + "deg) translateY(-280px)";
      if (i % 5 === 0) {
        scale.style.height = "30px";
        scale.style.top = "calc((100% - 30px) / 2)";
        scale.style.transform = "rotate(" + i * 6 + "deg) translateY(-275px)";
      }
      box.appendChild(scale);
    }
    for (var i = 1; i < 13; i++) {
      var num = document.createElement("span");
      num.innerText = i;
      num.classList.add("num");
      num.style.transform =
        "rotate(" +
        i * 30 +
        "deg) translateY(-220px) rotate(" +
        -i * 30 +
        "deg)";
      box.appendChild(num);
    }
  </script>
</body>
</html>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值