<!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>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
div {
width: 500px;
height: 100px;
font-size: 26px;
/* color: beige; */
margin: 200px auto;
}
div span {
background-color: black;
width: 80px;
height: 80px;
float: left;
margin: 100px 5px;
color: azure;
line-height: 80px;
text-align: center;
}
</style>
</head>
<body>
<div>
<span class="hour">1</span>
<span class="minute">2</span>
<span class="second">3</span>
</div>
<script>
var hour = document.querySelector('.hour');
var minute = document.querySelector('.minute');
var second = document.querySelector('.second');
var inputTime = +new Date('2022-6-22 15:00:00');
countDown(); //调用函数,防止有空白
//开启定时器
setInterval(countDown, 1000);
function countDown() {
var nowTime = +new Date();
// 1秒=1000ms
var times = (inputTime - nowTime) / 1000;
var h = parseInt(times / 60 / 60 % 24); //时
h < 10 ? '0' + h : h;
hour.innerHTML = h;
var m = parseInt(times / 60 % 60); //分
m < 10 ? '0' + m : m;
minute.innerHTML = m;
var s = parseInt(times % 60); //秒
s < 10 ? '0' + s : s;
second.innerHTML = s;
}
</script>
</body>
</html>
JS--倒计时盒子
最新推荐文章于 2024-06-20 20:20:21 发布