交通灯的实现

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .right {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            float: left;
            text-align: center;
            color: white;
        }
        #nav {
            transform: translate(43%);
        }
        .red {
            background-color: red;
        }
        .green {
            background-color: Lime;
        }
        @keyframes myfirst {
            0% {
                background-color: rgba(255, 255, 0, 1);
            }
            100% {
                background-color: rgba(255, 255, 0, 0);
            }
        }
        @keyframes mysecond {
            0% {
                background-color: rgba(0, 255, 0, 1);
            }
            100% {
                background-color: rgba(0, 255, 0, 0);
            }
        }
        @keyframes mythist {
            0% {
                background-color: rgba(255, 0, 0, 1);
            }
            100% {
                background-color: rgba(255, 0, 0, 0);
            }
        }
        @keyframes myp {
            0% {
                color: rgba(0, 0, 0, 1);
            }
            100% {
                color: rgba(0, 0, 0, 0);
            }
        }
        .yellow {
            background-color: yellow;
            animation: myfirst 1s infinite;
        }
        .g {
            animation: mysecond 1s infinite;
        }
        .r {
            animation: mythist 1s infinite;
        }
        #time1, #time2, #time3 {
            animation: myp 1s infinite;
            font-size: 40px;
            padding-top: 20px;
        }
    </style>
</head>
<body>
<div id="nav">
    <div class="right red">
        <div id="time1" class="p"></div>
    </div>
    <div class="right">
        <div id="time2" class="p"></div>
    </div>
    <div class="right">
        <div id="time3" class="p"></div>
    </div>
</div>
<script>
    let colorred = document.getElementsByClassName('right')[0];
    let colorgreen = document.getElementsByClassName('right')[2];
    let coloryellow = document.getElementsByClassName('right')[1];
    const setTime = function (ms) {
        return function (color) {
            return new Promise((resolve, reject) => {
                setTimeout(resolve, ms, color)
            });
        }
    }
    let red = setTime(30000);
    let yellow = setTime(4000);
    let green = setTime(30000);
    let p = document.getElementById("time1");
    let py = document.getElementById("time2");
    let pg = document.getElementById("time3");
    (function reset() {
        var time = 30;
        var timey = 4;
        var timeg = 30;
        (function () {
            let set = setInterval(function () {
                time--;
                p.innerHTML = time;
                if (time == 3) {
                    colorred.className = 'right red r';
                }
                if (time === 0) {
                    p.innerHTML = "";
                    clearInterval(set);
                    (function () {
                        let set = setInterval(function () {
                            timey--;
                            py.innerHTML = timey;
                            if (timey === 0) {
                                py.innerHTML = "";
                                clearInterval(set);
                            }
                        }, 1000);
                    })();
                }
            }, 1000);
        })();
        red('red').then((data) => {
            colorred.className = 'right';
            coloryellow.className = 'right yellow';
            return yellow('yellow')
        }).then((data) => {
            coloryellow.className = 'right';
            colorgreen.className = 'right green';
            var timey = 4;
            (function () {
                let set = setInterval(function () {
                    timeg--;
                    pg.innerHTML = timeg;
                    if (timeg == 3) {
                        colorgreen.className = 'right red g';
                    }
                    if (timeg === 0) {
                        pg.innerHTML = "";
                        clearInterval(set);
                        (function () {
                            let set = setInterval(function () {
                                timey--;
                                py.innerHTML = timey;
                                if (timey === 0) {
                                    py.innerHTML = "";
                                    clearInterval(set);
                                }
                            }, 1000);
                        })();
                    }
                }, 1000);
            })();
            return green('green')
        }).then((data) => {
            colorgreen.className = 'right'
            coloryellow.className = 'right yellow'
            return yellow('yellow')
        }).then((data) => {
            console.log(data, new Date().getSeconds());
            coloryellow.className = 'right';
            colorred.className = 'right red';
            reset();
        });
    })();
</script>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值