来源
学习视频来自于[JS+CSS简约好看的电子时钟效果~_哔哩哔哩_bilibili](https://www.bilibili.com/video/BV1AV4y1C7uq/?spm_id_from=333.880.my_history.page.click&vd_source=47be49ab0d7cd5a7b3de5a99b7bcff57)
实现
html
<!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">
<title>Demo-时钟</title>
<link rel="icon" sizes="16x16" href="favicon.ico">
<style >
body{
margin: 0;
padding: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: #ececec;
background-size: cover;
}
#main{
width: 1200px;
height: 300px;
line-height: 300px;
text-align: center;
background-color: #ececec;
border-radius: 70px;
box-shadow: -10px -10px 20px rgba(255,255,255,.5),
10px 10px 20px rgba(70,70,255,.12);
}
#clock{
font-size: 200px;
-webkit-text-stroke: 3px #fff;
color: #000;
letter-spacing: 25px;
}
</style>
</head>
<body>
<div id="main">
<div id="clock">12:00:00</div>
</div>
<script src="./clock.js"></script>
</body>
</html>
clock.js
let oClock = document.querySelector('#clock');
let addZero = (num)=>{
if(num >10) return num;
else return `0${num}`
}
let updateTime = ()=>{
let now = new Date();
let time = addZero(now.getHours())+":"+addZero(now.getMinutes())+":"+addZero(now.getSeconds());
oClock.innerText = time;
}
updateTime(); //先直接执行
setInterval(updateTime, 1000)
效果
总结
按照视频复现的简单的练手项目,实现时钟效果,另外,可以使用网站进行标签页图标的转换(www.favicon-generator.org/)