• 拟态时钟动画


    效果展示

    在这里插入图片描述

    CSS 知识点

    • box-shadow 属性灵活运用

      通过此属性来创建效果较好的仪表盘效果。

    页面整体布局

    
    <div class="clock">
      <div class="numbers">
        <span style="--i:0"><b>12b>span>
        <span style="--i:1"><b>3b>span>
        <span style="--i:2"><b>6b>span>
        <span style="--i:3"><b>9b>span>
    
        <div class="circle" id="hr"><i>i>div>
        <div class="circle" id="mn"><i>i>div>
        <div class="circle" id="sc"><i>i>div>
      div>
    div>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    编写时钟仪表盘

    .clock {
      position: absolute;
      top: 50%;
      right: 220px;
      transform: translateY(-50%);
      width: 400px;
      height: 400px;
      display: flex;
      justify-content: center;
      align-items: center;
      background: #c9d5e0;
      border-radius: 50px;
      box-shadow: 30px 30px 30px -10px rgba(0, 0, 0, 0.15), inset 15px 15px 10px
          rgba(255, 255, 255, 0.75), -15px -15px 35px rgba(255, 255, 255, 0.55), inset -1px -1px
          10px rgba(0, 0, 0, 0.2);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    绘制时钟上的数值

    .clock .numbers {
      position: absolute;
      inset: 35px;
      background: #152b4a;
      border-radius: 50%;
      box-shadow: 5px 5px 15px #152b4a66, inset 5px 5px 5px rgba(255, 255, 255, 0.55),
        -6px -6px 10px rgba(255, 255, 255, 1);
    }
    
    .clock .numbers span {
      position: absolute;
      inset: 5px;
      text-align: center;
      color: #fff;
      font-size: 1.25em;
      transform: rotate(calc(90deg * var(--i)));
    }
    
    .clock .numbers span b {
      font-weight: 600;
      display: inline-block;
      transform: rotate(calc(-90deg * var(--i)));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    绘制时钟的指针

    .clock .numbers .circle {
      position: absolute;
      inset: 0;
      border-radius: 50%;
      display: flex;
      justify-content: center;
      z-index: 10;
    }
    
    .clock .numbers .circle i {
      position: absolute;
      width: 3px;
      height: 50%;
      background: #fff;
      transform-origin: bottom;
    }
    
    .clock .numbers .circle#hr i {
      transform: scaleY(0.5);
      width: 4px;
    }
    
    .clock .numbers .circle#mn i {
      transform: scaleY(0.65);
    }
    
    .clock .numbers .circle#sc i {
      width: 2px;
      transform: scaleY(0.7);
      background: #e91e63;
      box-shadow: 0 30px 0 #e91e63;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    编写时钟边框(中心圆)

    .clock .numbers::before {
      content: "";
      position: absolute;
      inset: 35px;
      background: linear-gradient(#2196f3, #e91e63);
      border-radius: 50%;
      animation: animate 2s linear infinite;
    }
    
    .clock .numbers::after {
      content: "";
      position: absolute;
      inset: 38px;
      background: #152b4a;
      border-radius: 50%;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    编写时钟事件

    let hr = document.querySelector("#hr");
    let mn = document.querySelector("#mn");
    let sc = document.querySelector("#sc");
    
    setInterval(() => {
      let day = new Date();
      let hh = day.getHours() * 30;
      let mm = day.getMinutes() * 6;
      let ss = day.getSeconds() * 6;
    
      hr.style.transform = `rotateZ(${hh + mm / 12}deg)`;
      mn.style.transform = `rotateZ(${mm}deg)`;
      sc.style.transform = `rotateZ(${ss}deg)`;
    }, 1000);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    完整代码下载

    完整代码下载

  • 相关阅读:
    【vue2+onlyoffice】基础预览demo运行+问题解决
    深度学习——day22 知识回顾class1:什么是神经网络
    Windows系统配置CUDA编程环境
    索引优化与查询优化
    怎么理解Java内存区域
    【SQL解析】- Druid SQL AST 01
    计算机网络 第3 章 数据链路层
    并查集(Union-Find)
    系统内存管理:虚拟内存、内存分段与分页、页表缓存TLB以及Linux内存管理
    【Java】运算符以及JShell脚本工具
  • 原文地址:https://blog.csdn.net/qq_33003143/article/details/134287000