使用JavaScript、CSS、HTML完成红绿灯小案例

主要使用JavaScript里的鼠标点击时间onclick,倒计时事件setInterval等等。

代码:

<!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>
<style>
    *{
        margin: 0;
        padding: 0;
    }
    body{
        background-color: black;
    }
    div.red{
        width: 150px;
        height: 150px;
        border-radius: 50%;
        background-color: rgb(129, 129, 129);
        float: left;
        position: absolute;
        top: 300px;
        left: 470px;
        cursor: pointer;
    }
    div.red h1{
        font-size: 50px;
        font-weight: 300;
        text-align: center;
        line-height: 150px;
        color: white;
    }
    div.green{
        width: 150px;
        height: 150px;
        border-radius: 50%;
        background-color: rgb(129, 129, 129);
        float: left;
        position: absolute;
        top: 300px;
        left: 680px;
        cursor: pointer;
    }
    div.green h1{
        font-size: 50px;
        font-weight: 300;
        text-align: center;
        line-height: 150px;
        color: white;
    }
    div.yellow{
        width: 150px;
        height: 150px;
        border-radius: 50%;
        background-color: rgb(129, 129, 129);
        float: left;
        position: absolute;
        top: 300px;
        left: 880px;
        cursor: pointer;
    }
    div.yellow h1{
        font-size: 50px;
        font-weight: 300;
        text-align: center;
        line-height: 150px;
        color: white;
    }
</style>
<body>
    <div class="red">
        <h1 id="red" onclick="red()">红</h1>
    </div>
    <div class="green">
        <h1 id="green" onclick="green()">绿</h1>
    </div>
    <div class="yellow">
        <h1 id="yellow" onclick="yellow()">黄</h1>
    </div>
</body>
<script>
    let i = 10;
    let j = 15;
    let k = 3;
    let intval;
    intval = setInterval(red,1000);
    red();
    function red(){
        document.getElementById("red").style = "background-color:red;border-radius: 50%;";
        document.getElementById("red").innerHTML = i;
        
            if (i === 0){
                clearInterval(intval);
                i = 11;
                document.getElementById("red").style = "background-color: rgb(129, 129, 129);border-radius: 50%;";
                document.getElementById("red").innerHTML = "红";
                intval = setInterval(green,1000);
                green();
            }
        i--;
    }
    function green(){
        document.getElementById("green").style = "background-color:green;border-radius: 50%;";
        document.getElementById("green").innerHTML = j;
        
            if (j === 0){
                clearInterval(intval);
                j = 16;
                document.getElementById("green").style = "background-color: rgb(129, 129, 129);border-radius: 50%;";
                document.getElementById("green").innerHTML = "绿";
                intval = setInterval(yellow,1000);
                yellow();
            }
        j--; 
    }
    function yellow(){
        document.getElementById("yellow").style = "background-color:yellow;border-radius: 50%;";
        document.getElementById("yellow").innerHTML = k;
        
            if (k === 0){
                clearInterval(intval);
                k = 4;
                document.getElementById("yellow").style = "background-color: rgb(129, 129, 129);border-radius: 50%;";
                document.getElementById("yellow").innerHTML = "黄";
                intval = setInterval(red,1000);
                red();
            }
        k--; 
    }


</script>
</html>

效果图:

 

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

码农叮叮车

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

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

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

打赏作者

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

抵扣说明:

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

余额充值