!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>
* {
padding: 0;
margin: 0;
}
.box {
position: relative;
margin: 100px auto;
width: 240px;
height: 340px;
padding-top: 51px;
background-color: red;
text-align: center;
box-sizing: border-box;
}
.p1 {
width: 240px;
height: 70px;
line-height: 70px;
color: aliceblue;
font-size: 40px;
}
.p2 {
width: 240px;
height: 40px;
line-height: 40px;
color: #ccc;
font-size: 23px;
}
.p3 {
width: 240px;
height: 60px;
line-height: 60px;
font-size: 20px;
color: #fff;
}
.p4 {
width: 240px;
height: 45px;
line-height: 45px;
font-size: 18px;
color: #fff;
}
.times {
width: 240px;
height: 74px;
text-align: center;
}
span {
display: inline-block;
margin-top: 12px;
width: 50px;
height: 50px;
background-color: #000;
margin-right: 5px;
font-size: 30px;
color: #fff;
line-height: 50px;
}
</style>
</head>
<body>
<div class="box">
<div class="'txt">
<div class="p1">京东秒杀</div>
<div class="p2">给个关注</div>
<div class="p3">CSDN:秋叶00</div>
<div class="p4">本场距离结束还剩</div>
</div>
<div class="times">
<span class="hours">2</span>
<span class="min">3</span>
<span class="second">5</span>
</div>
</div>
<script>
var hours = document.querySelector('.hours')
var min = document.querySelector('.min')
var second = document.querySelector('.second')
var now = +new Date('2022-7-1 00:00:00'); //返回用户输入时间总毫秒数
getDaojishi();
// 开启定时器
setInterval(getDaojishi, 700)
function getDaojishi(time) {
var date = +new Date(); //时间总毫秒数
var times = (now - date) / 1000; //剩余时间总秒数
var t = parseInt(times / 60 / 60 / 24); //天
t = t < 10 ? '0' + t : t;
var h = parseInt(times / 60 / 60 % 24); //小时
h = h < 10 ? '0' + h : h;
hours.innerHTML = h;
var m = parseInt(times / 60 % 60); //分钟
m = m < 10 ? '0' + m : m;
min.innerHTML = m;
var s = parseInt(times % 60); //秒
s = s < 10 ? '0' + s : s;
second.innerHTML = s;
// return t + '天' + h + '小时' + m + '分钟' + s + '秒'
}
</script>
</body>
</html>