效果图
HTML
<div id="box">
<div class="miao">京东秒杀</div>
<div class="two"><span id="end">XXX</span>:00 点场 距结束</div><br>
<div class="three">
<span id="hour">02</span>
<span>:</span>
<span id="minute">00</span>
<span>:</span>
<span id="second">00</span>
</div>
</div>
CSS
<style>
* {
margin: 0;
padding: 0;
font-weight: 700
}
#box {
width: 190px;
height: 260px;
position: relative;
margin: 100px auto;
background: url(../images/秒杀.png);
}
.miao {
color: white;
font-size: 30px;
width: 190px;
height: 100px;
line-height: 100px;
text-align: center;
}
.two {
color: white;
position: absolute;
text-align: center;
width: 190px;
top: 160px;
font-size: 20px;
}
#hour,
#minute,
#second {
background-color: black;
color: white;
font-size: 30px;
}
.three {
width: 190px;
text-align: center;
position: absolute;
bottom: 20px;
}
</style>
JS
let end = document.querySelector('#end');
let hour = document.querySelector('#hour');
let minute = document.querySelector('#minute');
let second = document.querySelector('#second');
let th, tm, ts;
function time() {
let endTime = new Date();
let startTime = new Date();
let h = endTime.getHours();
h % 2 == 0 ? h : h--;
end.innerHTML = h;
endTime.setHours(h + 2);
endTime.setMinutes('0');
endTime.setSeconds('0');
let diff = (endTime - startTime) / 1000;
if (diff > 0) {
th = parseInt(diff / 60 / 60);
tm = parseInt(diff / 60 % 60);
ts = parseInt(diff % 60);
th = th < 10 ? '0' + th : th;
tm = tm < 10 ? '0' + tm : tm;
ts = ts < 10 ? '0' + ts : ts;
hour.innerHTML = th;
minute.innerHTML = tm;
second.innerHTML = ts;
}
}
setInterval(time,1000);