2017-05-10 58 views
0

我有下面的代码,我需要它自动工作,无需用户输入。此外,它应该在一个循环中,而不是通过一次序列。 我试图改变代码,但它所做的一切都不起作用,或者改变了交通灯的形状。 谢谢。没有按钮的交通灯颜色变化

<!DOCTYPE html> 
<html> 
<head> 
    <style> 

    </style> 
    <script src="/scripts/snippet-javascript-console.min.js?v=1"></script> 
    </head> 
<body> 
    <!DOCTYPE html> 
<html> 
<head> 
    <style> 
     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 { 
    width: 150px; 
    float: left; 
    background-color: #333; 
    border-radius: 40px; 
    margin: 30px 0; 
    padding: 20px; 
} 

.lightbulb { 
    height: 100px; 
    width: 100px; 
    background-color: #111; 
    border-radius: 50%; 
    margin: 25px auto; 
    transition: background 500ms; 
} 

#controlPanel>h1 { 
    margin-top: 30px; 
    margin-bottom: 30px; 
} 
    </style> 
    <script src="/scripts/snippet-javascript-console.min.js?v=1"></script> 
    </head> 
<body> 
    <div id="controlPanel"> 
    <h1 id="autoLights" class="button">Auto</h1> 
</div> 

<div id="traffic-light"> 
    <div id="stopLight" class="lightbulb"></div> 
    <div id="slowLight" class="lightbulb"></div> 
    <div id="goLight" class="lightbulb"></div> 
</div> 
    <script type="text/javascript"> 
document.getElementById('autoLights').onclick = autoLights; 

function stopRed() { 
    Lights(); 
    document.getElementById('stopLight').style.backgroundColor = "red"; 
} 

function slowYellow() { 
    Lights(); 
    document.getElementById('slowLight').style.backgroundColor = "orange"; 
} 

function goGreen() { 
    Lights(); 
    document.getElementById('goLight').style.backgroundColor = "green"; 
} 


function Lights() { 
    document.getElementById('stopLight').style.backgroundColor = "black"; 
    document.getElementById('slowLight').style.backgroundColor = "black"; 
    document.getElementById('goLight').style.backgroundColor = "black"; 
} 


function lightOne(num) { 
    Lights(); 
    switch (num) { 
    case 1: 
     stopRed(); 
     break; 
    case 2: 
     slowYellow(); 
     break; 
    case 3: 
     goGreen(); 
     break; 
    default: 
     alert("you made some error"); 
    } 
} 

counter = 0; 
maxSec = 3; 

function timer() { 
    setTimeout(function() { 

    counter++; 
    lightOne(counter); 
    if (counter == maxSec) { 
     return; 
    } 
    timer(); 
    }, 2000); 
} 

function autoLights() { 
    counter = 1; 
    lightOne(counter); 
    timer(); 
} 
    </script> 
</body> 
</html> 
    <script type="text/javascript"> 

    </script> 
</body> 
</html> 

代码应该是像这样的顺序和现在按钮。但是我不能将它与第一个代码做成相同的形状。

<!DOCTYPE html> 
<html> 
<head> 
    <style> 
     .traffic-light { 
    height: 40px; 
    width: 40px; 
    margin: 1px; 
    background-color: black; 
    border-radius: 20px; 
    border: solid 1px #000 
} 

.surround { 
    padding: 10px; 
    background-color: grey; 
    display: inline-block; 
} 
    </style> 
    <script src="/scripts/snippet-javascript-console.min.js?v=1"></script> 
    </head> 
<body> 
    <!DOCTYPE html> 
<html lang="en"> 


<body> 
    <div id="wrapper"> 
    <div class="surround"> 
     <div id="red-light" class="traffic-light"></div> 
     <div id="yellow-light" class="traffic-light"></div> 
     <div id="green-light" class="traffic-light"></div> 
    </div> 
    </div> 
</body> 

</html> 
    <script type="text/javascript"> 
     function trafficLights() { 
    var sequenceData = [ 
     [5, 1, 0, 0], 
     [2, 1, 1, 0], 
     [5, 0, 0, 1], 
     [3, 0, 1, 0] 
    ], 
    lights = [], 
    index = 0; 

    for (var i = 0, elemId; 
    (elemId = arguments[i]); i++) 
    lights[i] = document.getElementById(elemId); 

    function display() { 
    if (index >= sequenceData.length) 
     index = 0; 

    for (var i = 0, cv, dLen = lights.length; i < dLen; i++) 
     lights[i].style.backgroundColor = (sequenceData[index][i + 1] ? lights[i].id.match(/^[a-z]+/i).toString() : '#000'); 

    setTimeout(display, sequenceData[index++][0] * 977); 
    } 

    display(); 
} 


window.onload = function() { 
    trafficLights("red-light", "yellow-light", "green-light"); 
}; 
    </script> 
</body> 
</html> 
+2

正在运行的jsfiddle将不胜感激 –

+0

什么 '的jsfiddle'? –

+1

什么是'Google'? –

回答

0

我想你可以尝试这样的事:

function stopRed() { 
 
    Lights(); 
 
    document.getElementById('stopLight').style.backgroundColor = "red"; 
 
} 
 

 
function slowYellow() { 
 
    Lights(); 
 
    document.getElementById('slowLight').style.backgroundColor = "orange"; 
 
} 
 

 
function goGreen() { 
 
    Lights(); 
 
    document.getElementById('goLight').style.backgroundColor = "green"; 
 
} 
 

 

 
function Lights() { 
 
    document.getElementById('stopLight').style.backgroundColor = "black"; 
 
    document.getElementById('slowLight').style.backgroundColor = "black"; 
 
    document.getElementById('goLight').style.backgroundColor = "black"; 
 
} 
 

 

 
function lightOne(num) { 
 
    Lights(); 
 
    switch (num) { 
 
    case 1: 
 
     stopRed(); 
 
     break; 
 
    case 2: 
 
     slowYellow(); 
 
     break; 
 
    case 3: 
 
     goGreen(); 
 
     break; 
 
    default: 
 
     alert("you made some error"); 
 
    } 
 
} 
 

 
counter = 0; 
 
