javascript-HTML中的自动灯光更改
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-HTML中的自动灯光更改,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4464字,纯文字阅读大概需要7分钟。
内容图文
![javascript-HTML中的自动灯光更改](/upload/InfoBanner/zyjiaocheng/685/a83232a6482b44d4991822667ce033f1.jpg)
我必须创建一个HTML程序,当用户单击按钮时,该程序会自动更改交通灯中的颜色.
当用户单击三个按钮之一时,我已经完成了交通信号灯的布局,并且颜色发生了变化.
有人可以帮助我,使它们在单击按钮时自动点亮吗?
它应该亮红色(等待2秒),黄色(等待2秒),绿色并重复.
我尝试研究如何执行此操作,但似乎找不到任何解决方案.我发现的全部是如何通过1个按钮进行1种颜色更改.
document.getElementById('stopButton').onclick = stopRed;
document.getElementById('slowButton').onclick = slowYellow;
document.getElementById('goButton').onclick = goGreen;
document.getElementById('clearLights').onclick = clearLights;
document.getElementById('autoLights').onclick = setTimeout(autoLights, 3000);
function stopRed() {
clearLights();
document.getElementById('stopLight').style.backgroundColor = "red";
}
function slowYellow() {
clearLights();
document.getElementById('slowLight').style.backgroundColor = "orange";
}
function goGreen() {
clearLights();
document.getElementById('goLight').style.backgroundColor = "green";
}
function clearLights() {
document.getElementById('stopLight').style.backgroundColor = "black";
document.getElementById('slowLight').style.backgroundColor = "black";
document.getElementById('goLight').style.backgroundColor = "black";
}
function autoLights() {
alert('Hello');
}
body {
font-family: sans-serif;
}
#controlPanel {
float: left;
padding-top: 30px;
}
.button {
background-color: gray;
color: white;
border-radius: 10px;
padding: 20px;
text-align: center;
margin: 90px 40px;
cursor: pointer;
}
#traffic-light {
height: 550px;
width: 200px;
float: left;
background-color: #333;
border-radius: 40px;
margin: 30px 0;
padding: 20px;
}
.bulb {
height: 150px;
width: 150px;
background-color: #111;
border-radius: 50%;
margin: 25px auto;
transition: background 500ms;
}
<html>
<div id="controlPanel">
<h1 id="stopButton" class="button">Stop</h1>
<h1 id="slowButton" class="button">Slow</h1>
<h1 id="goButton" class="button">Go</h1>
<h1 id="clearLights" class="button">Clear</h1>
<h1 id="autoLights" class="button">Auto</h1>
</div>
<div id="traffic-light">
<div id="stopLight" class="bulb"></div>
<div id="slowLight" class="bulb"></div>
<div id="goLight" class="bulb"></div>
</div>
</html>
解决方法:
我更改您的(autoLights)功能
var counter = 2;
var timerId = 0;
document.getElementById('stopButton').onclick = stopRed;
document.getElementById('slowButton').onclick = slowYellow;
document.getElementById('goButton').onclick = goGreen;
document.getElementById('clearLights').onclick = clearLights;
document.getElementById('autoLights').onclick = autoLights;
function stopRed() {
document.getElementById('slowLight').style.backgroundColor = "black";
document.getElementById('goLight').style.backgroundColor = "black";
document.getElementById('stopLight').style.backgroundColor = "red";
}
function slowYellow() {
document.getElementById('stopLight').style.backgroundColor = "black";
document.getElementById('goLight').style.backgroundColor = "black";
document.getElementById('slowLight').style.backgroundColor = "orange";
}
function goGreen() {
document.getElementById('stopLight').style.backgroundColor = "black";
document.getElementById('slowLight').style.backgroundColor = "black";
document.getElementById('goLight').style.backgroundColor = "green";
}
function clearLights() {
document.getElementById('stopLight').style.backgroundColor = "black";
document.getElementById('slowLight').style.backgroundColor = "black";
document.getElementById('goLight').style.backgroundColor = "black";
clearInterval(timerId);
}
function autoLights() {
stopRed();
timerId = setInterval(timer, 2000);
}
function timer() {
if (counter == 1)
stopRed();
if (counter == 2)
slowYellow();
if (counter == 3)
goGreen();
counter++
if (counter > 3)
counter = 1;
}
body {
font-family: sans-serif;
}
#controlPanel {
float: left;
padding-top: 30px;
}
.button {
background-color: gray;
color: white;
border-radius: 10px;
padding: 20px;
text-align: center;
margin: 90px 40px;
cursor: pointer;
}
#traffic-light {
height: 550px;
width: 200px;
float: left;
background-color: #333;
border-radius: 40px;
margin: 30px 0;
padding: 20px;
}
.bulb {
height: 150px;
width: 150px;
background-color: #111;
border-radius: 50%;
margin: 25px auto;
transition: background 500ms;
}
<div id="controlPanel">
<h1 id="stopButton" class="button">Stop</h1>
<h1 id="slowButton" class="button">Slow</h1>
<h1 id="goButton" class="button">Go</h1>
<h1 id="clearLights" class="button">Clear</h1>
<h1 id="autoLights" class="button">Auto</h1>
</div>
<div id="traffic-light">
<div id="stopLight" class="bulb"></div>
<div id="slowLight" class="bulb"></div>
<div id="goLight" class="bulb"></div>
</div>
内容总结
以上是互联网集市为您收集整理的javascript-HTML中的自动灯光更改全部内容,希望文章能够帮你解决javascript-HTML中的自动灯光更改所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。