js实现倒计时特效(背景颜色随时间改变)

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 {
                    // 返回rgb随机颜色
                    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)
                // 不满10补零操作
                h = h > 10 ? h : '0' + h
                // 得到分
                let m = parseInt(time / 60 % 60)
                // 不满10补零操作
                m = m > 10 ? m : '0' + m
                // 得到秒
                let s = parseInt(time % 60)
                // 不满10补零操作
                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>
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东方青云、

你的鼓励将是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值