用html写一个交通灯代码
时间: 2023-05-18 21:03:29 浏览: 47
<!DOCTYPE html>
<html>
<head>
<title>交通灯</title>
<style>
.red {
background-color: red;
border-radius: 50%;
height: 100px;
width: 100px;
display: inline-block;
}
.yellow {
background-color: yellow;
border-radius: 50%;
height: 100px;
width: 100px;
display: inline-block;
}
.green {
background-color: green;
border-radius: 50%;
height: 100px;
width: 100px;
display: inline-block;
}
</style>
</head>
<body>
<div class="red"></div>
<div class="yellow"></div>
<div class="green"></div>
</body>
</html>
相关问题
idea 写一个交通信号灯的显示页面
以下是一个交通信号灯的显示页面的代码示例:
```html
<!DOCTYPE html>
<html>
<head>
<title>Traffic Signal</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
/* 样式表 */
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
}
h1 {
text-align: center;
margin-top: 50px;
}
.traffic-signal {
width: 100px;
height: 300px;
margin: 0 auto;
background-color: black;
border-radius: 50%;
position: relative;
}
.traffic-signal .light {
width: 80px;
height: 80px;
border-radius: 50%;
position: absolute;
top: 20px;
left: 10px;
background-color: gray;
box-shadow: 0px 0px 5px white;
transition: background-color 1s ease-in-out;
}
.traffic-signal .light.red {
top: 20px;
background-color: red;
}
.traffic-signal .light.yellow {
top: 120px;
background-color: yellow;
}
.traffic-signal .light.green {
top: 220px;
background-color: green;
}
</style>
</head>
<body>
<h1>Traffic Signal</h1>
<div class="traffic-signal">
<div class="light red"></div>
<div class="light yellow"></div>
<div class="light green"></div>
</div>
<script>
// JavaScript 代码
const redLight = document.querySelector('.light.red');
const yellowLight = document.querySelector('.light.yellow');
const greenLight = document.querySelector('.light.green');
function switchLight() {
setTimeout(() => {
redLight.style.backgroundColor = 'red';
yellowLight.style.backgroundColor = 'gray';
greenLight.style.backgroundColor = 'gray';
}, 0);
setTimeout(() => {
redLight.style.backgroundColor = 'gray';
yellowLight.style.backgroundColor = 'yellow';
greenLight.style.backgroundColor = 'gray';
}, 3000);
setTimeout(() => {
redLight.style.backgroundColor = 'gray';
yellowLight.style.backgroundColor = 'gray';
greenLight.style.backgroundColor = 'green';
}, 5000);
setTimeout(() => {
redLight.style.backgroundColor = 'gray';
yellowLight.style.backgroundColor = 'yellow';
greenLight.style.backgroundColor = 'gray';
}, 8000);
setTimeout(switchLight, 10000);
}
switchLight();
</script>
</body>
</html>
```
你可以将代码复制到你的 IDE 或文本编辑器中,然后将文件保存为 `.html` 文件格式。在浏览器中打开该文件,你应该可以看到一个交通信号灯的显示页面,并且灯的颜色会按照一定的时间间隔自动切换。
用js写交通灯倒计时思路
好的,以下是用 JavaScript 实现交通灯倒计时的思路:
1. 首先,我们需要在 HTML 中创建三个 div 元素,分别代表红灯、黄灯、绿灯。
2. 在 CSS 中,为这三个 div 元素分别设置样式,使它们呈现出交通灯的形态。
3. 在 JavaScript 中,我们需要定义三个变量来分别表示红灯、黄灯和绿灯的倒计时时间(以秒为单位),例如:
```
let redTime = 10;
let yellowTime = 3;
let greenTime = 15;
```
4. 接着,我们需要编写一个函数来控制交通灯的变化。该函数首先应该将红灯的 div 元素显示出来,同时将黄灯和绿灯的 div 元素隐藏起来。然后,等待 redTime 秒后,将红灯的 div 元素隐藏起来,将黄灯的 div 元素显示出来,并等待 yellowTime 秒后,将黄灯的 div 元素隐藏起来,将绿灯的 div 元素显示出来,并等待 greenTime 秒后,循环执行此操作。
5. 最后,我们需要调用该函数来启动交通灯的倒计时。可以使用 setInterval 函数来每隔一秒钟执行一次该函数,例如:
```
setInterval(changeLight, 1000);
```
完整代码示例如下:
HTML:
```
<div id="red"></div>
<div id="yellow"></div>
<div id="green"></div>
```
CSS:
```
#red {
width: 50px;
height: 50px;
background-color: red;
border-radius: 50%;
display: none;
}
#yellow {
width: 50px;
height: 50px;
background-color: yellow;
border-radius: 50%;
display: none;
}
#green {
width: 50px;
height: 50px;
background-color: green;
border-radius: 50%;
display: none;
}
```
JavaScript:
```
let redTime = 10;
let yellowTime = 3;
let greenTime = 15;
function changeLight() {
document.getElementById("red").style.display = "block";
document.getElementById("yellow").style.display = "none";
document.getElementById("green").style.display = "none";
setTimeout(function() {
document.getElementById("red").style.display = "none";
document.getElementById("yellow").style.display = "block";
document.getElementById("green").style.display = "none";
setTimeout(function() {
document.getElementById("red").style.display = "none";
document.getElementById("yellow").style.display = "none";
document.getElementById("green").style.display = "block";
setTimeout(function() {
changeLight();
}, greenTime * 1000);
}, yellowTime * 1000);
}, redTime * 1000);
}
setInterval(changeLight, 1000);
```