代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=<s>, initial-scale=1.0">
<title>Document</title>
<style>
.d1{
width: 872px;
height: 273px;
margin: 0 auto;
}
.d2{
text-align: center;
margin-top: 20px;
color: rgb(247, 31, 31);
}
</style>
</head>
<body>
<div class="d1"><img src="./images/miaosha.jpg" alt=""></div>
<div id="miaosha" class="d2"></div>
<script>
var dDiv = document.getElementById("miaosha");
function countDown(time){
var nowTime = new Date();//获取当前日期
var inputTime = new Date(time);//获取输入日期
var times = (inputTime - nowTime)/1000;//获取输入时间与现在时间相差的秒数
if (times > 0){
var d = parseInt (times/60/60/24);//获取相差天数
var h = parseInt (times/60/60%24);// 获取倒计时的小时数
var m = parseInt (times/60%60);//获取倒计时的分钟数
var s = parseInt (times%60);//获取倒计时的秒数
dDiv.innerHTML ="距离本场秒杀结束还剩: "+ d + "天" + h + "时" + m +"分" + s +"秒";
}
else{
clearInterval(id);
}
}
var id =setInterval(countDown,1000,"2024-11-11 00:00:00");
</script>
</body>
</html>
效果图: