JS代码+CSS样式实现一个动态时钟效果
Admin 2021-04-13 群英技术资讯 1362 次浏览
时钟是我们查看时间的一种工具,很多人家家里都会摆放一个时钟,那么我们用代码能实现一个时钟效果吗?答案是可以用的,这篇文章主要教大家用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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了JavaScript中对于SPA单页面的理解,单页应用SPA是一种网络应用程序或网站的模型,它通过动态重写当前页面来与用户交互
目录ant design vue的form表单取值官方中有以下两种取值方式ant design of vue 学习之表单formv-decorator(表单验证,内置绑定,初始值)数据获取与填充表单实例ant design vue的form表单取值官方中有以下两种取值方式因为不是很熟悉,所以还是查了文档找了一下使用方式
这篇文章我们来了解JavaScript迭代器相关内容,下文将介绍什么是迭代器,以及自定义迭代器的方法,文中示例代码介绍的非常详细,对大家学习和理解JavaScript迭代器会有一定的帮助,那么感兴趣的朋友接下来一起跟随小编了解看看吧。
本文主要介绍了Vue实现动态路由导航的示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
NgModule 模块是Angular种一个重要的点,因为Angular的基本构造块就是NgModule。本篇文章就来带大家了解一下Angular中的NgModule模块,希望对大家有所帮助!
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008