2016-11-06 3 views
0

私はここにいくつかのコードを持っており、正方形が青、緑、藍色になり、最初に戻るようにしたい。ボタンをクリックするたびに色が変わります。ここのコードは青、緑、藍になり、藍と黒の間で変化し、青に戻って欲しい。もう一度コード全体を再起動する方法はありますか?javascriptで複数のスクリプトをループするにはどうすればよいですか?

<--code for animated squares!--> 

<!DOCTYPE html> 
<html> 
<style> 
#container { 
    width: 400px; 
    height: 400px; 
    position: relative; 
    background: Black; 
} 
div#animate { 
    width: 50px; 
    height: 50px; 
    position: absolute; 
    left: 175px; 
    top: 0px; 
    background-color: Blue; 
} 
</style> 

<style> 
#containertwo { 
    width: 400px; 
    height: 400px; 
    position: relative; 
    background: Black; 
} 
div#animatetwo { 
    width: 50px; 
    height: 50px; 
    position: absolute; 
    left: 175px; 
    top: 175px; 
    background-color: Black; 
} 
</style> 
<body> 

<style> 
#containerthree { 
    width: 400px; 
    height: 400px; 
    position: relative; 
    background: Black; 
} 
div#animatethree { 
    width: 50px; 
    height: 50px; 
    position: absolute; 
    left: 175px; 
    top: 350px; 
    background-color: Black; 
} 
</style> 

<body> 

<p> 
<button onClick="button_click();button_clicktwo();button_clickthree()">Change Colour</button> 
</p> 

<div id ="container"> 
<div id ="animate"></div> 
<div id ="animatetwo"></div> 
<div id ="animatethree"></div> 
</div> 

<div id="box" onClick="button_click(j)();"></div> 
<script> 
var colors = ["Black","Black","Blue"]; 
function button_click() { 
    var box = document.getElementById("animate"); 
    var background_color = box.style.backgroundColor; 
    var i = colors.indexOf(background_color); 
    if (i === colors.length-1) { 
     i = -1; 
    } 
    animate.style.backgroundColor = colors[i+1]; 
} 
</script> 

<div id="box" onClick="button_clicktwo();"></div> 
<script> 
var colorstwo = ["Green","Black","Black",]; 
function button_clicktwo() { 
    var box = document.getElementById("animatetwo"); 
    var background_color = box.style.backgroundColor; 
    var i = colorstwo.indexOf(background_color); 
    if (i === colorstwo.length-1) { 
     i = -1; 
    } 
    animatetwo.style.backgroundColor = colorstwo[i+1]; 
} 
</script> 

<div id="box" onClick="button_clickthree();"></div> 
<script> 
var colorsthree = ["Black","Indigo","Black"]; 
function button_clickthree() { 
    var box = document.getElementById("animatethree"); 
    var background_color = box.style.backgroundColor; 
    var i = colorsthree.indexOf(background_color); 
    if (i === colorstwo.length-1) { 
     i = -1; 
    } 
    animatethree.style.backgroundColor = colorsthree[i+1]; 
} 
</script> 

答えて

0

無効で反復的なコードがかなりあります。そのため、わかりやすくするために、私は全体を改訂しました。私は以下のいくつかの問題に注意します。

HTML、CSS、JSには多くの変更が加えられていますので、それらをすべてリストすることはしませんが、違いを観察するために残しておきます。

// Gather the colors and elements, and set a shared `i` to `0` 
 
var colors = ["Blue", "Green", "Indigo"]; 
 
var elems = document.querySelectorAll(".animate"); 
 
var i = 0; 
 

 
// Have a single function that makes the current element black and the next 
 
// one a different color 
 
function button_click() { 
 
    elems[i].style.backgroundColor = "Black"; 
 

 
    if (++i === colors.length) { 
 
    i = 0 
 
    } 
 

 
    elems[i].style.backgroundColor = colors[i]; 
 
}
#container { 
 
    width: 400px; 
 
    height: 400px; 
 
    position: relative; 
 
    background: Black; 
 
} 
 
.animate { 
 
    width: 50px; 
 
    height: 50px; 
 
    left: 175px; 
 
    position: absolute; 
 
} 
 
.animate:nth-child(1) { 
 
    top: 0px; 
 
    background-color: Blue; 
 
} 
 
.animate:nth-child(2) { 
 
    top: 175px; 
 
} 
 
.animate:nth-child(3) { 
 
    top: 350px; 
 
}
<p> 
 
    <button onClick="button_click();">Change Colour</button> 
 
</p> 
 

 
<div id="container"> 
 
    <div class="animate"></div> 
 
    <div class="animate"></div> 
 
    <div class="animate"></div> 
 
</div>


CSSに設定された各色の、そしてJavaScriptでhiddenまたはvisiblevisibilityを設定するように、あまりにもこれを行うには、他の方法があることに注意してください。

一般的な問題のいくつかあった。

  • 同じID属性を超える大幅に削減することができた余分な<body>タグ
  • CSSを繰り返してたくさんの、JSとHTML
  • 後を使用
関連する問題