• HTML5实现一个时钟动画


    前期准备

    先上效果图,颜色搭配暂时没考虑,以实现功能为主。

    我们首先要理解如何去实现这个时钟,暂时不要考虑动画,学着将问题进行拆解,一步一步实现。

    • 首先我们需要画个方形,有个边框,给一个圆角就可以实现最外边的圆环
    • 再通过一个长的矩形旋转多个就可以实现刻度

    • 只要再画一个白色圆面去覆盖就可以实现标准的刻度

    • 最后再加上三个矩形和中间的小圆面就可以实现时钟的初始状态了

    代码实现

    以上过程理解了之后,代码实现就简单多了,唯一需要考虑的就是代码的优化问题,以下为了简单明了每一步是如何实现,存在很多重复的代码。

    关于动画,我们只需要设置旋转动画就可以了,时分秒针的动画只需要改变不同的时间就可以了。

    具体细节注意见代码:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>时钟</title>
        <style>
            *{
                padding: 0;
                margin: 0;
            }
            .clock{
                 300px;
                height: 300px;
                border: 10px solid #ccc;
                /*百分比参照的是实际宽高*/
                border-radius: 50%;
                margin: 20px auto;
                position: relative;
            }
            .line{
                 8px;
                height: 300px;
                background-color: #ccc;
                position: absolute;
    
                /*实现居中*/
                /*参照父元素的宽*/
                left: 50%;
                top: 0;
                /*参照元素本身*/
                transform: translate(-50%,0);
    
                /*保留,否则会被覆盖*/
    
    
            }
            .line2{
                transform: translate(-50%,0) rotate(30deg);
            }
            .line3{
                transform: translate(-50%,0) rotate(60deg);
            }
            .line4{
                transform: translate(-50%,0) rotate(90deg);
            }
            .line5{
                transform: translate(-50%,0) rotate(120deg);
            }
            .line6{
                transform: translate(-50%,0) rotate(150deg);
            }
    
            .cover{
                 250px;
                height: 250px;
                border-radius: 50%;
                background-color: #fff;
                position: absolute;
                left: 50%;
                top: 50%;
                transform: translate(-50%,-50%);
            }
            .hour{
                 6px;
                height: 80px;
                background-color: red;
                position: absolute;
                left: 50%;
                top: 50%;
                transform: translate(-50%,-100%);
    
                /*设置轴心*/
                transform-origin: center bottom;
                /*动画*/
                -webkit-animation: move 43200s linear infinite;
    
            }
            .minute{
                 4px;
                height: 90px;
                background-color: green;
                position: absolute;
                left: 50%;
                top: 50%;
                transform: translate(-50%,-100%);
    
                /*设置轴心*/
                transform-origin: center bottom;
                /*动画*/
                -webkit-animation: move 3600s linear infinite;
    
    
            }
            .second{
                 2px;
                height: 100px;
                background-color: blue;
                position: absolute;
                left: 50%;
                top: 50%;
                transform: translate(-50%,-100%);
    
                /*设置轴心*/
                transform-origin: center bottom;
                /*动画*/
                -webkit-animation: move 60s infinite steps(60);
                /*linear与step(60)重复*/
    
            }
            .center{
                20px;
                height:20px;
                background-color: #ccc;
                border-radius: 50%;
                position: absolute;
                left: 50%;
                top: 50%;
                transform: translate(-50%,-50%);
    
            }
    
            /*创建移动动画*/
            @keyframes move{
                0%{
                    transform: translate(-50%,-100%) rotate(0deg);
                }
                100%{
                    transform: translate(-50%,-100%) rotate(360deg);
                }
    
            }
        </style>
    </head>
    <body>
    <div class="clock">
        <div class="line line1"></div>
        <div class="line line2"></div>
        <div class="line line3"></div>
        <div class="line line4"></div>
        <div class="line line5"></div>
        <div class="line line6"></div>
        <div class="cover"></div>
        <div class="hour"></div>
        <div class="minute"></div>
        <div class="second"></div>
        <div class="center"></div>
    </div>
    </body>
    </html>
  • 相关阅读:
    点击图片显示原图
    SQL判断语句
    窗口淡入淡出效果
    判断两段时间之间的时间差
    软件行业发展趋势
    VSS客户端不能访问问题“unable to open user login file\\服务器项目管理目录\data\logedin\用户名.log ”
    鑫哥儿子顺利降生了!
    面向对象原则之单一职责原则实现
    PHP编码,乱码问题
    泛型中的default(T)
  • 原文地址:https://www.cnblogs.com/markniefeng/p/10561856.html
Copyright © 2020-2023  润新知