“Toggle"css颜色与纯Javascript
"Toggle" css colours with pure Javascript
我正在制作一个简单的3行益智游戏,我理解其中的逻辑,但我一直在尝试创建我的网格。
我要做的是改变我的蓝色瓷砖的背景颜色为白色时,一个蓝色的正方形被点击,如果相同的瓷砖再次被点击,恢复颜色回到蓝色。我在纯javascript中做这个
我的问题:我希望能够尽可能多地改变颜色,我目前仅限于改变蓝色->白色,然后白色->蓝色,没有进一步的改变。从本质上讲,这会让用户陷入第三个选择。
我可以做些什么改变来防止在点击x次后颜色变化停止?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Grid</title>
<style>
#blueTile {
background-color:blue;
}
td{
text-align: center;
border: 1px solid black;
padding: 3px;
height:50px;
width:50px;
}
table {
border-collapse: collapse;
}
</style>
</head>
body>
<script>
//6x6 array
var solutionArray = new Array(6);
solutionArray[0] = new Array(6);
solutionArray[1] = new Array(6);
solutionArray[2] = new Array(6);
solutionArray[3] = new Array(6);
solutionArray[4] = new Array(6);
solutionArray[5] = new Array(6);
var tile = {};
var blue = tile.colour = "blue";
var white = tile.colour = "white";
var grey = tile.colour = "grey";
solutionArray[0][0]=blue;
solutionArray[0][1]=white;
solutionArray[0][2]=blue;
solutionArray[0][3]=blue;
solutionArray[0][4]=white;
solutionArray[0][5]=blue;
solutionArray[1][0]=white;
solutionArray[1][1]=blue;
solutionArray[1][2]=white;
solutionArray[1][3]=blue;
solutionArray[1][4]=blue;
solutionArray[1][5]=white;
solutionArray[2][0]=blue;
solutionArray[2][1]=white;
solutionArray[2][2]=blue;
solutionArray[2][3]=white;
solutionArray[2][4]=white;
solutionArray[2][5]=blue;
solutionArray[3][0]=white;
solutionArray[3][1]=blue;
solutionArray[3][2]=white;
solutionArray[3][3]=white;
solutionArray[3][4]=blue;
solutionArray[3][5]=blue;
solutionArray[4][0]=blue;
solutionArray[4][1]=blue;
solutionArray[4][2]=white;
solutionArray[4][3]=blue;
solutionArray[4][4]=white;
solutionArray[4][5]=white;
solutionArray[5][0]=blue;
solutionArray[5][1]=white;
solutionArray[5][2]=blue;
solutionArray[5][3]=white;
solutionArray[5][4]=blue;
solutionArray[5][5]=white;
var x = document.createElement("TABLE");
x.setAttribute("id", "gridTable");
document.body.appendChild(x);
for(i=0;i<6;i++)
{
//output the row tag
var y = document.createElement("TR");
y.setAttribute("id", "row"+i);
document.getElementById("gridTable").appendChild(y)
for(j=0;j<solutionArray.length;j++)
{
///output the td tag
var z = document.createElement("TD");
if(solutionArray[i][j] == blue){
z.setAttribute("id", "blueTile");
}
else if (solutionArray[i][j] == white){
z.setAttribute("id", "whiteTile");
}
var t = document.createTextNode(solutionArray[i][j]);
z.appendChild(t);
document.getElementById("row"+i).appendChild(z);
}
}
var blueClick = document.getElementById("blueTile");
blueClick.addEventListener("click", switchColor);
function switchColor(){
blueClick.style.backgroundColor = "white";
blueClick.addEventListener("click", switchBack);
}
function switchBack(){
blueClick.style.backgroundColor = "blue";
blueClick.addEventListener("click", switchColor);
}
</script>
</body>
</html>
您可能想要改变切换的方式。不需要有两个独立的函数,只需为单个单击处理程序执行以下操作,而不需要单击任何tile。
function switchColor(e){
var curColor = this.style.backgroundColor;
if(curColor === "white"){
this.style.backgroundColor = "blue";
}
else{
this.style.backgroundColor = "white";
}
}
同样,ID对于元素来说应该是唯一的。在你的例子中,你像使用css类一样使用它们。下面是它的一个实现。https://jsfiddle.net/6su0b0w8/
你可以这样做。
//6x6 array
var solutionArray = new Array(6);
solutionArray[0] = new Array(6);
solutionArray[1] = new Array(6);
solutionArray[2] = new Array(6);
solutionArray[3] = new Array(6);
solutionArray[4] = new Array(6);
solutionArray[5] = new Array(6);
var tile = {};
var z = "";
var blue = tile.colour = "blue";
var white = tile.colour = "white";
var grey = tile.colour = "grey";
solutionArray[0][0] = blue;
solutionArray[0][1] = white;
solutionArray[0][2] = blue;
solutionArray[0][3] = blue;
solutionArray[0][4] = white;
solutionArray[0][5] = blue;
solutionArray[1][0] = white;
solutionArray[1][1] = blue;
solutionArray[1][2] = white;
solutionArray[1][3] = blue;
solutionArray[1][4] = blue;
solutionArray[1][5] = white;
solutionArray[2][0] = blue;
solutionArray[2][1] = white;
solutionArray[2][2] = blue;
solutionArray[2][3] = white;
solutionArray[2][4] = white;
solutionArray[2][5] = blue;
solutionArray[3][0] = white;
solutionArray[3][1] = blue;
solutionArray[3][2] = white;
solutionArray[3][3] = white;
solutionArray[3][4] = blue;
solutionArray[3][5] = blue;
solutionArray[4][0] = blue;
solutionArray[4][1] = blue;
solutionArray[4][2] = white;
solutionArray[4][3] = blue;
solutionArray[4][4] = white;
solutionArray[4][5] = white;
solutionArray[5][0] = blue;
solutionArray[5][1] = white;
solutionArray[5][2] = blue;
solutionArray[5][3] = white;
solutionArray[5][4] = blue;
solutionArray[5][5] = white;
var x = document.createElement("TABLE");
x.setAttribute("id", "gridTable");
document.body.appendChild(x);
for (i = 0; i < 6; i++) {
//output the row tag
var y = document.createElement("TR");
y.setAttribute("id", "row" + i);
document.getElementById("gridTable").appendChild(y)
for (j = 0; j < solutionArray.length; j++) {
///output the td tag
var z = document.createElement("TD");
if (solutionArray[i][j] == blue) {
z.setAttribute("class", "blueTile tile blue");
} else if (solutionArray[i][j] == white) {
z.setAttribute("class", "whiteTile tile");
}
var t = document.createTextNode(solutionArray[i][j]);
z.appendChild(t);
document.getElementById("row" + i).appendChild(z);
}
}
document.querySelector("#gridTable").addEventListener("click", function(event) {
if (event.target.tagName === "TD" && event.target.classList.contains("blueTile")) {
event.target.classList.toggle("blue");
}
});
.blue {
background-color: blue;
}
td {
text-align: center;
border: 1px solid black;
padding: 3px;
height: 50px;
width: 50px;
}
table {
border-collapse: collapse;
}
.tile {
cursor: pointer;
}
.whiteTile {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
cursor: default;
}
相关文章:
- 我们如何使用css或JavaScript在i/j上更改句点(点)的颜色
- Javascript对象类在单击时打开窗口进行颜色选择,并在更改时替换对象背景颜色
- 使用CSS/JavaScript更改剑道图工具提示文本颜色
- 比较javascript中的浮动并更改最高浮动的颜色
- Javascript通过列表项的函数和css来更改背景颜色
- Javascript没有从数组中选择背景颜色
- 如何更改编辑单元格上方单元格的背景颜色(Javascript、Google电子表格)
- 只使用一个整数表示颜色-Javascript
- 更改表格字体颜色Javascript
- 在表格中应用动态行颜色 Javascript/VBscript
- 背景更改时动态更改文本颜色(javascript)
- 更改文本链接颜色 JavaScript 或 CSS
- 两个选择的rgb颜色之间的随机颜色(JavaScript)
- 改变边框颜色Javascript
- 更改所有文档链接颜色Javascript
- 从数组中选择随机背景颜色.Javascript / html
- 改变颜色JavaScript不起作用
- 生成两种颜色之间的颜色javascript
- 我正在尝试生成一个随机颜色(javascript),并在单击按钮时将结果应用于按钮.如何将JS与Jquery结合起来
- 按特定顺序更改背景颜色 JavaScript