博主信息
博文 41
粉丝 0
评论 1
访问量 41595
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
js案例时钟
kong
原创
972人浏览过

微信图片_20200708140612.png


<!DOCTYPE html>

<html lang="en">


<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>时钟</title>

</head>

<style>

    html,

    body {

        height: 100%;

        overflow: hidden;

    }

    

    * {

        padding: 0;

        margin: 0;

    }

    

    ul {

        list-style: none;

    }

    

    #wrap {

        width: 200px;

        height: 200px;

        border: 2px solid #000;

        border-radius: 50%;

        position: absolute;

        left: 50%;

        top: 50%;

        transform: translate(-50%, -50%);

    }

    

    #wrap ul li {

        width: 2px;

        height: 6px;

        position: absolute;

        background-color: #000;

        left: 50%;

        top: 0;

        margin-left: -1px;

        transform-origin: center 100px;

    }

    

    #wrap ul li:nth-child(5n+1) {

        height: 10px;

    }

    

    .hour {

        position: absolute;

        left: 97px;

        width: 6px;

        top: 70px;

        height: 30px;

        background: blue;

        transform-origin: center bottom;

    }

    

    .min {

        position: absolute;

        left: 98px;

        top: 50px;

        width: 4px;

        height: 50px;

        background: brown;

        transform-origin: center bottom;

    }

    

    .sec {

        position: absolute;

        left: 99px;

        top: 30px;

        width: 2px;

        height: 70px;

        background: rgb(248, 3, 3);

        transform-origin: center bottom;

    }

    

    .icon {

        position: absolute;

        left: 90px;

        top: 90px;

        width: 20px;

        height: 20px;

        border-radius: 50%;

        background: pink;

    }

</style>


<body>

    <div id="wrap">

        <ul></ul>

        <div class="hour"></div>

        <div class="min"></div>

        <div class="sec"></div>

        <div class="icon"></div>

    </div>

   <script>
        window.onload = function() {
            //获取时间针
            var hourNode = document.querySelector("#wrap>.hour");
            var minNode = document.querySelector("#wrap>.min");
            var secNode = document.querySelector("#wrap>.sec");
            //获取ul容器
            var ulNode = document.querySelector("#wrap>ul");
            //创建一个li的容器
            var liHtml = "";
            //创建一个style标签
            var styleNode = document.createElement("style");
            //创建一个style容器
            var liStyle = "";
            //创建li
            for (var i = 0; i < 60; i++) {
                //追加到li的容器里
                liHtml += "<li></li>";
                //追加样式
                liStyle += "#wrap ul li:nth-child(" + (i + 1) + ") {transform: rotate(" + (i * 6) + "deg)}";
                styleNode.innerHTML = liStyle;
                document.head.appendChild(styleNode);
                //liHtml追加到ul里
                ulNode.innerHTML = liHtml;
            };
            //先执行
            move();
            //定时器
            setInterval(move, 1000);
            function move() {
                //获取时间戳
                var date = new Date();
                var s = date.getSeconds();
                var m = date.getMinutes() + s / 60;
                var h = date.getHours() + m / 60;
                //计算每秒各自旋转的角度
                hourNode.style.transform = "rotate(" + (30 * h) + "deg)";
                minNode.style.transform = "rotate(" + (6 * m) + "deg)";
                secNode.style.transform = "rotate(" + (6 * s) + "deg)";
            };
        }
    </script>

</body>


</html>


本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学