JS代码+CSS样式实现一个动态时钟效果
Admin 2021-04-13 群英技术资讯 1448 次浏览
时钟是我们查看时间的一种工具,很多人家家里都会摆放一个时钟,那么我们用代码能实现一个时钟效果吗?答案是可以用的,这篇文章主要教大家用JS代码配合CSS样式来实现一个动态的时钟效果,效果如下图所示,下面我们一起来学习下。

知识点总结:
document.querySelector()方法返回文档中匹配指定 CSS 选择器的一个元素。
setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。
setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭。由 setInterval() 返回的 ID 值可用作 clearInterval() 方法的参数。
HTML+js部分
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="shiying.css" >
<title>Document</title>
</head>
<body>
<div class="clock">
<div class="hour">
<div class="hr" id="hr"></div>
</div>
<div class="min">
<div class="mn" id="mn"></div>
</div>
<div class="sec">
<div class="sc" id="sc"></div>
</div>
</div>
<script type="text/javascript">
const deg = 6;
const hr = document.querySelector('#hr');
const mn = document.querySelector('#mn');
const sc = document.querySelector('#sc');
setInterval(() => {
let day = new Date();
let hh = day.getHours() * 30;
let mm = day.getMinutes() * deg;
let ss = day.getSeconds() * deg;
hr.style.transform = `rotateZ(${(hh) + (mm / 12)}deg)`;
mn.style.transform = `rotateZ(${mm}deg)`;
sc.style.transform = `rotateZ(${ss}deg)`;
})
</script>
</body>
</html>
CSS部分
*{
margin:0;
padding:0;
box-sizing: border-box;
}
body{
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #091921;
}
.clock{
width: 350px;
height: 350px;
display:flex;
justify-content: center;
align-items: center;
background: url(1613462007944.png);
background-size: cover;
border:4px solid #091921;
border-radius: 50%;
box-shadow: 0 -15px 15px rgba(255,255,255,0.05),
inset 0 -15px 15px rgba(255,255,255, 0.05),
0 -15px 15px rgba(0,0,0,0.05),
inset 0 -15px 15px rgba(0,0,0, 0.05);
}
.clock:before
{
content:"";
position: absolute;
width: 15px;
height: 15px;
background: #fff;
border-radius: 50%;
z-index:10000;
}
.clock .hour,
.clock .min,
.clock .sec
{
position: absolute;
}
.clock .hour, .hr{
width: 160px;
height: 160px;
}
.clock .min, .mn{
width: 190px;
height: 190px;
}
.clock .sec, .sc{
width: 230px;
height: 230px;
}
.hr, .mn, .sc{
display: flex;
justify-content: center;
position: absolute;
border-radius: 50%;
}
.hr:before{
content:"";
position: absolute;
width: 8px;
height: 80px;
background: #ff105e;
z-index: 10;
border-radius: 6px 6px 0 0;
}
.mn:before{
content:"";
position: absolute;
width: 4px;
height: 90px;
background: #fff;
z-index: 11;
border-radius: 6px 6px 0 0;
}
.sc:before{
content:"";
position: absolute;
width: 4px;
height: 150px;
background: #fff;
z-index:12;
border-radius: 6px 6px 0 0;
}
以上就是用JS+CSS实现动态时钟的示例 ,上文有详细的代码,对大家学习是一定的参考价值的,有学习需要的朋友可以自己参考上述代码来实现一个酷炫的时钟效果。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
简单来说就是使用自定义hook可以将某些组件逻辑提取到可重用的函数中。 自定义hook是一个从use开始的调用其他hook的Javascript函数,下面看下react中创建自定义hooks的相关知识,感兴趣的朋友一起看看吧
javascript将字符串转换为8进制的方法:1、使用parseInt()函数将字符串转为十进制数,语法“parseInt(字符串)”;2、使用toString()函数将十进制数转为8进制数,语法“十进制数.toString(8)”。
本文用vue简单的实现了一个轮播图的基础功能,并抽离出来作为一个公共组件,方便复用
JS数组中filter方法的使用是什么?filter()创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。
这篇文章主要为大家详细介绍了vue+el-upload实现多文件动态上传,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008