绘制表盘

效果:
在这里插入图片描述代码:
注意:代码中的js库用的是之前文章发过的myjs.js库

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="common.css">
    <style id="sty">
        #wrap{
            width:404px;
            height:404px;
            border:2px solid black;
            margin:100px auto;
            border-radius:50%;
            position:relative;
        }
        #h{
            width:8px;
            height:100px;
            background-color:#000;
            position:absolute;
            left:196px;
            top:100px;
            transform-origin:center bottom;
        }
        #m{
            width:6px;
            height:140px;
            background-color:#666;
            position:absolute;
            left:197px;
            top:60px;
            transform-origin:center bottom;
        }
        #s{
            width:4px;
            height:170px;
            background-color:red;
            position:absolute;
            left:198px;
            top:30px;
            transform-origin:center bottom;
            /* transform:rotate(90deg); */
        }
        li{
            width:4px;
            height:15px;
            background-color:black;
            position:absolute;
            left:198px;
            top:0;
            transform-origin:center 200px;
        }
        li:nth-child(5n+1){
            height:25px;
        }
        #point{
            width:20px;
            height:20px;
            background-color:orange;
            position:absolute;
            /* 让子元素在父元素中居中 */
            left:190px;
            top:190px;
            border-radius:50%;
        }
    </style>
</head>
<body>
    <div id="wrap">
        <div id="h"></div>
        <div id="m"></div>
        <div id="s"></div>
        <div id="point"></div>
        <ul></ul>
    </div>
    <script src="myjs.js"></script>
    <script>
        var oUl=document.getElementsByTagName("ul")[0];
        var oSty=document.getElementById("sty");
        var str="";
        var aLi=document.getElementsByTagName("li");
        var oH=document.getElementById("h");
        var oM=document.getElementById("m");
        var oS=document.getElementById("s");
        for(var i=0;i<60;i++){
            str+="<li></li>";
            // 让li在正确的位置显示
            oSty.innerHTML += "li:nth-child("+(i+1)+"){transform:rotate("+i*6+"deg);}"
        }
        oUl.innerHTML=str;
        // 获取当前时间时分秒和指针时分秒的转动角度对应起来
        showTime();
        setInterval(showTime,1000);
        function showTime(){
            var oTime=new Date();
            var iH=oTime.getHours();
            var iMi=oTime.getMinutes();
            var iS=oTime.getSeconds();
            var hDeg=iH*30+iMi*0.5;
            var mDeg=iMi*6+iS*0.1;
            var sDeg=iS*6;
            oH.style.cssText="transform:rotate("+hDeg+"deg);";
            oM.style.cssText="transform:rotate("+mDeg+"deg);";
            oS.style.cssText="transform:rotate("+sDeg+"deg);";
        }
    </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值