实现红绿灯效果

实现效果如图
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
		body{
			background:#1abc9c;
		}
		.box{
			height: 165px;
			width: 70px;
			margin: 300px auto;
			background-color: #2c3e50;
			border-radius: 50px;
			padding: 15px 0px;
		}
		.around{
			width: 40px;
			height: 40px;
			background:rgba(0,0,0,0.3);
			border-radius: 50%;
			margin: 10px 0px 0px 14px;
			position: relative;
		}
		.around::after {
			border-right: 4px solid rgba(255, 255, 255, 0.6);
			border-radius: 100%;
			content: ' ';
			position: absolute;
			top: 5px;
			left: 0px;
			width: 30px;
			height: 30px;
		}
		.red{
			background-color: rgba(206,25,28,1.00);
			box-shadow: 0px 0px 20px 5px rgba(206,25,28,1.00);
		}
		.yellow{
			background-color: rgba(218,201,10,1.00);
			box-shadow: 0px 0px 20px 5px rgba(218,201,10,1.00);	
		}
		.green{
			background-color: rgba(0,197,56,1.00);
			box-shadow: 0px 0px 20px 5px  rgba(0,197,56,1.00);
		}
	</style>
</head>
<body>
    <div class="box">
		<div class="around red" color="red"></div>
		<div class="around" color="yellow"></div>
		<div class="around" color="green"></div>
    </div>
    
    <script>
		const arounds=document.querySelectorAll('.around')
		//querySelector类选择器 const保留字
		let index=0;
		setInterval(()=>{
			changeLight();
		},1000);
		//setInterval()计时器 changeLight()1000毫秒改变一次
		function changeLight(){
			arounds[index].className='around';
		//around赋值于arounds[index]
			index++;
			
			if(index>2){
				index=0;
			}
			//如果index小于2,index=0
			const i=arounds[index]
			i.classList.add(i.getAttribute('color'));
		}
	</script>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用如下的 Javascript 代码实现: ```javascript function trafficLight() { var redTime = 30; // 红灯持续时间,单位:秒 var yellowTime = 3; // 黄灯持续时间,单位:秒 var greenTime = 45; // 绿灯持续时间,单位:秒 var red = document.getElementById('red'); var yellow = document.getElementById('yellow'); var green = document.getElementById('green'); setInterval(function() { // 显示红灯,隐藏黄灯和绿灯 red.style.display = 'block'; yellow.style.display = 'none'; green.style.display = 'none'; setTimeout(function() { // 显示黄灯,隐藏红灯绿灯 red.style.display = 'none'; yellow.style.display = 'block'; green.style.display = 'none'; setTimeout(function() { // 显示绿灯,隐藏红灯和黄灯 red.style.display = 'none'; yellow.style.display = 'none'; green.style.display = 'block'; }, greenTime * 1000); }, yellowTime * 1000); }, (redTime + yellowTime + greenTime) * 1000); } trafficLight(); ``` 这段代码使用了 `setInterval` 和 `setTimeout` 函数来实现红绿灯的定时控制。首先定义了红灯、黄灯和绿灯的持续时间,以及相应的 DOM 元素。然后使用 `setInterval` 函数来控制整个红绿灯的循环,每次循环的总时间为红灯、黄灯和绿灯的持续时间之和。在每个循环中,先使用 `setTimeout` 函数来控制红灯的持续时间,然后再使用 `setTimeout` 函数来控制黄灯的持续时间,最后使用 `setTimeout` 函数来控制绿灯的持续时间。在每个时间段中,只显示相应的灯,其他灯都隐藏。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值