红绿灯+读秒——html+CSS+JavaScript实现

一、实现效果

       绿(10秒)→黄(5秒)→红(10秒)→绿(10秒)…依次循环。

二、代码分析

       首先编写html代码,使用CSS确定需要改变的div。其次编写JavaScript代码,通过修改背景颜色的方式,设置信号灯样式。最后实现红绿灯读秒,利用setInterval()和setTimeout()函数完成信号灯读秒的动态改变效果。
 

三、代码实现


1.编写HTML代码

<body>
<div class="trfc_lts1">
    <div id="redLight" class="light"></div>         <!-- 红灯 -->
    <div id="yellowLight" class="light"></div>      <!-- 黄灯 -->
    <div id="greenLight" class="light"></div>       <!-- 绿灯 -->
</div>
<div id="countDown"></div>                          <!-- 倒计时 -->
<button onclick="startChange()">开始工作</button>    
</body>

2.初始化

       将三块颜色全部变白,可以不写。

function startChange() {
    let redObj = document.getElementById("redLight");
    let yellowObj = document.getElementById("yellowLight");
    let greenObj = document.getElementById("greenLight");
    redObj.style.backgroundColor = "white";
    yellowObj.style.backgroundColor = "white";
    greenObj.style.backgroundColor = "white";

    toGreen();
}

       toGreen():使信号灯由红变绿。

3.实现红绿灯变化

       这里以变成绿色为例,变成其他颜色的函数是类似的。

function toGreen() {                                    <!-- 变成绿色 -->
    let redObj = document.getElementById("redLight");
    let greenObj = document.getElementById("greenLight");
    redObj.style.backgroundColor = "white";
    greenObj.style.backgroundColor = "green";
    document.getElementById('countDown').style.color='green';//数字颜色相应信号灯改变
    let timer = 9;
    countDown(timer);
    setTimeout("toYellow()",(timer+1)*1000);            <!-- 1000ms == 1s -->
}

       timer:存储对应信号灯剩余亮灯时间。(注:这里的timer赋值为9(少了一秒),是由于单线程的缘故,颜色改变的同时倒计时并不会开始(会等一秒),因此在后面的countDown()函数和setTimeout里对timer加一,对齐时间。)

       countDown():倒计时函数。

4.实现红绿灯倒计时

function countDown(timer){
    document.getElementById('countDown').innerHTML = timer+1; //对应颜色显示初始数字
    setInterval(() => {
        if (timer >= 0) {
            document.getElementById('countDown').innerHTML = timer--; //倒数
        }
    }, 1000);
}

       利用setInterval()函数,在每隔1秒钟间歇调用一次匿名函数。 

5.CSS相关

.trfc_lts1{
    width: 220px;
    height: 618px;
    float: left;
    border: 1px solid black;
}
.trfc_lts1 .light{
    width: 200px;
    height: 200px;
    border-radius: 100%;
    border:3px solid black;
}
.trfc_lts1 #redLight{
    background-color: red;
    margin-left: 7px;
}
.trfc_lts1 #yellowLight{
    background-color: yellow;
    margin-left: 7px;
}
.trfc_lts1 #greenLight{
    background-color: green;
    margin-left: 7px;
}
#countDown{
    margin-left: 10px;
    width: 202px;
    height: 202px;
    font-size: 155px;
    float: left;
    border: 2px solid black;
    background-color: gray;
}



总结

感谢文章javascript并行执行详解给予的启发。

如果对你有所帮助,别忘了帮忙点个赞!!!

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

DDD_duck

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

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

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

打赏作者

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

抵扣说明:

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

余额充值