2016-05-18 2 views
4

Wie kann ich diese Ampelschaltung machen wiederholen mehr als einmal, wenn die Taste

document.getElementById('AllButton').onclick = switchAll; 
 

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

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

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

 
function illuminateRedOrange() { 
 
    clearLights(); 
 
    document.getElementById('stopLight').style.backgroundColor = "red"; 
 
    document.getElementById('slowLight').style.backgroundColor = "orange"; 
 
} 
 

 
function illuminateBlack() { 
 
    clearLights(); 
 

 
} 
 

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

 
var clickTimes = 0; 
 
var change = 1; 
 

 
function switchAll() { 
 
    clickTimes++; 
 
    switch (clickTimes) { 
 
    case 1: 
 
     clearLights(); 
 
     document.getElementById('stopLight').style.backgroundColor = "red"; 
 
     break; 
 
    case 2: 
 
     clearLights(); 
 
     document.getElementById('stopLight').style.backgroundColor = "red"; 
 
     document.getElementById('slowLight').style.backgroundColor = "orange"; 
 
     break; 
 
    case 3: 
 
     clearLights(); 
 
     document.getElementById('goLight').style.backgroundColor = "green"; 
 
     break; 
 
    case 4: 
 
     clearLights(); 
 
     document.getElementById('slowLight').style.backgroundColor = "orange"; 
 
     break; 
 
    case 5: 
 
     clearLights(); 
 
     document.getElementById('stopLight').style.backgroundColor = "red"; 
 
     break; 
 
    case 6: 
 
     document.getElementById('stopLight').style.backgroundColor = "black"; 
 
     document.getElementById('slowLight').style.backgroundColor = "black"; 
 
     document.getElementById('goLight').style.backgroundColor = "black"; 
 
     break; 
 

 
    } 
 
}
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="AllButton" class="button">Switch</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>
> gedrückt wird

Das für meine ICT Kurs ist, müssen wir ein Ampelsystem und mein Code bauen arbeitet bisher. Sobald ich jedoch den Knopf auf dem Bildschirm gedrückt habe und jeder Klick durch die möglichen Kombinationen getaktet hat, hat die Ampel ihre Sequenz beendet, sie wird sich nicht wiederholen. Ich frage mich, ob jemand mir helfen könnte, es in die Schleife zu bringen, ich würde es danken, danke, ich bin neu im Codieren, es tut mir so leid, wenn es ausgeschaltet ist.

+0

So wissen Sie, wie Sie die Funktion aufrufen, die den Zyklus beginnt. Haben Sie daran gedacht, die Funktion am Ende der Sequenz noch einmal aufzurufen? Durch Ausführen von 'switchAll()'? – Glubus

+0

Ich würde vorschlagen, ein Array von Farben zu verwenden und dann einfach durch diese Farben in einer Funktion mit einem 'window.setInterval()' Ansatz zu iterieren. Und, ganz ehrlich, das Löschen von JavaScript und das Verwenden von CSS-Animationen (es sei denn, JavaScript ist aus irgendeinem Grund erforderlich). –

+0

Sie arbeiten zweimal! Rufen Sie in Ihren 'switch case' die Funktionen auf, die Sie für jeden Status definiert haben. Viel weniger Code. – C14L

Antwort

5

Schreiben Sie einfach clickTimes = 0; vor dem letzten break.

Dies ist die Geige: https://jsfiddle.net/7oa28cta/

+0

Oh mein Gott, ich bin so dumm, vielen Dank. – ABinning

+1

@ABinning Lernen Sie, eine Antwort zu akzeptieren. Halten Sie sich links daneben und Sie sehen ein Häkchen. Klicken Sie einmal darauf und es wird grün. Der Beantworter bekommt 15 Punkte, du bekommst 2. Macht Leute wie dich! –

+0

Danke, @RachelGallen. –

1

Statt die Hintergrundfarbe zu ändern mit Javascript Sie das Klassenattribut des Elements ändern sollen, z.B. für Ihr Element mit der ID "Stopplicht":

document.getElementById("stopLight").className = "blink-several-times"; 

Dann CSS-Animationen auf den Klassen statt CSS-Übergänge verwenden Sie einstellen, zB:

