京东秒杀倒计时案例
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>京东秒杀</title>
<style>
* {
padding: 0;
margin: 0;
}
body,
h1,
h2,
h3,
h4,
h5,
h6 {
font-weight: normal;
font-size: 12px;
}
.jd {
width: 200px;
height: 300px;
background-color: #e83632;
;
margin-left: 100px;
margin-top: 100px;
text-align: center;
}
h2 {
padding: 15px 0;
font-size: 36px;
color: #fff;
}
h3 {
font-size: 20px;
color: #ccc;
}
.title {
margin-top: 100px;
color: #fff;
font-size: 16px;
}
.deay {
margin-top: 30px;
text-align: center;
height: 30px;
line-height: 30px;
}
.hours,
.minute,
.second {
display: inline-block;
width: 30px;
height: 30px;
background-color: #000;
color: #fff;
font-size: 16px;
}
</style>
</head>
<body>
<div class="jd">
<h2>京东秒杀</h2>
<h3>FLASH DEALS</h3>
<p class="title">距离本场结束还有</p>
<div class="deay">
<span class="hours">01</span>
<span class="minute">20</span>
<span class="second ">39</span>
</div>
</div>
<script>
f1()
setInterval(f1, 1000)
function f1() {
// 获取当前时间的毫秒数
var now = new Date().getTime()
var t = new Date('2020-12-20 0:0:0').getTime()
var times = (t - now) / 1000
// 计算今天距离2020-12-20 的毫秒数
var h = parseInt(times / 60 / 60 % 24)
h = h < 10 ? '0' + h : hours
var m = parseInt(times / 60 % 60); // 分
m = m < 10 ? '0' + m : m;
var s = parseInt(times % 60); // 当前的秒
s = s < 10 ? '0' + s : s;
document.querySelector('.hours').innerHTML = h
document.querySelector('.minute').innerHTML = m
document.querySelector('.second').innerHTML = s
}
</script>
</body>
</html>
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201214140900426.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xJS0RfRElLTA==,size_16,color_FFFFFF,t_70)