JS + HTML 罗盘式时钟的实现


Posted in Javascript onMay 21, 2021

代码块:

<!DOCTYPE html>
   <html lang="zh-hans">
       <head>
           <meta charset="UTF-8">
           <meta name="viewport" content="width=device-width, initial-scale=1.0">
           <meta http-equiv="X-UA-Compatible" content="ie=edge">
           <title>js仿抖罗盘时钟</title>
           <style>
               *{
                   margin:0;
                   padding:0
              }
              html,body{
                   width:100%;
                   height:100%;
                   background-color:#000;
                   overflow:hidden
              }
              #clock{
                   position:relative;
                  width:100%;
                  height:100%;
                  background:#000
              }
              .label{
                   display:inline-block;
                   color:#4d4d4d;
                  text-align:center;
                  padding:0 5px;
                  font-size:19px;
                  transition:left 1s,top 1s;
                  transform-origin:0% 0%
             }
          </style>     
      </head>
      <body>
          <div id="clock"></div>
          <script>
              var monthText=["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"];
              var dayText=["零一号","零二号","零三号","零四号","零五号","零六号","零七号","零八号","零九号","十号","十一号","十二号","十三号","十四号","十五号","十六号","十七号","十八号","十九号","二十号","二十一号","二十二号","二十三号","二十四号","二十五号","二十六号","二十七号","二十八号","二十九号","三十号","三十一号"];
              var weekText=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
              var hourText=["零点","零一点","零两点","零三点","零四点","零五点","零六点","零七点","零八点","零九点","零十点","十一点","十二点","十三点","十四点","十五点","十六点","十七点","十八点","十九点","二十点","二十一点","二十二点","二十三点"];
              var minuteText=["零一分","零二分","零三分","零四分","零五分","零六分","零七分","零八分","零九分","零十分","十一分","十二分","十三分","十四分","十五分","十六分","十七分","十八分","十九分","二十分","二十一分","二十二分","二十三分","二十四分","二十五分","二十六分","二十七分","二十八分","二十九分","三十分","三十一分","三十二分","三十三分","三十四分","三十五分","三十六分","三十七分","三十八分","三十九分","四十分","四十一分","四十二分","四十三分","四十四分","四十五分","四十六分","四十七分","四十八分","四十九分","五十分","五十一分","五十二分","五十三分","五十四分","五十五分","五十六分","五十七分","五十八分","五十九分","六十分"];
              var secondsText=["零一秒","零二秒","零三秒","零四秒","零五秒","零六秒","零七秒","零八秒","零九秒","零十秒","十一秒","十二秒","十三秒","十四秒","十五秒","十六秒","十七秒","十八秒","十九秒","二十秒","二十一秒","二十二秒","二十三秒","二十四秒","二十五秒","二十六秒","二十七秒","二十八秒","二十九秒","三十秒","三十一秒","三十二秒","三十三秒","三十四秒","三十五秒","三十六秒","三十七秒","三十八秒","三十九秒","四十秒","四十一秒","四十二秒","四十三秒","四十四秒","四十五秒","四十六秒","四十七秒","四十八秒","四十九秒","五十秒","五十一秒","五十二秒","五十三秒","五十四秒","五十五秒","五十六秒","五十七秒","五十八秒","五十九秒","六十秒"];
              var clock;var monthList=[];
              var dayList=[];
              var weekList=[];
              var hourList=[];
              var minuteList=[];
              var secondsList=[];
              var isCircle=false;
              var textSet=[[monthText,monthList],[dayText,dayList],[weekText,weekList],[hourText,hourList],[minuteText,minuteList],[secondsText,secondsList]];
              window.onload=function(){
                  init();
                  setInterval(function(){
                       runTime();
                       },100);
                      changePosition();
                      setTimeout(function(){
                      changeCircle();
                      },2000);
                  }
              function init(){
                  clock=document.getElementById('clock');
                  for(var i=0;i<textSet.length;i++){
                     for(var j=0;j<textSet[i][0].length;j++){
                          var temp=createLabel(textSet[i][0][j]);
                         clock.appendChild(temp);textSet[i][1].push(temp);
                      }
                  }
              }
              function createLabel(text)
              {
               var div=document.createElement('div');
              div.classList.add('label');
              div.innerText=text;return div;
              }
              function runTime(){
                  var now=new Date();
                  var month=now.getMonth();
                  var day=now.getDate();
                  var week=now.getDay();
                  var hour=now.getHours();
                  var minute=now.getMinutes();
                  var seconds=now.getSeconds();
                  initStyle();
                  var nowValue=[month,day-1,week,hour,minute,seconds];
                 for(var i=0;i<nowValue.length;i++){
                     var num=nowValue[i];
                     textSet[i][1][num].style.color='#fff';
                  }
                  if(isCircle){
                     var widthMid=document.body.clientWidth/2;
                     var heightMid=document.body.clientHeight/2;
                     for(var i=0;i<textSet.length;i++){
                         for(var j=0;j<textSet[i][0].length;j++){
                             var r=(i+1)*35+50*i;
                             var deg=360/textSet[i][1].length*(j-nowValue[i]);
                             var x=r*Math.sin(deg*Math.PI/180)+widthMid;
                             var y=heightMid-r*Math.cos(deg*Math.PI/180);
                             var temp=textSet[i][1][j];
                             temp.style.transform='rotate('+(-90+deg)+'deg)';
                             temp.style.left=x+'px';
                             temp.style.top=y+'px';
                         }
                     }
                 }
             }
             function initStyle(){
                 var label=document.getElementsByClassName('label');
                 for(var i=0;i<label.length;i++){
                     label[i].style.color='#4d4d4d';
                 }
             }
             function changePosition(){
                 for(let i=;i<textSet.length;i++){
                     for(let j=0;j<textSet[i][1].length;j++){
                         let tempX=textSet[i][1][j].offsetLeft+"px";
                         let tempY=textSet[i][1][j].offsetTop+"px";
                         setTimeout(function(){
                             textSet[i][][j].style.position="absolute";
                             textSet[i][1][j].style.left=tempX;textSet[i][1][j].style.top=tempY;
                         },50);
                      }
                  }
             }
             function changeCircle(){
                 isCircle=true;
                 clock.style.transform="rotate(90deg)";
             }
         </script>
     </body>
 </html>

