<body>
<h1 class="title">2020年倒计时</h1>
<div class="time-item">
<span><span id="day">00</span>天</span>
<strong><span id="hour">00</span>时</strong>
<strong><span id="minute">00</span>分</strong>
<strong><span id="second">00</span>秒</strong>
</div>
<script>
var day = document.getElementById('day')
var hour = document.getElementById('hour')
var minute = document.getElementById('minute')
var second = document.getElementById('second')
function countTime (){
//获取当前时间
var data = new Date()
var now = data.getTime()
//设置截止时间
var str = "2021/1/1 00:00:00"
var endDate = new Date(str)
var end = endDate.getTime()
//时间差
var leftTime = end - now
if(leftTime>0){
day.innerHTML = Math.floor(leftTime/1000/60/60/24)
hour.innerHTML = Math.floor(leftTime/1000/60/60%24)
minute.innerHTML = Math.floor(leftTime/1000/60%60)
second.innerHTML = Math.floor(leftTime/1000%60)
}
setTimeout(countTime,1000)
}
countTime()
</script>
</body>
h1标签、div样式都可以自行设置
这是我的代码的实现,时间都是可以动的哦