前端实现红绿灯
时间: 2023-07-07 11:04:38 浏览: 112
可以使用 HTML、CSS 和 JavaScript 来实现一个简单的红绿灯。
首先,在 HTML 中创建一个 div 元素,作为红绿灯的容器。然后,使用 CSS 设置容器的大小、颜色和形状。
```html
<div id="traffic-light"></div>
```
```css
#traffic-light {
width: 100px;
height: 300px;
border-radius: 50%;
background-color: gray;
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
}
```
接下来,在 JavaScript 中,为每个灯泡创建一个 div 元素,并设置它们的初始颜色。然后,定义一个函数来控制灯泡的颜色变化。
```javascript
const colors = {
red: 'red',
yellow: 'yellow',
green: 'green'
};
const trafficLight = document.getElementById('traffic-light');
const redLight = document.createElement('div');
redLight.style.backgroundColor = colors.red;
redLight.classList.add('light');
trafficLight.appendChild(redLight);
const yellowLight = document.createElement('div');
yellowLight.style.backgroundColor = colors.yellow;
yellowLight.classList.add('light');
trafficLight.appendChild(yellowLight);
const greenLight = document.createElement('div');
greenLight.style.backgroundColor = colors.green;
greenLight.classList.add('light');
trafficLight.appendChild(greenLight);
let currentLight = 0;
function changeLight() {
const lights = document.querySelectorAll('.light');
lights[currentLight].style.backgroundColor = colors[Object.keys(colors)[currentLight]];
if (currentLight === 0) {
lights[2].style.backgroundColor = colors.gray;
} else {
lights[currentLight - 1].style.backgroundColor = colors.gray;
}
currentLight = (currentLight + 1) % lights.length;
}
setInterval(changeLight, 2000);
```
最后,我们可以使用 setInterval 函数来定期调用 changeLight 函数,从而控制灯泡的颜色变化。
完整的代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Traffic Light</title>
<style>
#traffic-light {
width: 100px;
height: 300px;
border-radius: 50%;
background-color: gray;
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
}
.light {
width: 60px;
height: 60px;
border-radius: 50%;
}
</style>
</head>
<body>
<div id="traffic-light"></div>
<script>
const colors = {
red: 'red',
yellow: 'yellow',
green: 'green',
gray: 'gray'
};
const trafficLight = document.getElementById('traffic-light');
const redLight = document.createElement('div');
redLight.style.backgroundColor = colors.red;
redLight.classList.add('light');
trafficLight.appendChild(redLight);
const yellowLight = document.createElement('div');
yellowLight.style.backgroundColor = colors.gray;
yellowLight.classList.add('light');
trafficLight.appendChild(yellowLight);
const greenLight = document.createElement('div');
greenLight.style.backgroundColor = colors.gray;
greenLight.classList.add('light');
trafficLight.appendChild(greenLight);
let currentLight = 0;
function changeLight() {
const lights = document.querySelectorAll('.light');
lights[currentLight].style.backgroundColor =
colors[Object.keys(colors)[currentLight]];
if (currentLight === 0) {
lights[2].style.backgroundColor = colors.gray;
} else {
lights[currentLight - 1].style.backgroundColor = colors.gray;
}
currentLight = (currentLight + 1) % lights.length;
}
setInterval(changeLight, 2000);
</script>
</body>
</html>
```
阅读全文