JavaScript实现抖音罗盘时钟


Posted in Javascript onOctober 11, 2019

本文实例为大家分享了JavaScript实现抖音罗盘时钟的具体代码,供大家参考,具体内容如下

其实很早以前就想发了,很多东西其实并不难,只要多动手动脑就行了,下面直接上代码,其他的自己下去慢慢研究吧!

HTML部分代码:

<!DOCTYPE html>
<html lang="en">
<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>

<link rel="stylesheet" href="css/demo.css" >

</head>
<body>

<div id="clock"></div>

<script src="js/demo.js"></script>

</body>
</html>

css部分代码:

*{
 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%
}

JavaScript部分代码:

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=0;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][1][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)";
}

更多JavaScript时钟特效点击查看:JavaScript时钟特效专题

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
优化网页之快速的呈现我们的网页
Jun 29 Javascript
JS验证邮箱格式是否正确的代码
Dec 05 Javascript
Eclipse下jQuery文件报错出现错误提示红叉
Jan 13 Javascript
JS与jQuery遍历Table所有单元格内容的方法
Dec 07 Javascript
jquery实现列表上下移动功能
Feb 25 Javascript
Bootstrap select多选下拉框实现代码
Dec 23 Javascript
EasyUI学习之Combobox级联下拉列表(2)
Dec 29 Javascript
详解Angular2响应式表单
Jun 14 Javascript
vue 自定义 select内置组件
Apr 10 Javascript
vue动态设置img的src路径实例
Sep 18 Javascript
vue router导航守卫(router.beforeEach())的使用详解
Apr 19 Javascript
vue中datepicker的使用教程实例代码详解
Jul 08 Javascript
javascript刷新父页面方法汇总详解
Oct 10 #Javascript
element form 校验数组每一项实例代码
Oct 10 #Javascript
使用Vue-cli3.0创建的项目 如何发布npm包
Oct 10 #Javascript
js判断复选框是否选中的方法示例【基于jQuery】
Oct 10 #jQuery
VUE注册全局组件和局部组件过程解析
Oct 10 #Javascript
jquery获取并修改触发事件的DOM元素示例【基于target 属性】
Oct 10 #jQuery
electron-vue开发环境内存泄漏问题汇总
Oct 10 #Javascript
You might like
如何在PHP中使用Oracle数据库(2)
2006/10/09 PHP
Codeigniter中禁止A Database Error Occurred错误提示的方法
2014/06/12 PHP
mysqli扩展无法在PHP7下升级问题的解决
2019/09/10 PHP
php策略模式简单示例分析【区别于工厂模式】
2019/09/25 PHP
laravel 字段格式化 modle 字段类型转换方法
2019/09/30 PHP
PHP7移除的扩展和SAPI
2021/03/09 PHP
Firebug 字幕文件JSON地址获取代码
2009/10/28 Javascript
javascript oop开发滑动(slide)菜单控件
2010/08/25 Javascript
JS对img进行操作(换图片/切图/轮换/停止)
2013/04/17 Javascript
ie8 不支持new Date(2012-11-10)问题的解决方法
2013/07/31 Javascript
JS关闭窗口或JS关闭页面的几种代码分享
2013/10/25 Javascript
jQuery对下拉框,单选框,多选框的操作
2014/02/21 Javascript
JavaScript通过正则表达式实现表单验证电话号码
2014/03/07 Javascript
js原生之焦点图转换加定时器实例
2016/12/12 Javascript
Nodejs核心模块之net和http的使用详解
2019/04/02 NodeJs
js继承的这6种方式!(上)
2019/04/23 Javascript
详解微信小程序获取当前时间及日期的方法
2019/04/28 Javascript
Electron实现应用打包、自动升级过程解析
2020/07/07 Javascript
Vue实现图书管理小案例
2020/12/03 Vue.js
Python求两个文本文件以行为单位的交集、并集与差集的方法
2015/06/17 Python
在Python的Django框架中使用通用视图的方法
2015/07/21 Python
对python sklearn one-hot编码详解
2018/07/10 Python
Python Process多进程实现过程
2019/10/22 Python
在django admin中配置搜索域是一个外键时的处理方法
2020/05/20 Python
Python修改DBF文件指定列
2020/12/19 Python
HTML5实现QQ聊天气泡效果
2017/06/26 HTML / CSS
方太官方网上商城:销售方太抽油烟机、燃气灶、消毒柜等
2017/01/17 全球购物
Gerry Weber德国官网:优质女性时装,德国最大的时装公司之一
2019/11/02 全球购物
县政府班子个人对照检查材料
2014/10/05 职场文书
战略性融资合作协议书范本
2014/10/17 职场文书
向雷锋同志学习倡议书
2015/04/27 职场文书
小学体育组工作总结
2015/08/13 职场文书
初中生入团申请书范文(五篇)
2019/10/16 职场文书
高一作文之暖冬
2019/11/09 职场文书
三星 3nm 芯片将于第二季度开始量产
2022/04/29 数码科技
python 使用pandas读取csv文件的方法
2022/12/24 Python