帮我写一个跨年倒计时html代码
时间: 2023-02-08 15:50:33 浏览: 130
2024跨年倒计时html代码(跨年倒计时html代码2024)
5星 · 资源好评率100%
以下是一个简单的跨年倒计时 HTML 代码示例:
```html
<!DOCTYPE html>
<html>
<head>
<title>New Year Countdown</title>
</head>
<body>
<h1 id="countdown"></h1>
<script>
// Set the date we're counting down to
var countDownDate = new Date("Jan 1, 2022 00:00:00").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="countdown"
document.getElementById("countdown").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("countdown").innerHTML = "Happy New Year!";
}
}, 1000);
</script>
</body>
</html>
```
请注意,在上面代码中,我们设置了跨年时间为2022年1月1日, 如果您需要其他时间,请更改countDownDate 变量的值即可
阅读全文