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 表格分页等操作实现代码(pagedown,pageup)
Apr 11 Javascript
js 获取(接收)地址栏参数值的方法
Apr 01 Javascript
JS过滤url参数特殊字符的实现方法
Dec 24 Javascript
AngularJS入门教程之AngularJS指令
Apr 18 Javascript
JQuery 在文档中查找指定name的元素并移除的实现方法
May 19 Javascript
jQuery自定义图片上传插件实例代码
Apr 04 jQuery
vue 微信授权登录解决方案
Apr 10 Javascript
详解使用uni-app开发微信小程序之登录模块
May 09 Javascript
koa+jwt实现token验证与刷新功能
May 30 Javascript
vue中使用element组件时事件想要传递其他参数的问题
Sep 18 Javascript
node.js使用fs读取文件出错的解决方案
Oct 23 Javascript
js实现上下左右键盘控制div移动
Jan 16 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
Warning: session_destroy() : Trying to destroy uninitialized sessionq错误
2011/06/16 PHP
Yii2使用小技巧之通过 Composer 添加 FontAwesome 字体资源
2014/06/22 PHP
一键生成各种尺寸Icon的php脚本(实例)
2017/02/08 PHP
Add a Table to a Word Document
2007/06/15 Javascript
JS关键字变色实现思路及代码
2013/02/21 Javascript
JavaScript中字面量与函数的基本使用知识
2015/10/20 Javascript
JS组件Bootstrap Table表格行拖拽效果实现代码
2020/08/27 Javascript
基于BootStrap Metronic开发框架经验小结【五】Bootstrap File Input文件上传插件的用法详解
2016/05/12 Javascript
easyui combotree加载静态数据问题(选不上)解决方法
2016/12/26 Javascript
jQuery EasyUI Accordion可伸缩面板组件使用详解
2017/02/28 Javascript
JS表单提交验证、input(type=number) 去三角 刷新验证码
2017/06/21 Javascript
vue数据控制视图源码解析
2018/03/28 Javascript
微信小程序实现联动选择器
2019/02/15 Javascript
axios封装,使用拦截器统一处理接口,超详细的教程(推荐)
2019/05/02 Javascript
vue 插件的方法代码详解
2019/06/06 Javascript
Layui之table中的radio在切换分页时无法记住选中状态的解决方法
2019/09/02 Javascript
基于javascript实现日历功能原理及代码实例
2020/05/07 Javascript
Bootstrap告警框(alert)实现弹出效果和短暂显示后上浮消失的示例代码
2020/08/27 Javascript
vant picker+popup 自定义三级联动案例
2020/11/04 Javascript
JavaScript实现网页tab栏效果制作
2020/11/20 Javascript
Mac下Anaconda的安装和使用教程
2018/11/29 Python
python2.7的flask框架之引用js&amp;css等静态文件的实现方法
2019/08/22 Python
使用SQLAlchemy操作数据库表过程解析
2020/06/10 Python
使用keras时input_shape的维度表示问题说明
2020/06/29 Python
CSS3轻松实现圆角效果
2017/11/09 HTML / CSS
使用css3实现的tab选项卡代码分享
2014/12/09 HTML / CSS
HTML5 文件域+FileReader 分段读取文件并上传到服务器
2017/10/23 HTML / CSS
英国在线滑雪板和冲浪商店:The Board Basement
2020/01/11 全球购物
好的自荐信的要求
2013/10/30 职场文书
2014年煤矿安全工作总结
2014/12/04 职场文书
教师个人总结范文
2015/02/11 职场文书
怎样写辞职信
2015/02/27 职场文书
【HBU】数据库第四周 单表查询
2021/04/05 SQL Server
OpenCV-Python使用cv2实现傅里叶变换
2021/06/09 Python
mysql如何配置白名单访问
2021/06/30 MySQL
python基础之类属性和实例属性
2021/10/24 Python