用html制作下班倒计时且接入注释,设置字体居中,显示时间加入背景颜色
时间: 2023-11-25 08:05:18 浏览: 74
一款用于倒计时的html5代码
好的,我来给你实现一个用 HTML 制作下班倒计时的代码,并加入注释、设置字体居中、显示时间加入背景颜色。
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>下班倒计时</title>
<style>
/* 设置字体居中 */
body {
font-family: Arial, Helvetica, sans-serif;
text-align: center;
}
/* 设置背景颜色 */
#countdown {
background-color: #f2f2f2;
padding: 20px;
border-radius: 10px;
display: inline-block;
}
</style>
</head>
<body>
<h1>下班倒计时</h1>
<p>现在是 <span id="current_time"></span></p>
<p>距离下班还有 <span id="countdown"></span></p>
<script type="text/javascript">
// 设置下班时间,单位为小时
var offWorkTime = 18;
// 获取当前时间
var now = new Date();
var currentHour = now.getHours();
var currentMinute = now.getMinutes();
var currentSecond = now.getSeconds();
// 显示当前时间
document.getElementById("current_time").innerHTML = currentHour + ":" + currentMinute + ":" + currentSecond;
// 计算距离下班时间还有多久
var countdownHour = offWorkTime - currentHour - 1;
var countdownMinute = 60 - currentMinute - 1;
var countdownSecond = 60 - currentSecond;
var countdownInterval = setInterval(function() {
countdownSecond--;
if (countdownSecond < 0) {
countdownMinute--;
countdownSecond = 59;
}
if (countdownMinute < 0) {
countdownHour--;
countdownMinute = 59;
}
if (countdownHour < 0) {
clearInterval(countdownInterval);
document.getElementById("countdown").innerHTML = "已经下班啦!";
} else {
document.getElementById("countdown").innerHTML = countdownHour + "小时" + countdownMinute + "分钟" + countdownSecond + "秒";
}
}, 1000);
</script>
</body>
</html>
```
注释已经加好了,你可以根据需要修改倒计时的下班时间,同时也可以修改样式的设置。
阅读全文