1、实现效果
2、实现代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
em,
i {
font-style: normal;
}
.center {
position: absolute;
top: 20%;
left: 50%;
transform: translate(-50%,-50%);
}
.recomment_today {
position: relative;
float: left;
width: 251px;
height: 170px;
line-height: 165px;
background-color: #0a9e87;
text-align: center;
cursor: pointer;
transition: all .3s linear;
}
.recomment_today i {
position: absolute;
top: -35px;
left: 50px;
font-size: 30px;
color: #fff;
font-weight: 700;
}
.recomment_today .ems {
position: absolute;
top: 12px;
left: 36px;
}
.recomment_today span {
margin-top: 118px;
display: inline-block;
width: 35px;
height: 35px;
background-color: #333;
font-size: 20px;
color: #fff;
text-align: center;
line-height: 35px;
}
.recomment_today:hover {
box-shadow: 0 10px 10px #ccc;
}
.recomment_today em {
font-size: 20px;
margin-left: 2px;
color: #fff;
font-weight: 700;
}
</style>
</head>
<body>
<div class="center">
<div class="recomment_today">
<i>小兔鲜秒杀</i>
<em class="ems">22:00点场 距结束</em>
<span class="hour">12</span><em>:</em>
<span class="minute">20</span><em>:</em>
<span class="second">03</span>
</div>
</div>
<script>
(function () {
function getRandomColor(flag = true) {
if (flag) {
let str = '#'
let arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
for (let i = 1; i <= 6; i++) {
let random = Math.floor(Math.random() * arr.length)
str += arr[random]
}
return str
} else {
let r = Math.floor(Math.random() * 256)
let g = Math.floor(Math.random() * 256)
let b = Math.floor(Math.random() * 256)
return `rgb(${r},${g},${b})`
}
}
function countDown() {
let now = +new Date()
let last = +new Date('2024-3-28 22:00:00')
let time = (last - now) / 1000
let h = parseInt(time / 60 / 60 % 24)
h = h > 10 ? h : '0' + h
let m = parseInt(time / 60 % 60)
m = m > 10 ? m : '0' + m
let s = parseInt(time % 60)
s = s > 10 ? s : '0' + s
document.querySelector('.hour').innerHTML = h
document.querySelector('.minute').innerHTML = m
document.querySelector('.second').innerHTML = s
document.querySelector('.recomment_today').style.backgroundColor = getRandomColor()
}
countDown()
setInterval(countDown, 1000)
})()
</script>
</body>
</html>