JavaScript30_02 - JS + CSS Clock

02 JS + CSS Clock

实现效果

在这里插入图片描述

钟表显示当前时间。
实现效果预览

码云仓库

git上有更详细的分析

分析

指针旋转效果:
利用JS设置transform:rotate(90deg)来实现旋转效果

指针旋转的轴:
默认是以每个指针的中心为轴旋转,不符合要求。
在这里插入图片描述

使用transform-origin来改变旋转的轴

时间的获取:
使用new Date()创建时间对象。并且通过date.getSeconds() | data.getMinutes() | data.getHours() 等方法获取当前时间对应的时、分、秒数。通过简单的数学计算得到当前时间各个指针对应的角度。

实现:

HTML结构:

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

CSS样式:

html {
      background:#018DED url(background.jpg);
      background-size:cover;
      font-family:'helvetica neue';
      text-align: center;
      font-size: 10px;
    }

    body {
      font-size: 2rem;
      display:flex;
      flex:1;
      min-height: 100vh;
      align-items: center;
    }

    .clock {
      width: 30rem;
      height: 30rem;
      border:20px solid white;
      border-radius:50%;
      margin:50px auto;
      position: relative;
      padding:2rem;
      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;
      width: 100%;
      height: 100%;
      transform: translateY(-3px); /* account for the height of the clock hands */
    }

    .hand,.center {
      width:50%;
      height:6px;
      background:black;
      position: absolute;
      top:50%;

      /*transform-origin这个样式是用来与rotate样式配合,控制旋转中心的
      *设置为transiform-origin:100% 100%;表示以元素右下角为旋转中心
      * transiform-origin:100% 50%,以最右边中点为轴。
      */
      transform-origin:100% 50%;
      transition:all 0.05s;
      border-radius:6px

    }
    .second-hand{
      height: 3px;
      background-color: red;
    }
    .min-hand{
      background: white;
      height: 8px;
    }
    .hour-hand{
      background-color: white;
      height: 10px;
      width: 12rem;
      left: 3rem;
    }
    .center{
      width: 20px;
      height: 20px;
      background-color: #9Ebccc;
      left: 50%;
      margin-left:-10px;
      margin-top:-6px;
      border-radius:50%;
    }

JS代码:

    const secondHand = document.querySelector(".second-hand");
    const minHand = document.querySelector(".min-hand");
    const hourHand = document.querySelector(".hour-hand");

    function setTime(){
        const date = new Date();

        const seconds = date.getSeconds();
        const secondDegree = (seconds/60)*360 + 90;
        // console.log("secondes:", seconds); 48 会直接得到相应的秒数不用转换
        secondHand.style.transform = `rotate(${secondDegree}deg)`;

        const minutes = date.getMinutes();
        const minsDegree = (minutes/60)*360 + 90;
        // console.log("minutes:", minutes); 55
        minHand.style.transform = `rotate(${minsDegree}deg)`;

        const hours = date.getHours();
        const hoursDegree = (hours/60)*360 + 90;
        // console.log("hours:", hours);  15
        hourHand.style.transform = `rotate(${hoursDegree}deg)`;


    }

    setInterval(setTime,1000);

    // setInterval的第一次执行是要在一秒以后的
    setTime();

重点;

  • new Date()
    • date.getSeconds()
    • date.getMinutes()
    • date.getHours()
  • transform:rotate(90deg)
    • transform-origin:100%;
  • border-radius
  • setInterval(callback,1000)回调函数在1s后才开始执行
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值