HTML时钟源码


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>动态时钟</title>
</head>
<style>
    body,div,p{ font-family: 'Microsoft Yahei' ;font-size: 14px;}
    .box{ width: 400px; height: 400px; border:10px solid #8bf2f1;margin:100px auto; border-radius: 50%;
        box-shadow: 0px 0px 20px 3px #444 inset; position: relative;}
    /*原点*/
    .origin{ width: 20px; height: 20px; border-radius: 50%; background: #ff0000; top:190px; left: 190px; position: absolute;}
    /* 指针 */
    .clock_line{ position: absolute;position:absolute;z-index:20;}
    .hour_line{width:100px;height:4px;top:198px;left:200px;background-color:#000;border-radius:2px;
        transform-origin:0 50%;box-shadow:1px -3px 8px 3px #aaa;}
    .minute_line{width:130px;height:2px;top:199px;left:190px;background-color:#000;
        transform-origin:7.692% 50%;box-shadow:1px -3px 8px 1px #aaa;}
    .second_line{width:170px;height:1px;top:199.5px;left:180px;background-color:#f60;
        transform-origin:11.765% 50%;box-shadow:1px -3px 7px 1px #bbb;}


    .dot_box{width: inherit; height: inherit;}
    /*时钟数*/
    .dot{ width: 40px; height: 40px; line-height: 40px; text-align: center; font-size: 22px; position: absolute;}
    .clock-scale{width:195px;height:2px;transform-origin:0% 50%;z-index:7;
      position:absolute;top:199px;left:200px;}
    .scale-show{ width:12px;height:2px;background-color:#555;float:left;}
    .scale-hidden{width:183px;height:2px;float:left;}
    /*日期*/
    .date_info{width:160px;height:28px; 
        line-height:28px;text-align:center;position:absolute;top:230px;left:120px;z-index:11;color:#555;
        font-weight:bold;}
    .time_info{ width: 110px; height: 35px; line-height: 35px;text-align:center;position:absolute;top:270px;left:150px;z-index:11;color:#555; background: #253e3e; }
    .time{ width: 35px ;height:35px; float: left; color: #fff; font-size: 22px;}
     #minute_time{border-left:1px solid #fff;border-right:1px solid #fff;}
</style>
<body>
<div class="box" id="clock">
    <!-- 原点 -->
    <div class="origin"></div>
    <!-- 时钟数 -->
    <div class="dot_box">
        <div class="dot">6</div>
        <div class="dot">5</div>
        <div class="dot">4</div>
        <div class="dot">3</div>
        <div class="dot">2</div>
        <div class="dot">1</div>
        <div class="dot">12</div>
        <div class="dot">11</div>
        <div class="dot">10</div>
        <div class="dot">9</div>
        <div class="dot">8</div>
        <div class="dot">7</div>
    </div>
    <!-- 时、分、秒针 -->
    <div class="clock_line hour_line" id="hour_line"></div>
    <div class="clock_line minute_line" id="minute_line"></div>
    <div class="clock_line second_line" id="second_line"></div>
    <!-- 日期 -->
    <div class="date_info" id="date_info"></div>
    <!-- 时间 -->
    <div class="time_info" >
        <div class="time" id="hour_time"></div>
        <div class="time" id="minute_time"></div>
        <div class="time" id="second_time"></div>
    </div>
</div>
</body>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
<script>
$(function(){
    var clock = document.getElementById("clock");
    function initNumXY(){
        // 1、12个数字的位置设置
        var radius = 160;//大圆半价
        var dot_num = 360/$(".dot").length;//每个div对应的弧度数
        //每一个dot对应的弧度;
        var ahd = dot_num*Math.PI/180;
        $(".dot").each(function(index, el) {
            $(this).css({
                "left":180+Math.sin((ahd*index))*radius,
                "top":180+Math.cos((ahd*index))*radius
            });
        });
        // 2、刻钟设置
        for(var i = 0; i < 60; i++) {
            clock.innerHTML += "<div class='clock-scale'> " + 
                   "<div class='scale-hidden'></div>" + 
                   "<div class='scale-show'></div>" + 
                  "</div>";
        }
        var scale = document.getElementsByClassName("clock-scale");
            for(var i = 0; i < scale.length; i++) {
                scale[i].style.transform="rotate(" + (i * 6 - 90) + "deg)";
        }
    }
    initNumXY();//调用上面的函数
    //获取时钟id
    var hour_line = document.getElementById("hour_line"),
        minute_line = document.getElementById("minute_line"),
        second_line = document.getElementById("second_line"),
        date_info=document.getElementById("date_info"),//获取date_info
        hour_time = document.getElementById("hour_time"),// 获去时间id
        minute_time = document.getElementById("minute_time"),
        second_time = document.getElementById("second_time");
    //3、设置动态时间
    function setTime(){
        var nowdate = new Date();
        //获取年月日时分秒
        var year = nowdate.getFullYear(),
            month = nowdate.getMonth()+1,
            day = nowdate.getDay(),
            hours = nowdate.getHours(),
            minutes = nowdate.getMinutes(),
            seconds = nowdate.getSeconds(),
            date = nowdate.getDate();
        var weekday =["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
        // 获取日期id
        date_info.innerHTML=year+"年"+month+"月"+date+"日   "+weekday[day];
        hour_time.innerHTML = hours >=10 ? hours : "0"+hours;
        minute_time.innerHTML = minutes >=10 ? minutes : "0"+minutes;
        second_time.innerHTML = seconds >=10 ? seconds : "0"+seconds;
        console.log(year+"年"+month+"月"+day+"日   "+weekday[day]);
        //时分秒针设置
        var hour_rotate = (hours*30-90) + (Math.floor(minutes / 12) * 6);
        hour_line.style.transform = 'rotate(' + hour_rotate + 'deg)';
        minute_line.style.transform = 'rotate(' + (minutes*6 - 90) + 'deg)';
        second_line.style.transform = 'rotate(' + (seconds*6 - 90)+'deg)';
    }
    // setTime();
    setInterval(setTime, 1000);
    
    
});
</script>
</html>

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HTML5网页动态时钟源码可以使用JavaScriptCSS来实现。下面是一个简单的示例: HTML代码: ```html <!DOCTYPE html> <html> <head> <title>动态时钟</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div class="clock"> <div id="hour" class="hand"></div> <div id="minute" class="hand"></div> <div id="second" class="hand"></div> </div> <script src="script.js"></script> </body> </html> ``` CSS代码(style.css): ```css .clock { width: 200px; height: 200px; background-color: #f1f1f1; border-radius: 50%; position: relative; margin: 0 auto; margin-top: 100px; overflow: hidden; } .hand { background-color: #333; position: absolute; left: 50%; bottom: 50%; transform-origin: bottom center; } #hour { width: 8px; height: 60px; margin-left: -4px; } #minute { width: 4px; height: 80px; margin-left: -2px; } #second { width: 2px; height: 100px; margin-left: -1px; } ``` JavaScript代码(script.js): ```javascript function rotateClockHands() { var now = new Date(); var hourAngle = now.getHours() % 12 * 30 + now.getMinutes() / 2; var minuteAngle = now.getMinutes() * 6; var secondAngle = now.getSeconds() * 6; document.getElementById('hour').style.transform = 'rotate(' + hourAngle + 'deg)'; document.getElementById('minute').style.transform = 'rotate(' + minuteAngle + 'deg)'; document.getElementById('second').style.transform = 'rotate(' + secondAngle + 'deg)'; } setInterval(rotateClockHands, 1000); ``` 这段代码创建一个200x200像素的圆形时钟,并用JavaScript动态旋转时、分、秒指针。CSS用于设置时钟的样式,JavaScript负责计算当前时间并将指针旋转到正确的位置。setInterval()函数用于每秒钟更新一次指针的旋转角度,使时钟指针动起来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值