<section class="game--section">
        <div class="game-space">
            <div class="box box1 active-box"></div>
            <div class="box box2"></div>
            <div class="box box3"></div>
            <div class="box box4"></div>
            <div class="box box5"></div>
            <div class="box box6"></div>
        </div>
        <div class="restart-button">?</div>
    </section>

我有一个包含6个子框的div(.Game-space),其中一个是具有绿色背景的(‘.active-Box)类的活动框,

  1. 所以当我点击活动框时,将它的类(.active-Box)移到下一个给出绿色背景的框,这样它就会一直移到最后一个框.

我也有重新开始按钮,当我点击它时,游戏应该会重新开始,就像活动框类被标记到第一个框一样,从这里它再次转移到下一个类,并通过点击一直到结束框.

但是,在点击重新开始按钮之前,框之间的移动是线性的,逐个移动,但在点击重新启动按钮后,框是被跳过的,而不是在线性移动之间跳过一些框, 为什么?有什么解决办法吗?

enter image description here

'use strict'
const allBox = document.querySelectorAll('.box');
const activeBox = document.querySelector('.active-box')
const restartButton = document.querySelector('.restart-button')
let active_box_index = activeBox_index_function();

slideBox(allBox[active_box_index]);

function slideBox(pass_active_box) {
  pass_active_box.addEventListener('click', function() {
    allBox[active_box_index].classList.remove('active-box')
    allBox[active_box_index + 1].classList.add('active-box')

    active_box_index = activeBox_index_function()
    slideBox(allBox[active_box_index]);
  })
}

console.log(allBox);

// active box index
function activeBox_index_function() {
  let index;
  allBox.forEach((el, indx) => {
    if (el.matches('.active-box')) {
      index = indx
    };
  })
  return index;
}

// when restart button clicked
restartButton.addEventListener('click', function() {
  allBox[active_box_index].classList.remove('active-box')
  allBox[0].classList.add('active-box')

  active_box_index = activeBox_index_function()
})
@import url('https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,200;0,300;0,400;0,600;0,700;0,800;0,900;1,800&display=swap');
@import url('https://fonts.cdnfonts.com/css/common-pixel');
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

html {
  font-size: 62.5%;
  font-family: 'Poppins', sans-serif;
  color: black;
}

.game--section {
  height: 100vh;
  background-color: #252525;
  display: flex;
  justify-content: center;
  align-items: center;
}

.game-space {
  width: 80rem;
  height: 10rem;
  padding: 1rem;
  border-radius: 9px;
  background-color: #f5f5f5;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 1rem;
}

.box {
  background-color: tomato;
}

.active-box {
  background-color: #099268;
  cursor: pointer;
}

.restart-button {
  font-size: 3rem;
  cursor: pointer;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="style.css">
</head>

<body>

  <section class="game--section">
    <div class="game-space">
      <div class="box box1 active-box"></div>
      <div class="box box2"></div>
      <div class="box box3"></div>
      <div class="box box4"></div>
      <div class="box box5"></div>
      <div class="box box6"></div>
    </div>
    <div class="restart-button">?</div>
  </section>

  <script src="script.js"></script>
</body>

</html>

推荐答案

您所描述的问题是由于事件监听程序的积累造成的.

您可以使用以下代码:

'use strict'
const allBox = document.querySelectorAll('.box');
const activeBox = document.querySelector('.active-box')
const restartButton = document.querySelector('.restart-button')
let active_box_index = activeBox_index_function();

slideBox(allBox[active_box_index]);

function myFunction(){
    allBox[active_box_index].classList.remove('active-box');
    if (!((active_box_index + 1)===allBox.length)){
    allBox[active_box_index + 1].classList.add('active-box');
    }
    allBox[active_box_index].removeEventListener('click', myFunction);
    active_box_index = activeBox_index_function();
    slideBox(allBox[active_box_index]);
}
  
function slideBox(pass_active_box) {
  if (!((active_box_index + 1)===allBox.length)){
  pass_active_box.addEventListener('click', myFunction)
  }
}

console.log(allBox);

// active box index
function activeBox_index_function() {
  let index;
  allBox.forEach((el, indx) => {
    if (el.matches('.active-box')) {
      index = indx
    };
  })
  return index;
}

// when restart button clicked
restartButton.addEventListener('click', function() {
  allBox[active_box_index].classList.remove('active-box');
  allBox[0].classList.add('active-box');
  allBox[active_box_index].removeEventListener('click', myFunction);
  allBox[0].addEventListener('click', myFunction);
  active_box_index = activeBox_index_function()
})

更改如下:

以及其他几个错误修复:

  • 仅当事件侦听器不是最后一个框时才传递类并添加事件侦听器.如果需要,您可以将最后一个框之后的第一个框设置为活动框.只需添加一个else语句,然后在其中添加allBox[0].classList.add('active-box');即可. 同样,也不要忘记添加事件侦听器...

希望这篇文章对你有用!

编辑:我刚刚发现,如果我们定义函数,我们不需要添加removeEventListener().这是因为如果该函数已经在该目标的事件侦听器列表中,则不会再次添加该函数.但当我们用匿名函数调用事件侦听器时,它是第二次添加的.请参见this answerMDN Web Docs about addEventListener().

Javascript相关问答推荐

Mongodb拥有5亿个文档,我想根据JavaScript驱动程序中的两个字段使用regEx进行搜索,而不是模式

窗口.getComputedStyle()在MutationObserver中不起作用

被CSS优先级所迷惑

为什么ngModel不能在最后一个版本的Angular 17上工作?'

成功完成Reducers后不更新状态

在grafana情节,避免冲突的情节和传说

在我的html表单中的用户输入没有被传送到我的google表单中

如何用拉威尔惯性Vue依赖下拉?

如何从Intl.DateTimeFormat中仅获取时区名称?

还原器未正确更新状态

更改预请求脚本中重用的JSON主体变量- Postman

在Reaction中的handleSubmit按钮内,useSelector值仍然为空

Webpack在导入前混淆文件名

触发异步函数后不能显示数据

从逗号和破折号分隔的给定字符串中查找所有有效的星期几

令牌JWT未过期

如何修复使用axios运行TSC index.ts时出现的错误?

如何根据查询结果重新排列日期

如何在Web项目中同步语音合成和文本 colored颜色 更改

Rails 7:在不使用导入映射的情况下导入Java脚本