效果:

JS + HTML 罗盘式时钟的实现

到此这篇关于HTML 罗盘式时钟的实现的文章就介绍到这了,更多相关HTML 罗盘式时钟内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章,希望大家以后多多支持三水点靠木!

 
Javascript 相关文章推荐
jQuery 事件的命名空间简单了解
Nov 22 Javascript
jquery动态加载js/css文件方法(自写小函数)
Oct 11 Javascript
JQuery ztree带筛选、异步加载实例讲解
Feb 25 Javascript
简单的js计算器实现
Oct 26 Javascript
vue.js实现备忘录功能的方法
Jul 10 Javascript
JQuery用$.ajax或$.getJSON跨域获取JSON数据的实现代码
Sep 23 jQuery
Vue使用Proxy监听所有接口状态的方法实现
Jun 07 Javascript
javascript网页随机点名实现过程解析
Oct 15 Javascript
JS+CSS实现随机点名(实例代码)
Nov 04 Javascript
vue.config.js常用配置详解
Nov 14 Javascript
vue中data改变后让视图同步更新的方法
Mar 29 Vue.js
微信小程序实现录音Record功能
May 09 Javascript
JavaScript canvas实现流星特效
May 20 #Javascript
vue使用节流函数的踩坑实例指南
vue实现同时设置多个倒计时
May 20 #Vue.js
Vue和Flask通信的实现
JavaScript实现班级抽签小程序
May 19 #Javascript
JS实现扫雷项目总结
深入浅析React中diff算法
May 19 #Javascript
You might like
PHP 冒泡排序算法的实现代码
2010/08/08 PHP
Smarty foreach控制循环次数的实现详解
2013/07/03 PHP
php实现搜索类封装示例
2016/03/31 PHP
浅谈PHP发送HTTP请求的几种方式
2017/07/25 PHP
jQuery学习基础知识小结
2010/11/25 Javascript
js 代码优化点滴记录
2012/02/19 Javascript
使用JavaScript动态设置样式实现代码及演示动画
2013/01/25 Javascript
JavaScript字符串对象fromCharCode方法入门实例(用于把Unicode值转换为字符串)
2014/10/17 Javascript
理解jQuery stop()方法
2014/11/21 Javascript
AngularJS  $modal弹出框实例代码
2016/08/24 Javascript
jQuery实现淡入淡出的模态框
2017/02/09 Javascript
jquery中each循环的简单回滚操作
2017/05/05 jQuery
关于Mac下安装nodejs、npm和cnpm的教程
2018/04/11 NodeJs
vue.js做一个简单的编辑菜谱功能
2018/05/08 Javascript
vue实现自定义多选与单选的答题功能
2018/07/05 Javascript
jquery+css实现Tab栏切换的代码实例
2019/05/14 jQuery
js微信分享接口调用详解
2019/07/23 Javascript
JS实现电商商品展示放大镜特效
2020/01/07 Javascript
Python中的time模块与datetime模块用法总结
2016/06/30 Python
Python实现快速计算词频功能示例
2018/06/25 Python
Python设计模式之抽象工厂模式原理与用法详解
2019/01/15 Python
基于wxPython的GUI实现输入对话框(1)
2019/02/27 Python
python 判断三个数字中的最大值实例代码
2019/07/24 Python
python 两个数据库postgresql对比
2019/10/21 Python
django和flask哪个值得研究学习
2020/07/31 Python
详解如何用HTML5 Canvas API控制图片的缩放变换
2016/03/22 HTML / CSS
利用Node实现HTML5离线存储的方法
2020/10/16 HTML / CSS
巴黎卡诗美国官方网站:始于1964年的头发头皮护理专家
2017/07/10 全球购物
基督教卡片、励志礼品、家居装饰等:DaySpring
2018/10/12 全球购物
小米乌克兰网上商店:Xiaomi.UA
2019/10/29 全球购物
一百多行代码实现react拖拽hooks
2021/03/23 Javascript
学期自我评价
2014/01/27 职场文书
访谈节目策划方案
2014/05/15 职场文书
党风廉正建设个人工作总结
2015/03/06 职场文书
2016年乡镇综治宣传月活动总结
2016/03/16 职场文书
Nginx优化服务之网页压缩的实现方法
2021/03/31 Servers