• JS30


    效果展示

    代码展示

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <title>JS + CSS Clock</title>
    </head>
    
    <body>
    
    
        <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>
        </div>
    
    
        <style>
            html {
                background: #018DED;
                background-size: cover;
                font-family: 'helvetica neue';
                text-align: center;
                font-size: 10px;
            }
    
            body {
                margin: 0;
                font-size: 2rem;
                display: flex;
                flex: 1;
                min-height: 100vh;
                align-items: center;
            }
    
            .clock {
                 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;
                 100%;
                height: 100%;
                transform: translateY(-3px);
                /* account for the height of the clock hands */
            }
    
            .hand {
                 50%;
                height: 6px;
                background: black;
                position: absolute;
                top: 50%;
                transform-origin: 100%;
                transform: rotate(90deg);
                transition: all 0.05s;
                transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
            }
        </style>
    
        <script>
            (function () {
                let secondHand = document.querySelector('.second-hand');
                let minHand = document.querySelector('.min-hand');
                let hourHand = document.querySelector('.hour-hand');
    
                function setClock() {
                    let now = new Date();
    
                    let secondDeg = now.getSeconds() * 6 + 90;    // 从 12 点起算,而初始位置为 9 点,需加 90 度
                    let minDeg = now.getMinutes() * 6 + now.getSeconds() / 60 * 6 + 90;
                    let hourDeg = now.getHours() * 30 + now.getMinutes() / 60 * 30 + 90;
    
                    secondHand.style.transform = `rotate(${secondDeg}deg)`;
                    minHand.style.transform = `rotate(${minDeg}deg)`;
                    hourHand.style.transform = `rotate(${hourDeg}deg)`;
                    // console.log(second.style.transform);
                }
    
                // 方法1 
                // setClock();
                // setInterval(setClock, 1000);
    
                // 方法2
                function animationHandler() {
                    setClock();
                    window.requestAnimationFrame(animationHandler, 1000);
                }
                window.requestAnimationFrame(animationHandler, 1000);    // 可当做是能够处理画面更新的 setTimeout
            })();
        </script>
    </body>
    
    </html>
    

    参考

  • 相关阅读:
    (void) (&_x == &_y)的作用
    GNU C 与 ANSI C(下)
    GNU C 与 ANSI C(上)
    “多个单核CPU”与“单个多核CPU”哪种方式性能较强?
    ARM 处理器寻址方式之间接寻址的几种表达
    Video for Linux Two API Specification
    UVC 驱动调用过程与驱动框架的简单分析
    线程安全
    合法的立即数的判断
    Redis的Java客户端Jedis
  • 原文地址:https://www.cnblogs.com/Ohmy/p/13520897.html
Copyright © 2020-2023  润新知