当前位置 博文首页 > 小丞同学:圆盘时钟效果 原生JS

    小丞同学:圆盘时钟效果 原生JS

    作者:[db:作者] 时间:2021-07-20 18:40

    圆盘时钟 旋转时钟 数字时钟

    写在前面

    仿荣耀手机时钟,设计的同款时钟效果

    实现效果

    在这里插入图片描述

    实现原理

    数字时钟

    1. 利用Date内置对象获取当下的时间,通过处理呈现在页面上

    这一步获取时间是非常简单的,通过Date下的一些属性就可以实现了

    1. 背景采用的是一个炫彩流光的效果,利用了CSS3新增的动画效果

    这部分的炫彩流光效果在之前的博客中有详细讲到

    炫彩流光效果

    指针时钟

    1. 通过定位将三根针重叠在一起,下端对齐都摆在原点,通过transfrom-origin属性,将旋转原点定为底部,也就是,让三根针转的时候绕中心点转

    2. 在获取到时间的基础上,将时间转化为旋转的角度,每一个小时时针转30度,每分钟分钟转6度,每秒钟秒针转6度

    这里有一点点的计算,需要理解一下

    1. 为了让分针和时针是缓慢移动的,而不是突然间的抖动,让秒针转的时候,实时反馈给分针,分针转的时候反馈给时针
    'rotate(' + (rotateH + (rotateM / 60)) + 'deg' + ')'
    

    就像这样把分针旋转了多少反馈给时针,60分钟一个小时,所以是除以60

    旋转时钟

    1. 这里采用的是clip-path属性采取另一半的圆,圆环的效果采用的是大小圆的思路,用小圆盖在大半圆的上方
    2. 圆环的旋转和指针的旋转异曲同工,圆环是反馈秒钟的,只需根据秒钟来判断即可
    3. 为了让圆能够自转,需要将旋转原点设置为圆的中心点

    实现代码

    HTML结构

    <!-- 转盘时钟 -->
    <div class="clocker">
        <span class="long"></span>
        <span class="short"></span>
        <span class="mini"></span>
        <i class="twelve">12</i>
        <i class="three">3</i>
        <i class="six">6</i>
        <i class="nine">9</i>
        <div class="round"></div>
    </div>
    <!-- 旋转时钟 -->
    <div class="rot">
        <div class="timing"></div>
        <div class="outer"></div>
        <div class="inner"></div>
    </div>
    <!-- 显示日期 -->
    <div class="date">
        <p>中国标准时间</p>
        <div class="now"></div>
    </div>
    <!-- 数字时钟 -->
    <div class = "clock">
        <span class ="hour"></span>
        <span class ="minute"></span>
        <span class ="second"></span>
    </div>
    

    CSS部分

    * {
        margin: 0;
        padding: 0;
    }
    body {
        background-color: black;
    }
    @keyframes move {
        0% {
            background-position: 0%;
        }
        100% {
            background-position: 100%;
        }
    }
    /* 数字炫彩时钟 */
    .clock {
        display: flex;
        position: relative;
        width: 400px;
        height: 100px;
        margin: 20px auto;
        background: linear-gradient(90deg,rgb(39,122,218),rgb(74,230,121),rgb(201,214,13),rgb(226,20,233),rgb(16,172,219));
        background-size: 400%;
        border-radius: 10px;
        justify-content: center;
        align-items: center;
        animation: move 3s linear infinite alternate;
    }
    .clock::before {
        content: '';
        position: absolute;
        top: -5px;
        left: -5px;
        width: 410px;
        height: 110px;
        background: linear-gradient(90deg,rgb(39,122,218),rgb(74,230,121),rgb(201,214,13),rgb(226,20,233),rgb(16,172,219));
        background-size: 400%;
        opacity: 1;
        border-radius: 10px;
        transition: all .6s;
        z-index: -1;
        filter: blur(10px);
        animation: move 3s linear infinite alternate;
    }
    .clock span {
        color: white;
        font-size: 50px;
        margin: 0px 30px;
        text-shadow: 0px 0px 12px white;
    }
    /* 日期样式 */
    .date {
        width: 300px;
        height: 100px;
        margin: 0 auto;
    }
    p {
        text-align: center;
        letter-spacing: 4px;
        color: white;
        font-size: 34px;
    }
    .date .now {
        color: white;
        font-size: 20px;
        margin-top: 10px;
        letter-spacing: 3px;
        text-align: center;
    }
    /* 表盘 */
    .clocker {
        /* display: none; */
        position: relative;
        width: 400px;
        height: 400px;
        border-radius: 50%;
        margin: 50px auto;  
        background-color: rgb(179, 179, 179); 
    }
    .clocker span {
        display: inline-block;
        position: absolute;
        left: 50%;
        transform-origin: bottom;
    }
    /* 三根针 */
    .long {
        top: 30px;
        width: 4px;
        height: 170px;
        background: linear-gradient(rgb(39,122,218),rgb(226,20,233),pink);
        background-size: 400%;
        transform: translate(-2px,0);
    }
    .short {
        position: relative;
        top: 60px;
        width: 8px;
        height: 140px;
        border-radius: 4px;
        background-color: black;
        transform: translate(-4px,0);
    }
    .short::before {
        position: absolute;
        bottom: 0;
        left: 3px;
        content: '';
        width: 2px;
        height: 60px;
        background-color: white;
        border-radius: 1px;
    }
    .mini {
        position: relative;
        top: 100px;
        width: 10px;
        height: 100px;
        border-radius: 5px ;
        background-color: black;
        transform: translate(-3px,0);
    }
    .mini::before {
        position: absolute;
        bottom: 0;
        left: 4px;
        content: '';
        width: 2px;
        height: 40px;
        background-color: white;
        border-radius: 1px;
    }
    /* 中间的小圆圈 */
    .round {
        position: absolute;
        width: 20px;
        height: 20px;
        border: px solid black;
        /* background-color: white; */
        background: linear-gradient(rgb(39,122,218),rgb