JavaScript實現數字時鐘

利用定時器實現數字時鐘

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
        *{margin: 0px;padding: 0px;}
        #clock{
            width: 600px;
            height: 600px;
            background: url("images/clock.jpg") no-repeat;
            position: relative;
        }
        #hour,#minute,#second{
            position: absolute;
            width: 30px;
            height: 600px;
            left: 50%;
            top: 0;
            margin-left: -15px;
        }
        #hour{
            background: url("images/hour.png") no-repeat center center;
        }
        #minute{
            background: url("images/minute.png") no-repeat center center;
        }
        #second{
            background: url("images/second.png") no-repeat center center;
        }
    </style>
</head>
<body>
    <div id="clock">
        <div id="hour"></div>
        <div id="minute"></div>
        <div id="second"></div>
    </div>
    <script>
        var hour=document.getElementById('hour');
        var minute=document.getElementById('minute');
        var second=document.getElementById('second');
        setInterval(function(){
            var now=new Date();
            //獲取秒、分、時
            var s=now.getSeconds();
            var m=now.getMinutes()+s/60;
            var h=now.getHours()%12+m/60;
            //旋轉
            second.style.transform=`rotate(${s*6}deg)`;
            minute.style.transform=`rotate(${m*6}deg)`;
            hour.style.transform=`rotate(${h*30}deg)`;
        },10);
    </script>
</body>
</html>

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章