gpt4 book ai didi

javascript - 控制台不会记录 div 数组

转载 作者:行者123 更新时间:2023-12-02 23:50:08 27 4
gpt4 key购买 nike

我正在制作一个四连线游戏,现在我正在尝试记录单击该列时位于棋盘特定列中的每个 div 的数组。我能够从第一个索引到最后一个索引记录数组。现在我不想记录从最后一个索引到第一个索引的数组。我的函数如下所示。

`function dropChip(col) {
for(let i = col.length-1; i < col.length; i--) {
console.log(col[i]);
}
}`

令我困惑的是我可以去 console.log(col);并查看所有六个项目。但我不能从反向看它们?(因为我需要检查底部 div 是否为空,因为芯片需要堆叠。另外,使用 DOM 进行游戏逻辑是一个好主意吗?我是这样做的我制作的以前的版本被告知这是糟糕的设计,但我不知道其他方法或如何用 JS 做到这一点)以这种方式查看它们会破坏游戏吗?关于我做错了什么有什么想法吗?谢谢:)

const board = document.querySelectorAll(".slot");

for(let i = 0; i < board.length; i++) {
board[i].addEventListener('click', selectColumn);
}

function selectColumn(evt) {
let column = evt.target.id;
let columnArray = [];

for(let i=0; i < board.length; i++) {
if(column === board[i].id) columnArray.push(board[i]);
}
dropChip(columnArray);
}

function dropChip(col) {
for(let i = col.length-1; i < col.length; i--) {
console.log(col[i]);
}
}
/** {
border: 1px solid red;
}*/

*, *:before, *:after {
box-sizing: inherit;
}

html {
box-sizing: border-box;
}

html, body {
margin: 0;
padding: 0;
}

body {
display: flex;
justify-content: flex-end;
min-height: 100vh;
background-color: #e6e9ef;
flex-direction: column;
}

.board-wrapper {
/*display: flex;*/
margin: 0 auto;
padding-top: 100px;
position: relative;
overflow: hidden;
}

.board {
display: flex;
background-color: #00c;
flex-wrap: wrap;
max-width: 706px;
padding: 3px;
/*margin: auto;*/
}

.board > div {
width: 100px;
height: 100px;
background-color: blue;
border: 3px solid #00c;
cursor: pointer;
position: relative;
}

.board > div:before {
content: "";
position: absolute;
top: 7px;
left: 7px;
background-color: #00c;
width: 80px;
height: 80px;
border-radius: 50%;
box-shadow: inset 0px 0px 13px #0606aa;

}

.board > div:after {
content: "";
position: absolute;
z-index: 99;
left: 0;
right: 0;
top: -999em;
bottom: -999em;
transition: all .5s ease;
background-color: transparent;
}

.board > div:hover:after {
background-color: #00c;
}

.board > div.counter {
/*display: block;
padding: 0;*/
position: absolute;
top: 0;
left: 0;
right: 0;
width: auto;
height: 100px;
border: none;
background-color: #e6e9ef;
z-index: 100;
}

.board > div.counter:after {
/*content: "";*/
/*display: block;*/
/*padding: 0;*/
height: 80px;
width: 80px;
background-color: red;
top: 0;
/*left: 0;
right: auto;*/
border-radius: 50%;
transition: transform .5s ease, opacity .2s ease;
/*transform: translateX(0);*/
z-index: 100;
opacity: 0;
}

.board > div.counter:before {
content: "";
display: block;
padding: 0;
position: absolute;
top: 15px;
left: 15px;
right: auto;
height: 50px;
width: 50px;
border-radius: 50%;
background-color: red;
box-shadow: inset 0px 0px 20px #cc0000;
z-index: 101;
transition: transform .5s ease, opacity .2s ease;
/*transform: translateX(0);*/
opacity: 0;
}

.board > div:nth-of-type(7n+1):hover ~ .counter:after{transform:translateX(14px);opacity: 1;}
.board > div:nth-of-type(7n+2):hover ~ .counter:after{transform:translateX(114px);opacity: 1;}
.board > div:nth-of-type(7n+3):hover ~ .counter:after{transform:translateX(214px);opacity: 1;}
.board > div:nth-of-type(7n+4):hover ~ .counter:after{transform:translateX(314px);opacity: 1;}
.board > div:nth-of-type(7n+5):hover ~ .counter:after{transform:translateX(414px);opacity: 1;}
.board > div:nth-of-type(7n+6):hover ~ .counter:after{transform:translateX(514px);opacity: 1;}
.board > div:nth-of-type(7n+7):hover ~ .counter:after{transform:translateX(614px);opacity: 1;}

.board > div:nth-of-type(7n+1):hover ~ .counter:before{transform:translateX(14px);opacity: 1;}
.board > div:nth-of-type(7n+2):hover ~ .counter:before{transform:translateX(114px);opacity: 1;}
.board > div:nth-of-type(7n+3):hover ~ .counter:before{transform:translateX(214px);opacity: 1;}
.board > div:nth-of-type(7n+4):hover ~ .counter:before{transform:translateX(314px);opacity: 1;}
.board > div:nth-of-type(7n+5):hover ~ .counter:before{transform:translateX(414px);opacity: 1;}
.board > div:nth-of-type(7n+6):hover ~ .counter:before{transform:translateX(514px);opacity: 1;}
.board > div:nth-of-type(7n+7):hover ~ .counter:before{transform:translateX(614px);opacity: 1;}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Connect Four</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="board-wrapper">
<div class="board">
<div class="slot" id="col-1"></div>
<div class="slot" id="col-2"></div>
<div class="slot" id="col-3"></div>
<div class="slot" id="col-4"></div>
<div class="slot" id="col-5"></div>
<div class="slot" id="col-6"></div>
<div class="slot" id="col-7"></div>
<div class="slot" id="col-1"></div>
<div class="slot" id="col-2"></div>
<div class="slot" id="col-3"></div>
<div class="slot" id="col-4"></div>
<div class="slot" id="col-5"></div>
<div class="slot" id="col-6"></div>
<div class="slot" id="col-7"></div>
<div class="slot" id="col-1"></div>
<div class="slot" id="col-2"></div>
<div class="slot" id="col-3"></div>
<div class="slot" id="col-4"></div>
<div class="slot" id="col-5"></div>
<div class="slot" id="col-6"></div>
<div class="slot" id="col-7"></div>
<div class="slot" id="col-1"></div>
<div class="slot" id="col-2"></div>
<div class="slot" id="col-3"></div>
<div class="slot" id="col-4"></div>
<div class="slot" id="col-5"></div>
<div class="slot" id="col-6"></div>
<div class="slot" id="col-7"></div>
<div class="slot" id="col-1"></div>
<div class="slot" id="col-2"></div>
<div class="slot" id="col-3"></div>
<div class="slot" id="col-4"></div>
<div class="slot" id="col-5"></div>
<div class="slot" id="col-6"></div>
<div class="slot" id="col-7"></div>
<div class="slot" id="col-1"></div>
<div class="slot" id="col-2"></div>
<div class="slot" id="col-3"></div>
<div class="slot" id="col-4"></div>
<div class="slot" id="col-5"></div>
<div class="slot" id="col-6"></div>
<div class="slot" id="col-7"></div>
<div class="counter"></div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>

最佳答案

您想要更改 for 循环:

for (let i = 0; i < col.length; i++) {...}

关于javascript - 控制台不会记录 div 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55684646/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com