idea 写一个交通信号灯的显示页面
时间: 2023-12-07 08:02:42 浏览: 21
以下是一个交通信号灯的显示页面的代码示例:
```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` 文件格式。在浏览器中打开该文件,你应该可以看到一个交通信号灯的显示页面,并且灯的颜色会按照一定的时间间隔自动切换。