CSS + JS 实现时钟

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<style>
  * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: '微软雅黑', sans-serif;
  }

  body {
    min-height: 100vh;
    background-color: #091921;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .clock {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 400px;
    height: 400px;
    background: url('/clock.png');
    background-size: cover;
    border-radius: 50%;
    border: 3px solid #091921;
    box-shadow: 0 -15px 15px rgba(255, 255, 255, 0.05),
      inset 0 -15px 15px rgba(255, 255, 255, 0.05),
      0 15px 15px rgba(0, 0, 0, 0.1),
      inset 0 15px 15px rgba(0, 0, 0, 0.1);
  }

  .clock::before {
    content: '';
    position: absolute;
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: #fff;
    z-index: 999;
  }

  .hour,
  .min,
  .second {
    position: absolute;
  }

  .hour,
  .hr {
    width: 160px;
    height: 160px;
  }

  .min,
  .mn {
    width: 190px;
    height: 190px;
  }

  .second,
  .sc {
    width: 230px;
    height: 230px;
  }

  .hr,
  .mn,
  .sc {
    display: flex;
    justify-content: center;
    position: absolute;
    border-radius: 50%;
  }

  .hr::before {
    content: '';
    position: absolute;
    width: 8px;
    height: 80px;
    z-index: 10;
    background-color: #99ffcc;
    border-radius: 6px 6px 0 0;
  }

  .mn::before {
    content: '';
    position: absolute;
    width: 4px;
    height: 100px;
    z-index: 11;
    background-color: #9fc;
    border-radius: 6px 6px 0 0;
  }

  .sc::before {
    content: '';
    position: absolute;
    width: 2px;
    height: 150px;
    z-index: 12;
    background-color: #9fc;
    border-radius: 6px 6px 0 0;
  }
</style>

<body>
  <div class="clock">
    <div class="hour">
      <div class="hr"></div>
    </div>
    <div class="min">
      <div class="mn"></div>
    </div>
    <div class="second">
      <div class="sc"></div>
    </div>
  </div>
  <script>
    var deg = 6;
    var hr = document.querySelector('.hr');
    var mn = document.querySelector('.mn');
    var sc = document.querySelector('.sc');
    setInterval(() => {
      var time = new Date();
      var hh = time.getHours() * 30;
      var mm = time.getMinutes() * deg;
      var ss = time.getSeconds() * deg;
      hr.style.transform = `rotateZ(${(hh) + (mm/12)}deg)`;
      mn.style.transform = `rotate(${(mm)}deg)`;
      sc.style.transform = `rotate(${(ss)}deg)`;
    }, 1000);
  </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值