You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

游戏弹窗功能故障求助:按钮可见但弹窗无法触发

问题修复方案

你的死亡弹窗无法触发主要由几个语法错误和变量未定义问题导致,以下是具体修复点和修正后的代码:

核心错误点

  • JavaScript 大小写错误:DOM操作中添加类的属性是classList(首字母L大写),你写成了classlist,导致代码执行失败。
  • Overlay 变量未启用:你注释掉了overlay的获取代码,导致后续操作overlay时会报错,必须取消注释。
  • CSS 语法错误:
    • 半透明背景色写法错误,black 0,0,0 .7不符合规范,应改为rgba(0,0,0,0.7)。
    • 内边距的逗号要改成空格,padding:10px,15px → padding:10px 15px。
    • 未定义--red变量,需要全局声明后才能使用。
  • HTML 类名冲突:弹窗容器和内部内容都用了.youdied类,容易导致样式和选择器混淆,建议拆分类名。

修正后的代码

JavaScript

const openyoudiedbuttons = document.querySelectorAll("[data-youdied-target]")
const overlay = document.getElementById("overlay") // 取消注释

openyoudiedbuttons.forEach(button => {
  button.addEventListener("click", () => {
    const youdied = document.querySelector(button.dataset.youdiedTarget)
    openyoudied(youdied)
  })
})

// 可选:添加点击overlay关闭弹窗的功能
overlay.addEventListener("click", () => {
  const activeModals = document.querySelectorAll(".youdied.active")
  activeModals.forEach(modal => {
    closeYouDied(modal)
  })
})

function openyoudied(youdied) {
  if (youdied == null) return
  youdied.classList.add("active") // 修正classList大小写
  overlay.classList.add("active") // 修正classList大小写
}

// 可选:添加关闭弹窗的函数
function closeYouDied(youdied) {
  if (youdied == null) return
  youdied.classList.remove("active")
  overlay.classList.remove("active")
}

CSS

:root {
  --red: #ca0d0d; /* 定义--red变量 */
}

.dead {
  color: #ca0d0d;
  font-size: xx-large;
}

.youdied{
  width: 400px;
  top: 50%;
  left: 50%;
  background: black;
  color: var(--red);
  position: fixed;
  transform: translate(-50%, -50%) scale(0);
  transition: 200ms ease-in-out;
  border: 1px solid rgb(134, 29, 29); 
  border-radius: 10px;
  z-index: 10;
  padding: 10px 15px; /* 修正逗号为空格 */
}
.youdied.active {
  transform: translate(-50%, -50%) scale(1);
}

.youdied-header{
  padding: 10px 15px;
  font-weight: bold;
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--red);
}
.youdied-header .title{
  font-size: 1.25rem;
  font-weight: bold;
}

#overlay{
  position: fixed;
  opacity: 0;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  transition: 200ms ease-in-out;
  background-color: rgba(0, 0, 0, 0.7); /* 修正背景色写法 */
  pointer-events: none;
}

#overlay.active{
  pointer-events: all;
  opacity: 1;
}

.youdied-content {
  margin: 15px 0;
  line-height: 1.5;
}

.box-center {
  text-align: center;
  margin-top: 15px;
}

HTML

<div class="dead">
  <h1>The Pond</h1>
  <img src="pond.webp" height="300" width="600">
</div>      

<div class="youdied" id="youdied">
  <div class="youdied-content"> <!-- 修改类名避免冲突 -->
    You Died<br>
    As you reached for the coat. <br>
    A pale hand grabs you and<br>
    pulls you under the water.<br>
    <img src="ghosthand.jpg" height="150" width="250">
    <div class="box-center">
      <a href="final.html">Try Again?</a> &nbsp;
    </div>
  </div>
</div>

<div>
  <button data-youdied-target="#youdied">Grab Your Brother's coat</button>
</div>

<!-- 添加缺失的overlay元素 -->
<div id="overlay"></div>

内容的提问来源于stack exchange,提问作者Ricky07

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 16:37:35