返回列表 发布新帖
其它

回复:HTML实现罗盘时钟 全部代码

10 0

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册 |

×


  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>www.dupengfei.top</title>
  6.     <style>
  7.         html{
  8.             background: #000;
  9.             color: #666;
  10.             font-size: 12px;
  11.             overflow:hidden;
  12.         }
  13.         *{
  14.             margin: 0;
  15.             padding: 0;
  16.         }
  17.         span{
  18.             display: block;
  19.             float: left;
  20.         }
  21.         .on{
  22.             color: #fff;
  23.         }
  24.         .wrapper{
  25.             width: 200px;
  26.             height: 200px;
  27.             position: absolute;
  28.             left:50%;
  29.             top:50%;
  30.             margin-top: -100px;
  31.             margin-left: -100px;
  32.         }
  33.         .wrapper .timebox{
  34.             position: absolute;
  35.             width: 200px;
  36.             height: 200px;
  37.             top: 0;
  38.             left:0;
  39.             border-radius: 100%;
  40.             transition: all 0.5s;
  41.         }
  42.         .timebox span{
  43.             transition: all 0.5s;
  44.             float: left;
  45.         }
  46.         .wrapper  .timebox span{
  47.             position: absolute;
  48.             left:50%;
  49.             top:50%;
  50.             width: 40px;
  51.             height: 18px;
  52.             margin-top: -9px;
  53.             margin-left: -20px;
  54.             text-align: right;
  55.         }

  56.     </style>
  57. </head>
  58. <body>

  59. <div id="wrapper">
  60.     <div class="timebox yuebox" id="yueBox"></div>
  61.     <div class="timebox riqiBox" id="riqiBox"></div>
  62.     <div class="timebox hourbox" id="hourbox"></div>
  63.     <div class="timebox minutebox" id="minutebox"></div>
  64.     <div class="timebox secondbox" id="secondbox"></div>
  65. </div>


  66. <script>

  67.     let wrapper = document.getElementById("wrapper");
  68.     let yueBox = document.getElementById("yueBox");
  69.     let riqiBox = document.getElementById("riqiBox");
  70.     let hourbox = document.getElementById("hourbox");
  71.     let minutebox = document.getElementById("minutebox");
  72.     let secondbox = document.getElementById("secondbox");

  73.     /*
  74.     * 找所有的东西标签函数
  75.     * */
  76.     let findSiblings = function( tag ){
  77.         let parent = tag.parentNode;
  78.         let childs = parent.children;
  79.         let sb = [];
  80.         for(let i=0 ; i <= childs.length-1 ; i++){
  81.             if( childs[i]!==tag){
  82.                 sb[sb.length] = childs[i];
  83.             }
  84.         }
  85.         return  sb ;
  86.     };

  87.     /*
  88.     * 去掉所有兄弟的类
  89.     * */
  90.     let removeSiblingClass =function( tag ){
  91.         let sb = findSiblings( tag );
  92.         for(let i=0 ;  i <= sb.length-1 ; i++){
  93.             sb[i].className = "";
  94.         }
  95.     };

  96.     /*
  97.     * 初始化月份函数
  98.     * */
  99.     let initMonth = function(){
  100.         for(let i=1; i<=12; i++){
  101.             let span = document.createElement("span");
  102.             span.innerHTML = i+"月";
  103.             yueBox.appendChild( span );
  104.         }
  105.     };

  106.     // 初始化日期
  107.     let initDate = function(){
  108.         for(let i=1; i<=31; i++){
  109.             let span = document.createElement("span");
  110.             span.innerHTML = i+"日";
  111.             riqiBox.appendChild( span );
  112.         }
  113.     };

  114.     // 初始化小时,分钟,秒
  115.     let initHour = function(){
  116.         for(let i=0; i<=23; i++){
  117.             let h = i ;
  118.             let span = document.createElement("span");
  119.             if( h<10){
  120.                 h="0"+h;
  121.             }
  122.             span.innerHTML = h +"点";
  123.             hourbox.appendChild( span );
  124.         }
  125.     };
  126.     let initMinute = function(){
  127.         for(let i=0; i<=59; i++){
  128.             let  f = i ;
  129.             let span = document.createElement("span");
  130.             if( f<10){
  131.                 f="0"+f;
  132.             }
  133.             span.innerHTML = f +"分";
  134.             minutebox.appendChild( span );
  135.         }
  136.     };
  137.     let initSecond = function(){
  138.         for(let i=0; i<=59; i++){
  139.             let  miao = i ;
  140.             let span = document.createElement("span");
  141.             if( miao<10){
  142.                 miao="0"+miao;
  143.             }
  144.             span.innerHTML = miao +"秒";
  145.             secondbox.appendChild( span );
  146.         }
  147.     };

  148.     // 时间文字样式切换函数
  149.     let changeTime = function(tag){
  150.         tag.className = "on";
  151.         removeSiblingClass( tag );
  152.     };

  153.     /*
  154.     * 初始化日历函数
  155.     * */
  156.     let initRili = function(){
  157.         initMonth(); // 初始化月份
  158.         initDate(); // 初始化日期
  159.         initHour(); // 小时
  160.         initMinute();
  161.         initSecond();
  162.     };

  163.     /*
  164.     * 展示当前时间
  165.     * 参数:mydate 时间对象
  166.     * */
  167.     let  showNow = function( mydate ){

  168.         let yue = mydate.getMonth() ;
  169.         let riqi = mydate.getDate();
  170.         let hour = mydate.getHours()  ;
  171.         let minute = mydate.getMinutes();
  172.         let second = mydate.getSeconds();
  173.         // 时间文字样式切换函数
  174.         changeTime( yueBox.children[yue] );
  175.         changeTime( riqiBox.children[riqi-1] );
  176.         changeTime( hourbox.children[hour] );
  177.         changeTime( minutebox.children[minute] );
  178.         changeTime( secondbox.children[second] );

  179.     };

  180.     // 展示时间圆圈函数
  181.     // tag:目标
  182.     // num:数字数量
  183.     // dis:圆圈半径
  184.     let textRound = function(tag,num,dis){
  185.         let span = tag.children ;
  186.         for(let i=0 ; i<=span.length-1; i++){
  187.             span[i].style.transform="rotate("+ (360/span.length)*i+"deg)  translateX("+dis+"px)" ;
  188.         }
  189.     };
  190.     /*
  191.     * 旋转指定“圆圈”指定度数
  192.     * */
  193.     let rotateTag = function(tag , deg){
  194.         tag.style.transform = "rotate("+deg+"deg)";
  195.     };

  196.     let main = function(){
  197.         initRili(); // 初始化日历

  198.         setInterval(function(){
  199.             let mydate = new Date();
  200.             showNow( mydate ); // 展示当前时间
  201.         },1000);

  202.         //  n秒后,摆出圆形
  203.         setTimeout(function(){
  204.             wrapper.className = "wrapper";
  205.             textRound(yueBox,12,40);
  206.             textRound(riqiBox,31,80);
  207.             textRound(hourbox,24,120);
  208.             textRound(minutebox,60,160);
  209.             textRound(secondbox,60,200);
  210.             setInterval(function(){
  211.                 let mydate = new Date();
  212.                 rotateTag( yueBox , -30*mydate.getMonth());
  213.                 rotateTag( riqiBox , -360/31*(mydate.getDate()-1) );
  214.                 rotateTag( hourbox , -360/24*mydate.getHours());
  215.                 rotateTag( minutebox , -6*mydate.getMinutes());
  216.                 rotateTag( secondbox , -6*mydate.getSeconds());
  217.             },1000)
  218.         },6000)

  219.     };
  220.     main();

  221. </script>

  222. </body>
  223. </html>
复制代码
抱歉,上次代码没有发全,请大家原谅
车研会员,开心每一天!

回复

您需要登录后才可以回帖 登录 | 立即注册 |

本版积分规则

头条资讯换一批
扫一扫访问小程序
获取最新资讯
关灯 在本版发帖
扫一扫添加客服微信
返回顶部
快速回复 返回顶部 返回列表