JS30 - 02 JS and CSS Clock

效果展示

代码展示

<!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 {
            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 {
            width: 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>

参考

posted @ 2020-08-17 23:32  Ohmy~  阅读(163)  评论(0编辑  收藏  举报