炫酷时钟(css3+js)

html

<body>
        <ul id='box'></ul>
</body>

css

<style>
            *{
                margin: 0;
                padding: 0;
            }
            body{
                background-color: #aaa;
            }
            ul{
                width: 400px;
                height: 400px;
                border: 5px solid skyblue;
                margin: 100px auto 0 auto;
                border-radius: 50%;
                 background: radial-gradient(green 50%, yellow 100%);;
                position: relative;

            }
            ul li{
                width: 2px;
                height: 15px;
                list-style: none;
                background-color: #fff;
                position: absolute;
                left: 199px;
                transform-origin: center 200px;
            }

            h1{
                width: 2px;
                height: 180px;
                background-color: orange;
                position: absolute;
                left: 199px;
                top:20px;
                -transition: 1s linear;
                transform-origin: center 180px;
            }
            h2{
                width: 6px;
                height: 160px;
                background-color: #fff;
                position: absolute;
                left: 197px;
                top:40px;
                transform-origin: center 160px;
                border-radius:20%;
            }

            h3{
                width: 8px;
                height: 140px;
                background-color: #fff;
                position: absolute;
                left: 196px;
                top:60px;
                transform-origin: center 140px;
                transform: rotate(0deg);
                border-radius: 20%;
            }
            h4{
                width: 30px;
                height: 30px;
                position: absolute;
                left: 185px;
                top:185px;
                border-radius: 50%;
                background-color: orange;
            }

            span{
                display: inline-block;
                width: 20px;
                height: 20px;
                line-height: 20px;
                text-align: center;
                font-size: 24px;
                position: absolute;
                left: -10px;
                top: 28px;
                color: #fff;
            }

        </style>

js

(function(){
            var oUl=document.getElementById('box');
            var timer=null;
            for(var i=0,j=0;i<60;i++,j+=6){
                var oLi=document.createElement('li');
                oLi.style.transform='rotate('+j+'deg)';
                if(i%5==0){
                    oLi.style.height='20px';
                    var oSpan=document.createElement('span');
                    oSpan.style.transform='rotate('+(-j)+'deg)';
                    if(i==0){
                        oSpan.innerHTML='12';
                        oSpan.style.fontSize='30px';
                        oSpan.style.left='-17px';
                    }else{
                        oSpan.innerHTML=parseInt(i/5);
                        if(i%15==0){
                            oSpan.style.fontSize='36px';
                        }
                    }
                    oLi.appendChild(oSpan);
                }
                if(i==0){
                    var oH1=document.createElement('h1');
                    var oH2=document.createElement('h2');
                    var oH3=document.createElement('h3');
                    var oH4=document.createElement('h4');
                    oUl.appendChild(oH1);
                    oUl.appendChild(oH2);
                    oUl.appendChild(oH3);
                    oUl.appendChild(oH4);
                }
                oUl.appendChild(oLi);
            }
            var oH=document.getElementsByTagName('h3')[0];
            var oM=document.getElementsByTagName('h2')[0];
            var oS=document.getElementsByTagName('h1')[0];
            timer=setInterval(function(){
                 var now = new Date();
                 var s=now.getSeconds();
                 var m=now.getMinutes()+s/60;
                 var h=now.getHours()+m/60;
                 oS.style.transform='rotate('+s*6+'deg)'; oM.style.transform='rotate('+m*6+'deg)';
                 oH.style.transform='rotate('+(h%12)*30+'deg)'; },30); })();

作品截图
这里写图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一份使用 HTML+CSS+JS 实现时钟的示例代码: ```html <!DOCTYPE html> <html> <head> <title>Cool Clock</title> <style type="text/css"> body { background-color: #333; } .clock { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); border-radius: 50%; background-color: #fff; box-shadow: 0 0 20px rgba(0, 0, 0, 0.1); width: 300px; height: 300px; display: flex; align-items: center; justify-content: center; flex-direction: column; } .clock .hour, .clock .minute, .clock .second { position: relative; width: 70%; height: 70%; margin: auto; border-radius: 50%; background-color: #333; display: flex; align-items: center; justify-content: center; font-size: 2rem; color: #fff; } .clock .hour:before, .clock .minute:before, .clock .second:before { content: ""; position: absolute; top: 0; left: 50%; transform: translate(-50%, 0); height: 50%; width: 5px; background-color: #fff; border-radius: 5px; } .clock .hour { z-index: 3; } .clock .hour:before { height: 30%; } .clock .minute { z-index: 2; } .clock .minute:before { height: 40%; } .clock .second { z-index: 1; } .clock .second:before { height: 50%; } .clock .tick { position: absolute; top: 0; left: 50%; transform: translate(-50%, 0); height: 10%; width: 5px; background-color: #fff; border-radius: 5px; transform-origin: center 90%; animation: tick 1s linear infinite; } .clock .tick:nth-child(5n+1) { height: 20%; } .clock .tick:nth-child(10n+1) { height: 30%; } .clock .tick:nth-child(15n+1) { height: 40%; } .clock .tick:nth-child(20n+1) { height: 50%; } @keyframes tick { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } </style> </head> <body> <div class="clock"> <div class="hour"></div> <div class="minute"></div> <div class="second"></div> <div class="tick"></div> <div class="tick"></div> <div class="tick"></div> <div class="tick"></div> <div class="tick"></div> <div class="tick"></div> <div class="tick"></div> <div class="tick"></div> <div class="tick"></div> <div class="tick"></div> </div> <script type="text/javascript"> function updateClock() { var now = new Date(); var hour = now.getHours(); var minute = now.getMinutes(); var second = now.getSeconds(); var hourHand = document.querySelector(".hour"); var minuteHand = document.querySelector(".minute"); var secondHand = document.querySelector(".second"); var hourAngle = (hour % 12) / 12 * 360 + minute / 60 * 30; var minuteAngle = minute / 60 * 360; var secondAngle = second / 60 * 360; hourHand.style.transform = "rotate(" + hourAngle + "deg)"; minuteHand.style.transform = "rotate(" + minuteAngle + "deg)"; secondHand.style.transform = "rotate(" + secondAngle + "deg)"; } setInterval(updateClock, 1000); </script> </body> </html> ``` 这份代码实现了一个圆形的时钟时钟的样式可以通过 CSS 进行自定义。时钟的指针是通过 CSS 的 `transform: rotate()` 属性来控制旋转角度的,指针的角度是根据当前时间计算出来的。时钟上的刻度线是通过 CSS 的 `animation` 属性来实现的,用 `transform-origin` 控制旋转中心,用 `@keyframes` 定义动画。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值