html+css+JavaScript实现简洁的圆形时钟数字时钟+指针时钟

简介: 使用前端三件套实现一个实时跟新的时钟,其中包括电子时钟和刻度时钟

 ⏰题目要求

image.gif

⏰html代码

<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>简洁的圆形时钟js代码</title><linkrel="stylesheet"href="css/style.css"type="text/css"/></head><body><divclass="container"><divclass="clock"><divid="date"class="date box"></div><divclass="clock-face"><divclass="marker twelve"></div><divclass="marker three"></div><divclass="marker six"></div><divclass="marker nine"></div><divid="minute-hand"class="hand minute-hand"></div><divid="hour-hand"class="hand hour-hand"></div><divid="second-hand"class="hand second-hand"></div><divid="centre"class="centre"></div><divid="digital-time"class="digital-time box"></div></div></div></div><scriptsrc="js/script.js"></script></body></html>
image.gif

⏰css代码

* {
transition: color.4s, background.4s, border.4s;
transition-timing-function: ease-in;
}
body {
background: #F9FBE7;
}
body.dark {
background: #223;
}
.container {
display: flex;
height: 100vh;
}
.clock {
position: relative;
margin: auto;
width: 220px;
height: 260px;
}
.box {
color: #7986CB;
background: #EDE7F6;
font-size: 20px;
text-align: center;
font-family: Lato, sans-serif;
border: 2pxsolid#D1C4E9;
border-radius: 4px;
}
.dark.box {
color: #90CAF9;
background: #4527A0;
border: 2pxsolid#673AB7;
}
.date {
width: 160px;
padding: 5px8px;
position: absolute;
top: -65px;
left: 15px;
}
.clock-face {
position: absolute;
height: 220px;
width: 220px;
background: #EDE7F6;
border-radius: 50%;
border: 4pxsolid#D1C4E9;
}
.dark.clock-face {
background: #4527A0;
border: 4pxsolid#673AB7;
}
.marker {
position: absolute;
width: 10px;
height: 10px;
background: #7986CB;
border-radius: 50%;
}
.dark.marker {
background: #90CAF9;
}
.twelve {
left: 105px;
top: 8px;
}
.three {
right: 8px;
top: 105px;
}
.six {
left: 105px;
bottom: 8px;
}
.nine {
left: 8px;
top: 105px;
}
.hand {
position: absolute;
left: 110px;
transform-origin: 0%;
border-radius: 050%50%0;
}
.hour-hand {
height: 8px;
width: 62px;
top: 107px;
background: #F06292;
}
.dark.hour-hand {
background: #E91E63;
}
.minute-hand {
height: 6px;
width: 88px;
top: 108px; 
background: #FF8A65;
}
.dark.minute-hand {
background: #FF9800;
}
.second-hand {
height: 3px;
width: 98px;
top: 109px; 
background: #777;
}
.dark.second-hand {
background: #eee}
.centre {
position: absolute;
width: 16px;
height: 16px;
background: #777;
border-radius: 50%;
top: 102px;
left: 102px;
}
.dark.centre {
background: #eee}
.digital-time {
width: 90px;
padding: 5px8px;
position: absolute;
top: 250px;
left: 55px;
}
.dark.digital-time {
color: #90CAF9;
background: #4527A0;
border: 2pxsolid#673AB7;
}
.switch-label {
color: #7986CB;
padding-right: 6px;
line-height: 1.6em;
font-family: Lato, sans-serif;
font-size: 0.9em;
}
.dark.switch-label {
color: #90CAF9;
}
/* The switch - the box around the slider */.switch {
position: relative;
display: inline-block;
width: 44px;
height: 24px;
}
/* Hide default HTML checkbox */.switchinput {
opacity: 0;
width: 0;
height: 0;
}
/* The slider */.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #aaa;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 18px;
width: 18px;
left: 3px;
bottom: 3px;
background-color: #fff;
transition: .4s;
}
input:checked + .slider {
background-color: #90CAF9;
}
input:focus + .slider {
box-shadow: 001px#2196F3;
}
input:checked + .slider:before {
transform: translateX(20px);
}
/* Rounded sliders */.slider.round {
border-radius: 8px;
}
.slider.round:before {
border-radius: 5px;
}
image.gif

⏰js代码

//(1)声明一个7个长度的数组daysvardays=[];
days[0]='星期日';
days[1]='星期一';
days[2]='星期二';
days[3]='星期三';
days[4]='星期四';
days[5]='星期五';
//(2)往数组days后面添加一个元素,值为星期六;days[6]='星期六';
vardateDisplay=document.getElementById('date');
varhourHand=document.getElementById('hour-hand');
varminuteHand=document.getElementById('minute-hand');
varsecondHand=document.getElementById('second-hand');
vardigitalTime=document.getElementById('digital-time');
varclock=function() {  
//(3)获取当前时间vartimeNow=newDate(); 
varday=timeNow.getDay();
vardate=timeNow.getDate();
varmonth=timeNow.getMonth();
dateDisplay.innerHTML=days[day];
//(4)获取当前时间的秒varseconds=timeNow.getSeconds() ;
varsRot=seconds*6-90;
//(5)获取当前时间的分钟varminutes=timeNow.getMinutes();
varmRot= (minutes*6) + (seconds/10) -90;
//(6)获取当前时间的小时varhours=timeNow.getHours();
varhRot= (hours%12*30) + (minutes/2) -90;
hourHand.style.transform="rotate("+hRot+"deg)";
minuteHand.style.transform="rotate("+mRot+"deg)";
secondHand.style.transform="rotate("+sRot+"deg)";
//(7)给digitalTime的内容赋值digitalTime.innerHTML=format(hours)+":"+format(minutes)+":"+format(seconds);
}
functionformat(num) {
//(8)三元运算符,如果小于10则在num前面加个0,否则返回numreturnnum=num<10?'0'+num:num;
}
(functionrun() {
//(9)调用clock方法clock();
//(10)(11)(12)定义定时器1秒后执行调用自己setTimeout(function(){ run();}, 1000);
})();
image.gif

⏰代码分析

本题的考核是js代码的补充,其中考核重点是日期对象。

首先js中的日期对象需要使用new Date()实例化对象才能使用

Date对象的常用get方法image.gif

时钟需要获取到小时、分钟、秒

所以需要用到getHours()、getMinutes()、getSeconds()来获取

❗PS:

虽然指针时钟不是本题的考核,但也是一个重点

指针的移动是通过transform属性来设置旋转角度

image.gif

其次就是指针移动的算法

秒:var sRot = seconds * 6 - 90;

分:var mRot = (minutes * 6) + (seconds / 10) - 90;

时:var hRot = (hours % 12 * 30) + (minutes / 2) - 90;

最后计时器不能忘记设置和调用

1000毫秒=1秒

image.gif

⏰实现效果

image.gif

相关文章
|
11月前
|
存储 前端开发 JavaScript
仿真银行app下载安装, 银行卡虚拟余额制作app,用html+css+js实现逼真娱乐工具
这是一个简单的银行账户模拟器项目,用于学习前端开发基础。用户可进行存款、取款操作,所有数据存储于浏览器内存中
|
人工智能 程序员 UED
【01】完成新年倒计时页面-蛇年新年快乐倒计时领取礼物放烟花html代码优雅草科技央千澈写采用html5+div+CSS+JavaScript-优雅草卓伊凡-做一条关于新年的代码分享给你们-为了C站的分拼一下子
【01】完成新年倒计时页面-蛇年新年快乐倒计时领取礼物放烟花html代码优雅草科技央千澈写采用html5+div+CSS+JavaScript-优雅草卓伊凡-做一条关于新年的代码分享给你们-为了C站的分拼一下子
840 21
【01】完成新年倒计时页面-蛇年新年快乐倒计时领取礼物放烟花html代码优雅草科技央千澈写采用html5+div+CSS+JavaScript-优雅草卓伊凡-做一条关于新年的代码分享给你们-为了C站的分拼一下子
|
前端开发 JavaScript
【02】v1.0.1更新增加倒计时完成后的放烟花页面-优化播放器-优化结构目录-蛇年新年快乐倒计时领取礼物放烟花html代码优雅草科技央千澈写采用html5+div+CSS+JavaScript-优雅草卓伊凡-做一条关于新年的代码分享给你们-为了C站的分拼一下子
【02】v1.0.1更新增加倒计时完成后的放烟花页面-优化播放器-优化结构目录-蛇年新年快乐倒计时领取礼物放烟花html代码优雅草科技央千澈写采用html5+div+CSS+JavaScript-优雅草卓伊凡-做一条关于新年的代码分享给你们-为了C站的分拼一下子
702 14
【02】v1.0.1更新增加倒计时完成后的放烟花页面-优化播放器-优化结构目录-蛇年新年快乐倒计时领取礼物放烟花html代码优雅草科技央千澈写采用html5+div+CSS+JavaScript-优雅草卓伊凡-做一条关于新年的代码分享给你们-为了C站的分拼一下子
|
前端开发
【2025优雅草开源计划进行中01】-针对web前端开发初学者使用-优雅草科技官网-纯静态页面html+css+JavaScript可直接下载使用-开源-首页为优雅草吴银满工程师原创-优雅草卓伊凡发布
【2025优雅草开源计划进行中01】-针对web前端开发初学者使用-优雅草科技官网-纯静态页面html+css+JavaScript可直接下载使用-开源-首页为优雅草吴银满工程师原创-优雅草卓伊凡发布
1169 1
【2025优雅草开源计划进行中01】-针对web前端开发初学者使用-优雅草科技官网-纯静态页面html+css+JavaScript可直接下载使用-开源-首页为优雅草吴银满工程师原创-优雅草卓伊凡发布
css3 svg制作404页面动画效果HTML源码
css3 svg制作404页面动画效果HTML源码
297 34
html+js+css实现的建筑方块立体数字时钟源码
html+js+css实现的建筑方块立体数字时钟源码
640 33
|
JSON 前端开发 JavaScript
使用html,css,js 实现一个龙年春节祝福卡片效果
使用html,css,js 实现一个龙年春节祝福卡片效果
599 123
|
前端开发 JavaScript
用HTML CSS JS打造企业级官网 —— 源码直接可用
必看!用HTML+CSS+JS打造企业级官网-源码直接可用,文章代码仅用于学习,禁止用于商业
643 1
|
前端开发 JavaScript 安全
HTML+CSS+JS密码灯登录表单
通过结合使用HTML、CSS和JavaScript,我们创建了一个带有密码强度指示器的登录表单。这不仅提高了用户体验,还帮助用户创建更安全的密码。希望本文的详细介绍和代码示例能帮助您在实际项目中实现类似功能,提升网站的安全性和用户友好性。
376 3
|
JSON 移动开发 数据格式
html5+css3+js移动端带歌词音乐播放器代码
音乐播放器特效是一款html5+css3+js制作的手机移动端音乐播放器代码,带歌词显示。包括支持单曲循环,歌词显示,歌曲搜索,音量控制,列表循环等功能。利用json获取音乐歌单和歌词,基于html5 audio属性手机音乐播放器代码。
540 6