web前端如何实现html的3D倒计时 代码
时间: 2024-05-12 11:12:59 浏览: 54
元旦倒计时代码,纯前端html+css+js实现
要实现HTML的3D倒计时,可以使用CSS3的3D变换和JS的计时器来实现。以下是一个简单的实现示例:
HTML代码:
```html
<div class="countdown">
<div class="box">
<div class="side front">
<span id="days">00</span>
<span>Days</span>
</div>
<div class="side back">
<span id="hours">00</span>
<span>Hours</span>
</div>
<div class="side top">
<span id="minutes">00</span>
<span>Minutes</span>
</div>
<div class="side bottom">
<span id="seconds">00</span>
<span>Seconds</span>
</div>
<div class="side left"></div>
<div class="side right"></div>
</div>
</div>
```
CSS代码:
```css
.countdown {
perspective: 800px;
margin: 50px auto;
width: 300px;
height: 300px;
}
.box {
position: relative;
width: 100%;
height: 100%;
transform-style: preserve-3d;
animation: rotate 1s linear infinite;
}
.side {
position: absolute;
width: 100%;
height: 100%;
background-color: #333;
font-size: 40px;
color: #fff;
text-align: center;
line-height: 300px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5);
}
.front {
transform: rotateY(0deg) translateZ(150px);
}
.back {
transform: rotateY(180deg) translateZ(150px);
}
.top {
transform: rotateX(-90deg) translateZ(150px);
}
.bottom {
transform: rotateX(90deg) translateZ(150px);
}
.left {
transform: rotateY(-90deg) translateZ(150px);
}
.right {
transform: rotateY(90deg) translateZ(150px);
}
@keyframes rotate {
0% {
transform: rotateY(0deg);
}
100% {
transform: rotateY(360deg);
}
}
```
JS代码:
```js
function countdown() {
var now = new Date();
var eventDate = new Date("2022-01-01");
var currentTime = now.getTime();
var eventTime = eventDate.getTime();
var remTime = eventTime - currentTime;
var seconds = Math.floor(remTime / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %= 60;
document.getElementById("days").innerHTML = days;
document.getElementById("hours").innerHTML = hours;
document.getElementById("minutes").innerHTML = minutes;
document.getElementById("seconds").innerHTML = seconds;
setTimeout(countdown, 1000);
}
countdown();
```
在上面的代码中,我们使用了CSS3的3D变换来创建一个带有6个面的盒子,然后使用JS的计时器来计算剩余时间,并将其更新到对应的面上。最后,我们使用CSS的动画来使盒子无限旋转。
阅读全文