css3 简易时钟

本次简易时钟,主要用到定位 position,css3中的 transform-origin ,还有旋转 rotate

time.png

全代码打包

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>钟表练习</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        .outer{
            width:200px;
            height: 200px;
            margin: 100px auto;
            background-color: #548abb;
            border-radius: 50%;
            position: relative;
        }
        ul{
            width:200px;
            height: 200px;
            position: relative;
        }
        ul li{
            width: 2px;
            height: 6px;
            background-color: #000000;
            transform-origin: 2px 100px;
            position: absolute;
            left: 98px;
            top: 0;
        }
        ul li:nth-child(5n+1){
            height: 10px;
            width: 2px;
            background-color: #cb5381;
        }
        .cir{
            width: 10px;
            height: 10px;
            background-color: #cb5381;
            border-radius: 50%;
            position: absolute;
            left: 50%;
            top: 50%;
            margin-left: -5px;
            margin-top: -5px;

        }
        .hour{
            width: 6px;
            height: 70px;
            border-radius: 5px;
            background-color: #ff9900;
            transform-origin: 50% 100%;
            position: absolute;
            left: 98px;
            top: 30px;

        }
        .minu{
            width: 4px;
            height: 80px;
            border-radius: 5px;
            background-color: #5bff72;
            transform-origin: 50% 100%;
            position: absolute;
            left: 99px;
            top: 20px;
        }
        .sec{
            width: 2px;
            height: 90px;
            border-radius: 5px;
            background-color: #ff3315;
            transform-origin: 50% 100%;
            position: absolute;
            left: 98px;
            top: 10px;
        }


    </style>
</head>
<body>
<div id="wrap">
    <div class="outer">
        <ul>
            <li></li>
<!--            60个li-->
      
        </ul>
        <div class="hour"></div>
        <div class="minu"></div>
        <div class="sec"></div>
        <div class="cir"></div>
    </div>
</div>
<script>
    var olis = document.querySelectorAll("ul li");//querySelectorAll 结果为数组
    var oHour = document.querySelector(".hour");
    var oMinu = document.querySelector(".minu");
    var oSec = document.querySelector(".sec");
  
    olis.forEach(function (item,index,arr) {
        //遍历设置li的角度
        item.style.transform = "rotate("+ index * 6+"deg)";
    })
    move();
    function move(){
        var now = new Date();
        var sec = now.getSeconds();

        var minu = now.getMinutes();
        minu = minu + sec/60;
        var hour = now.getHours();
        hour = hour + minu/60;

        oSec.style.transform = "rotate("+ sec * 6 +"deg)";
        oMinu.style.transform = "rotate("+ minu * 6 +"deg)";
        oHour.style.transform = "rotate("+ hour * 30 +"deg)";
    }
    setInterval(move,1000);
    
</script>

</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 各种纯css图标 CSS3可以实现很多漂亮的图形,我收集了32种图形,在下面列出。直接用CSS3画出这些图形,要比...
    剑残阅读 9,720评论 0 8
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    love2013阅读 2,350评论 0 11
  • CSS参考手册 一、初识CSS3 1.1 CSS是什么 CSS3在CSS2.1的基础上增加了很多强大的新功能。目前...
    没汁帅阅读 3,796评论 1 13
  • 如果要我去买书,我想大概不会买这本。我和很多年轻人一样喜欢干货,或者那种名人传记类的书。能看出我多浮躁多急切的想要...
    dakedou阅读 2,753评论 0 3
  • 读过几页村上先生的文字 靠着床头坐直 闭眼 一阵愉悦涌起 曾经烦恼的问题 似乎已经散去 或许 我本是颗健忘的脑子 ...
    彩色的陶俑阅读 133评论 0 1