HTML+CSS+JS制作一个黑灰色简约时钟

1. 效果图:


2. 特点:这次借鉴了网络上的代码,利用JS动态创建元素,减少html语句的数量,也便于与vue、react等语言进行结合。


3. 代码实现:

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>黑灰色简约时钟</title>
<style lang="css">
.clock {
position: absolute;
margin: auto;
width: 400px;
height: 400px;
padding: 0;
background-color: #262c33;
border: 18px solid #373f4a;
border-radius: 50%;
background-clip: border-box;
}
.point{
position: absolute;
width: 400px;
height: 400px;
top: 0;
}
.s,.m,.h{
position: absolute;
box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.4);
border-radius: 10px;
left: 50%;
bottom: 50%;
transform-origin: center bottom;
} .s{
height: 180px;
width: 4px;
background: red;
margin-left: -1px;
z-index: 110;
} .m{
height: 140px;
width: 5px;
background: #26a79d;
margin-left: -2.5px;
z-index: 100;
} .h{
height: 100px;
width: 8px;
background: #d4d5d6;
margin-left: -4px;
z-index: 90;
border-left: 1px solid rgba(255, 255, 255, 0.1);
box-sizing: border-box;
}
.clock-center{
width: 20px;
height: 20px;
margin: -10px 0 0 -10px;
border-radius: 50%;
background-color: #d4d5d6;
position: absolute;
top: 50%;
left: 50%;
z-index: 999;
}
.minute-marks {
display: inline-block;
padding: 0;
margin: 0;
list-style-type: none;
width: 0px;
height: 0px;
}
.minute-marks li {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
display: inline-block;
width: 200px;
height: 200px;
}
.minute-marks li:before,
.minute-marks li:after {
content: '';
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
width: 0px;
height: 0px;
display: inline-block;
border-color: #d4d5d6;
border-width: 4px;
border-style: solid;
border-radius: 4px;
background-clip: padding-box;
opacity: 1;
}
.minute-marks li:before {top: -380px;}
.minute-marks li:after {bottom: -380px;}
.minute-marks li.five:before,.minute-marks li.five:after {width: 0px;height: 20px;}
.minute-marks li.five:before {top: -360px;}
.minute-marks li.five:after {bottom: -360px;}
</style>
</head>
<body>
<div class='clock'>
<ul class='minute-marks'></ul>
<div class='point'>
<div class="m"></div><div class="h"></div><div class="s"></div>
<div class="clock-center"></div>
</div>
</div>
<script>
// autor:戈小戈
for (var i = 1; i <= 6; i++){
for(var m = 1;m<=5;m++){
var div=document.createElement('li');
if(m==1){div.className = 'five';}
div.style.transform = 'rotate('+(i*5-6+m)*6+'deg )';//设置每一分钟点所在位置
document.getElementsByClassName("minute-marks")[0].appendChild(div);//添加分钟点
} }
function setTime() {
const sHand = document.querySelector('.s');
const mHand = document.querySelector('.m');
const hHand = document.querySelector('.h'); const d = new Date();
const s = d.getSeconds();//秒
const m = d.getMinutes();//分
const h = d.getHours();//时 const sDeg = (( s / 60 ) * 360 );
const mDeg = (( m / 60 ) * 360 ) + (( s / 60 ) * 6 );
const hDeg = (( h / 12 ) * 360 ) + (( m / 60 ) * 30 ); sHand.style.transform = 'rotate('+sDeg+'deg )';
mHand.style.transform = 'rotate('+mDeg+'deg )';
hHand.style.transform = 'rotate('+hDeg+'deg )'; }
setInterval( setTime, 1000 );
</script>
</body>
</html>

最新文章

  1. Datatables JQuery插件
  2. mysql 简单优化方法
  3. Day Three(Beta)
  4. wifi 驱动移植范例
  5. 线上mysql内存持续增长直至内存溢出被killed分析(已解决)
  6. Java for LeetCode 027 Remove Element
  7. 利用Qt Assistant 定制帮助文档
  8. excel取消自动超链接的方法:还原和自动更正取消自动超链接
  9. 【给你几个使用Xamarin的理由】
  10. Inno setup卸载前退出进程、删除文件夹
  11. 解决sdk manager无法更新的问题
  12. HDU 2209 翻纸牌游戏
  13. Ant.SOA微服务框架开源
  14. .NET CORE 框架ABP的代码生成器(ABP Code Power Tools )使用说明文档
  15. 坑:JavaScript 中 操作符“==” 和“===” 的区别
  16. Python 头部 #!/usr/bin/python 和 #!/usr/bin/env 的区别
  17. parted分区详解 查看UUID两种方式 blkid 和 ls -l /dev/disk/by-uuid
  18. WordPress安装官方文档教程
  19. JQuery Rest客户端库
  20. hdu2036

热门文章

  1. 面向对象程序设计第三次blog
  2. 2.2 在resources目录下,新建applicationContext.xml
  3. Tomcat配置中的java.lang.IllegalStateException: No output folder问题
  4. SpringBoot容器化的多环境配置
  5. redis底层数据结构之压缩列表(ziplist)
  6. 一些JVM指令
  7. K8S详细教程
  8. JVM(一) --- 什么是JVM
  9. C# 变量和表达式
  10. Kubernetes--管理资源标签(标签)