maxSec = 3; 
 

 
function timer() { 
 
    setTimeout(function() { 
 

 
    counter++; 
 
    if (counter == maxSec + 1) { 
 
     return autoLights(); 
 
    } 
 
    lightOne(counter); 
 
    
 
    timer(); 
 
    }, 2000); 
 
} 
 

 
function autoLights() { 
 
    counter = 1; 
 
    lightOne(counter); 
 
    timer(); 
 
} 
 

 
autoLights();
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 { 
 
    width: 150px; 
 
    float: left; 
 
    background-color: #333; 
 
    border-radius: 40px; 
 
    margin: 30px 0; 
 
    padding: 20px; 
 
} 
 

 
.lightbulb { 
 
    height: 100px; 
 
    width: 100px; 
 
    background-color: #111; 
 
    border-radius: 50%; 
 
    margin: 25px auto; 
 
    transition: background 500ms; 
 
} 
 

 
#controlPanel>h1 { 
 
    margin-top: 30px; 
 
    margin-bottom: 30px; 
 
}
<div id="traffic-light"> 
 
    <div id="stopLight" class="lightbulb"></div> 
 
    <div id="slowLight" class="lightbulb"></div> 
 
    <div id="goLight" class="lightbulb"></div> 
 
</div>

+0

不应该有一个按钮,黄色也应该在红色之后工作。我为这个问题增加了一个新的代码。 –

+0

@anonymoususer你可以检查我的新帖子,我已经编辑了代码 –

0

喜用下面的代码替换你的脚本。

//document.getElementById('autoLights').onclick = autoLights; 

function stopRed() { 
    Lights(); 
    document.getElementById('stopLight').style.backgroundColor = "red"; 
} 

function slowYellow() { 
    Lights(); 
    document.getElementById('slowLight').style.backgroundColor = "orange"; 
} 

function goGreen() { 
    Lights(); 
    document.getElementById('goLight').style.backgroundColor = "green"; 
} 


function Lights() { 
    document.getElementById('stopLight').style.backgroundColor = "black"; 
    document.getElementById('slowLight').style.backgroundColor = "black"; 
    document.getElementById('goLight').style.backgroundColor = "black"; 
} 


function lightOne(num) { 
    Lights(); 
    switch (num) { 
    case 1: 
     stopRed(); 
     break; 
    case 2: 
     slowYellow(); 
     break; 
    case 3: 
     goGreen(); 
     break; 
    default: 
     alert("you made some error"); 
    } 
} 

counter = 0; 
maxSec = 3; 

function timer() { 
    setTimeout(function() { 

    counter++; 
    lightOne(counter); 
    if (counter == maxSec) { 
     counter = 0; 
     //return; 
    } 
    timer(); 
    }, 2000); 
} 

function autoLights() { 
    counter = 1; 
    lightOne(counter); 
    timer(); 
} 
autoLights(); // to auto call lighting. 
0

使用setInterval而不是setTimeout

document.getElementById('autoLights').onclick = autoLights; 
 
autoLights(); 
 

 
var call = ['stopRed','slowYellow','goGreen']; 
 
function stopRed() { 
 
    Lights(); 
 
    document.getElementById('stopLight').style.backgroundColor = "red"; 
 
} 
 

 
function slowYellow() { 
 
    Lights(); 
 
    document.getElementById('slowLight').style.backgroundColor = "orange"; 
 
} 
 

 
function goGreen() { 
 
    Lights(); 
 
    document.getElementById('goLight').style.backgroundColor = "green"; 
 
} 
 

 

 
function Lights() { 
 
    document.getElementById('stopLight').style.backgroundColor = "black"; 
 
    document.getElementById('slowLight').style.backgroundColor = "black"; 
 
    document.getElementById('goLight').style.backgroundColor = "black"; 
 
} 
 

 

 
var count = 0; 
 

 
function timer() { 
 
    setInterval(function() { 
 
    count = count % 3; 
 
    window[call[count]](); 
 
    count++; 
 
    }, 1000); 
 
} 
 

 
function autoLights() { 
 
    timer(); 
 
}
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 { 
 
    width: 150px; 
 
    float: left; 
 
    background-color: #333; 
 
    border-radius: 40px; 
 
    margin: 30px 0; 
 
    padding: 20px; 
 
} 
 

 
.lightbulb { 
 
    height: 100px; 
 
    width: 100px; 
 
    background-color: #111; 
 
    border-radius: 50%; 
 
    margin: 25px auto; 
 
    transition: background 500ms; 
 
} 
 

 
#controlPanel>h1 { 
 
    margin-top: 30px; 
 
    margin-bottom: 30px; 
 
}
<script src="/scripts/snippet-javascript-console.min.js?v=1"></script> 
 
<script src="/scripts/snippet-javascript-console.min.js?v=1"></script> 
 

 
<div id="controlPanel"> 
 
    <h1 id="autoLights" class="button">Auto</h1> 
 
</div> 
 

 
<div id="traffic-light"> 
 
    <div id="stopLight" class="lightbulb"></div> 
 
    <div id="slowLight" class="lightbulb"></div> 
 
    <div id="goLight" class="lightbulb"></div> 
 
</div>