html+css 实现红绿灯效果

声明一个函数light,接收时间、颜色、当前标签三个参数,函数中获取当前标签并改变其背景颜色赋值,返回promise,成功执行一个延时器,延时器中执行promise成功的回调
声明另一个函数step,并执行 Promise.resolve().then方法,方法中分别获取三个灯的标签,并未当前标签背景颜色设置为白色,返回 light(时间,颜色, 当前标签);
执行 step 函数

<!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>
  </head>
  <body>
    <div style="display: flex">
      <div
        style="
          padding: 10px 15px;
          background-color: black;
          border: 1px solid #666;
          border-radius: 15px;
        "
      >
        <div
          class="redbox"
          style="
            width: 100px;
            height: 100px;
            border: 1px solid #fff;
            background: #fff;
            border-radius: 50%;
          "
        ></div>
        <div
          style="
            width: 100px;
            height: 100px;
            border: 1px solid #fff;
            background: #fff;
            border-radius: 50%;
          "
        ></div>
        <div
          style="
            width: 100px;
            height: 100px;
            border: 1px solid #fff;
            background: #fff;
            border-radius: 50%;
          "
        ></div>
      </div>
      <div
        style="
          padding: 10px 15px;
          background-color: black;
          border: 1px solid #666;
          border-radius: 15px;
        "
      >
        <div
          style="
            width: 100px;
            height: 100px;
            border: 1px solid #fff;
            background: #fff;
            border-radius: 50%;
          "
        ></div>
        <div
          style="
            width: 100px;
            height: 100px;
            border: 1px solid #fff;
            background: #fff;
            border-radius: 50%;
          "
          class="greenbox"
        ></div>
        <div
          style="
            width: 100px;
            height: 100px;
            border: 1px solid #fff;
            background: #fff;
            border-radius: 50%;
          "
        ></div>
      </div>
      <div
        style="
          padding: 10px 15px;
          background-color: black;
          border: 1px solid #666;
          border-radius: 15px;
        "
      >
        <div
          style="
            width: 100px;
            height: 100px;
            border: 1px solid #fff;
            background: #fff;
            border-radius: 50%;
          "
        ></div>
        <div
          style="
            width: 100px;
            height: 100px;
            border: 1px solid #fff;
            background: #fff;
            border-radius: 50%;
          "
        ></div>
        <div
          style="
            width: 100px;
            height: 100px;
            border: 1px solid #fff;
            background: #fff;
            border-radius: 50%;
          "
          class="yellowbox"
        ></div>
      </div>
    </div>
    <script>
      let light = function (timmer, color, box) {
        console.log(box);
        let divbox = document.querySelector("." + box);
        divbox.style.backgroundColor = color;
        return new Promise(function (resolve, reject) {
          setTimeout(function () {
            resolve();
          }, timmer);
        });
      };

      let step = function () {
        Promise.resolve()
          .then(function () {
            let divbox = document.querySelector("." + "yellowbox");
            divbox.style.backgroundColor = "#fff";
            return light(3000, "red", "redbox");
          })
          .then(function () {
            let divbox = document.querySelector("." + "redbox");
            divbox.style.backgroundColor = "#fff";
            return light(2000, "green", "greenbox");
          })
          .then(function () {
            let divbox = document.querySelector("." + "greenbox");
            divbox.style.backgroundColor = "#fff";
            return light(1000, "yellow", "yellowbox");
          })
          .then(function () {
            step();
          });
      };
      step();
    </script>
  </body>
</html>
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值