.blink-several-times { 
    animation: myAnimation linear 4s; 
    animation-iteration-count: infinite; 
    transform-origin: 50% 50%; 
    -webkit-animation: myAnimation linear 4s; 
    -webkit-animation-iteration-count: infinite; 
    -webkit-transform-origin: 50% 50%; 
    -moz-animation: myAnimation linear 4s; 
    -moz-animation-iteration-count: infinite; 
    -moz-transform-origin: 50% 50%; 
    -o-animation: myAnimation linear 4s; 
    -o-animation-iteration-count: infinite; 
    -o-transform-origin: 50% 50%; 
    -ms-animation: myAnimation linear 4s; 
    -ms-animation-iteration-count: infinite; 
    -ms-transform-origin: 50% 50%; 
} 

@keyframes myAnimation{ 
    0% { 
    background-color: rgba(0, 128, 0, 0); 
    } 
    100% { 
    background-color: rgba(0, 128, 0, 1); 
    } 
} 

@-moz-keyframes myAnimation{ 
    0% { 
    -moz-transform: translate(0px,0px) rotate(0deg) ; 
    } 
    100% { 
    -moz-transform: translate(200px,0px) rotate(180deg) ; 
    } 
} 

@-webkit-keyframes myAnimation { 
    0% { 
    -webkit-transform: translate(0px,0px) rotate(0deg) ; 
    } 
    100% { 
    -webkit-transform: translate(200px,0px) rotate(180deg) ; 
    } 
} 

@-o-keyframes myAnimation { 
    0% { 
    -o-transform: translate(0px,0px) rotate(0deg) ; 
    } 
    100% { 
    -o-transform: translate(200px,0px) rotate(180deg) ; 
    } 
} 

@-ms-keyframes myAnimation { 
    0% { 
    -ms-transform: translate(0px,0px) rotate(0deg) ; 
    } 
    100% { 
    -ms-transform: translate(200px,0px) rotate(180deg) ; 
    } 
} 

Die Zeilen mit "Animation-Iteration-count" set die Anzahl der Iterationen. Wenn es "unendlich" ist, wird die Animation endlos wiederholt.

Die vielen verschiedenen Zeilen -webkit, -o, -moz usw. werden für die browserübergreifende Unterstützung benötigt.

CSS-Animation Code erzeugen, empfehle ich eine CSS-Animation Generator wie http://cssanimate.com/

0

Der einfachste Weg, verwenden Sie dies tun können, ist von:

case 6: 
     document.getElementById('stopLight').style.backgroundColor = "black"; 
     document.getElementById('slowLight').style.backgroundColor = "black"; 
     document.getElementById('goLight').style.backgroundColor = "black"; 
     clickTimes = 1; 
     break; 

Allerdings würde ich darüber nachdenken, dies zu tun ist eine effizientere So wie bei der Verwendung einer for-Schleife, um dies zu erreichen, ohne jedes Mal manuell zu klicken.

1

Short Lösung

var traffic_button = document.querySelector('.traffic-button'); 
 

 
if (traffic_button) { 
 
    traffic_button.addEventListener('click', traffic, false); 
 
} 
 

 
var clicks = 0; 
 

 
function traffic() { 
 
    var lights = document.querySelectorAll(this.dataset.lights + ' .bulb'); 
 

 
    if (lights.length !== clicks) { 
 
    switch (clicks) { 
 
     case 0: 
 
     offLights(); 
 
     lights[clicks].classList.add('active'); 
 
     clicks++; 
 
     break; 
 
     case 1: 
 
     offLights(); 
 
     lights[clicks - 1].classList.add('active'); 
 
     lights[clicks].classList.add('active'); 
 
     clicks++; 
 
     break; 
 
     case 2: 
 
     offLights(); 
 
     lights[clicks].classList.add('active'); 
 
     clicks++; 
 
     break; 
 
     default: 
 
     break; 
 
    } 
 
    } else { 
 
    offLights(); 
 
    clicks = 0; 
 
    } 
 

 
    function offLights() { 
 
    for (var i = 0; i < lights.length; i++) { 
 
     var element = lights[i]; 
 
     if (element.classList.contains('active')) { 
 
     element.classList.remove('active'); 
 
     } 
 
    } 
 
    } 
 
}
#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; 
 
} 
 
.bulb.red.active { 
 
    background: red; 
 
} 
 
.bulb.yellow.active { 
 
    background: yellow; 
 
} 
 
.bulb.green.active { 
 
    background: green; 
 
}
<div id="controlPanel"> 
 
    <h1 id="button" data-lights="#traffic-light" class="traffic-button button">Switch</h1> 
 
</div> 
 
<div id="traffic-light"> 
 
    <div class="bulb red"></div> 
 
    <div class="bulb yellow"></div> 
 
    <div class="bulb green"></div> 
 
</div>

Verwandte Themen