开发者问题收集

无法打开模式窗口

2022-11-11
87

您好,我有一个研究项目,该项目是在仪表板上显示数据,用户可以根据自己的需要对其进行或多或少的修改。

我试图打开一个模态窗口,但它不起作用,我不知道我的错误来自哪里。

const body = document.querySelector("body"),
  sidebar = body.querySelector(".sidebar"),
  btn = body.querySelector("#btn");

btn.addEventListener("click", () => {
  sidebar.classList.toggle("active");
})

const modalContainer = document.querySelector(".modal-container");
const modalTriggers = document.querySelector(".modal-trigger");
modalTriggers.forEach(trigger => trigger.addEventListener("click", toggleModal))

function toggleModal() {
  modalContainer.classList.toggle("active")
}
body {
  height: 100vh;
  background: var(--body-color);
}

.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  height: 100%;
  width: 78px;
  background: var(--primary-color);
  padding: 6px 14px;
  transition: all 0.5s ease;
}

.sidebar.active {
  width: 240px
}

.sidebar .logo_content .logo {
  color: #FFF;
  display: flex;
  height: 50px;
  width: 100%;
  align-items: center;
  opacity: 0;
  pointer-events: none;
}

.sidebar.active .logo_content .logo {
  opacity: 1;
  pointer-events: none;
}

.logo_content .logo i {
  font-size: 28px;
  margin-right: 5px;
}

.logo_content .logo .logo_name {
  font-size: 20px;
  font-weight: 400;
}

.sidebar #btn {
  position: absolute;
  color: #FFF;
  top: 6px;
  left: 50%;
  font-size: 20px;
  height: 50px;
  width: 50px;
  text-align: center;
  line-height: 50px;
  cursor: pointer;
  transform: translateX(-50%);
}

.sidebar.active #btn {
  left: 90%;
}

.sidebar ul {
  margin-top: 20px;
}

.sidebar li {
  position: relative;
  height: 50px;
  width: 100%;
  margin: 0 0px;
  list-style: none;
  line-height: 50px;
}

.sidebar li a {
  color: #FFF;
  display: flex;
  align-items: center;
  text-decoration: none;
  transition: all 0.4s ease;
  border-radius: 12px;
  white-space: nowrap;
}

.sidebar li a:hover {
  color: #11101d;
  background: #FFF;
}

.sidebar li a i {
  height: 50px;
  min-width: 50px;
  border-radius: 12px;
  line-height: 50px;
  text-align: center;
}

.sidebar .links_name {
  opacity: 0;
  pointer-events: none;
}

.sidebar.active .links_name {
  opacity: 1;
  pointer-events: auto;
}

.sidebar .menu-bar {
  height: calc(100% - 50px);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.home {
  position: relative;
  height: 100vh;
  left: 78px;
  width: calc(100% - 78px);
  background: var(--body-color);
  transition: var(--tran-05);
}

.home .text {
  font-size: 30px;
  font-weight: 500;
  color: var(--text-color);
  padding: 8px 40px;
}

.sidebar.active~.home {
  left: 240px;
  width: calc(100% - 78px);
}

.modal-container {
  display: none;
  position: fixed;
  top: 0;
  width: 100vw;
  height: 100vh;
}

.modal-container.active {
  display: flex;
}

.overlay {
  position: absolute;
  width: 100%;
  height: 100%;
  background: #333333d3;
}

.modal {
  width: 95%;
  border-radius: 5px;
  max-width: 500px;
  min-width: 300px;
  padding: 30px;
  background: #FFF;
  position: absolute;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.close-modal {
  padding: 8px 10px;
  border: none;
  border-radius: 5px;
  font-size: 18px;
  position: absolute;
  top: 10px;
  right: 10px;
  cursor: pointer;
  background: #ff365e;
  color: #FFF;
}

.modal h1 {
  margin-top: 0px;
  font-family: Montserrat, sans-serif;
  font-weight: 500;
}
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <!-- <link rel="stylesheet" href="style.css"> -->
  <link href='https://unpkg.com/[email protected]/css/boxicons.min.css' rel='stylesheet'>
</head>

<body>
  <div class="sidebar">
    <div class="logo_content">
      <div class="logo">
        <i class='bx bx1-c-plus-plus'></i>
        <div class="logo_name">SailVision</div>
      </div>
      <i class='bx bx-menu' id="btn"></i>
    </div>
    <div class="menu-bar">
      <ul class="dash_list">
        <li>
          <a href="#">
            <i class='bx bx-windows'></i>
            <span class="links_name">Dashboard n°1</span>
          </a>
        </li>
        <li>
          <a href="#">
            <i class='bx bx-windows'></i>
            <span class="links_name">Dashboard n°2</span>
          </a>
        </li>
        <li>
          <a href="#">
            <i class='bx bx-plus'></i>
            <span class="links_name">Ajouter</span>
          </a>
        </li>
        <li>
          <a href="#">
            <i class='bx bx-customize modal-trigger'></i>
            <span class="links_name">Template</span>
          </a>
        </li>
      </ul>
      <div class="bottom_content">
        <li>
          <a href="#">
            <i class='bx bx-cog'></i>
            <span class="links_name">Paramètre</span>
          </a>
        </li>
      </div>
    </div>
  </div>
  <div class="home">
    <div class="template">Template n°1</div>
  </div>
  <div class="modal-container">
    <div class="overlay modal-trigger">
      <div class="modal">
        <button class="close-modal modal-trigger">X</button>
        <h1>Choix des templates</h1>
      </div>
    </div>
  </div>
  <!-- <script src="script.js"></script> -->
</body>
2个回答

补充一下之前的回答,点击关闭按钮后模态窗口不会关闭。只有点击窗口外部才会关闭。这应该可以解决所有问题:删除覆盖类中的 modal-trigger 类。它应该是 <div class="overlay"> 而不是 <div class="overlay modal-trigger"> ,为了演示,我更改了背景颜色

const body = document.querySelector("body"),
        sidebar = body.querySelector(".sidebar"),
        btn = body.querySelector("#btn");

        btn.addEventListener("click", () => {
            sidebar.classList.toggle("active");
        })

        const modalContainer = document.querySelector(".modal-container");

        const modalTriggers = document.querySelectorAll(".modal-trigger");

        modalTriggers.forEach(trigger => trigger.addEventListener("click", toggleModal))

        function toggleModal() {
            modalContainer.classList.toggle("active")
        }
body {
            height: 100vh;
            background: purple;
        }

        .sidebar {
            position: fixed;
            top: 0;
            left: 0;
            height: 100%;
            width: 78px;
            background: var(--primary-color);
            padding: 6px 14px;
            transition: all 0.5s ease;
        }

        .sidebar.active {
            width: 240px
        }

        .sidebar .logo_content .logo {
            color: #FFF;
            display: flex;
            height: 50px;
            width: 100%;
            align-items: center;
            opacity: 0;
            pointer-events: none;
        }

        .sidebar.active .logo_content .logo {
            opacity: 1;
            pointer-events: none;
        }

        .logo_content .logo i {
            font-size: 28px;
            margin-right: 5px;
        }

        .logo_content .logo .logo_name {
            font-size: 20px;
            font-weight: 400;
        }

        .sidebar #btn {
            position: absolute;
            color: #FFF;
            top: 6px;
            left: 50%;
            font-size: 20px;
            height: 50px;
            width: 50px;
            text-align: center;
            line-height: 50px;
            cursor: pointer;
            transform: translateX(-50%);
        }

        .sidebar.active #btn {
            left: 90%;
        }

        .sidebar ul {
            margin-top: 20px;
        }

        .sidebar li {
            position: relative;
            height: 50px;
            width: 100%;
            margin: 0 0px;
            list-style: none;
            line-height: 50px;
        }

        .sidebar li a {
            color: #FFF;
            display: flex;
            align-items: center;
            text-decoration: none;
            transition: all 0.4s ease;
            border-radius: 12px;
            white-space: nowrap;
        }

        .sidebar li a:hover {
            color: #11101d;
            background: #FFF;
        }

        .sidebar li a i {
            height: 50px;
            min-width: 50px;
            border-radius: 12px;
            line-height: 50px;
            text-align: center;
        }

        .sidebar .links_name {
            opacity: 0;
            pointer-events: none;
        }

        .sidebar.active .links_name {
            opacity: 1;
            pointer-events: auto;
        }

        .sidebar .menu-bar {
            height: calc(100% - 50px);
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }

        .home {
            position: relative;
            height: 100vh;
            left: 78px;
            width: calc(100% - 78px);
            background: var(--body-color);
            transition: var(--tran-05);
        }

        .home .text {
            font-size: 30px;
            font-weight: 500;
            color: var(--text-color);
            padding: 8px 40px;
        }

        .sidebar.active~.home {
            left: 240px;
            width: calc(100% - 78px);
        }

        .modal-container {
            display: none;
            position: fixed;
            top: 0;
            width: 100vw;
            height: 100vh;
        }

        .modal-container.active {
            display: block;
        }

        .overlay {
            position: absolute;
            width: 100%;
            height: 100%;
            background: #333333d3;
        }

        .modal {
            width: 95%;
            border-radius: 5px;
            max-width: 500px;
            min-width: 300px;
            padding: 30px;
            background: #FFF;
            position: absolute;
            top: 40%;
            left: 50%;
            transform: translate(-50%, -50%);
        }

        .close-modal {
            padding: 8px 10px;
            border: none;
            border-radius: 5px;
            font-size: 18px;
            position: absolute;
            top: 10px;
            right: 10px;
            cursor: pointer;
            background: #ff365e;
            color: #FFF;
        }

        .modal h1 {
            margin-top: 0px;
            font-family: Montserrat, sans-serif;
            font-weight: 500;
        }
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- <link rel="stylesheet" href="style.css"> -->
    <link href='https://unpkg.com/[email protected]/css/boxicons.min.css' rel='stylesheet'>
</head>

<body>
    <div class="sidebar">
        <div class="logo_content">
            <div class="logo">
                <i class='bx bx1-c-plus-plus'></i>
                <div class="logo_name">SailVision</div>
            </div>
            <i class='bx bx-menu' id="btn"></i>
        </div>
        <div class="menu-bar">
            <ul class="dash_list">
                <li>
                    <a href="#">
                        <i class='bx bx-windows'></i>
                        <span class="links_name">Dashboard n°1</span>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i class='bx bx-windows'></i>
                        <span class="links_name">Dashboard n°2</span>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i class='bx bx-plus'></i>
                        <span class="links_name">Ajouter</span>
                    </a>
                </li>
                <li>
                    <a href="#" class="modal-trigger">
                        <i class='bx bx-customize'></i>
                        <span class="links_name">Template</span>
                    </a>
                </li>
            </ul>
            <div class="bottom_content">
                <li>
                    <a href="#">
                        <i class='bx bx-cog'></i>
                        <span class="links_name">Paramètre</span>
                    </a>
                </li>
            </div>
        </div>
    </div>
    <div class="home">
        <div class="template">Template n°1</div>
    </div>
    <div class="modal-container">
        <div class="overlay">
            <div class="modal">
                <button class="close-modal modal-trigger">X</button>
                <h1>Choix des templates</h1>
            </div>
        </div>
    </div>
</body>
</html>
Owusu Ansah Gerald
2022-11-11

现在应该可以工作了,请记住 querySelector() 仅抓取第一个匹配项,您应该使用 querySelectorAll() 方法,该方法是可迭代的,可抓取所有匹配的元素。

const body = document.querySelector("body"),
  sidebar = body.querySelector(".sidebar"),
  btn = body.querySelector("#btn");

btn.addEventListener("click", () => {
  sidebar.classList.toggle("active");
})

const modalContainer = document.querySelector(".modal-container");
const modalTriggers = document.querySelectorAll(".modal-trigger");
modalTriggers.forEach(trigger => trigger.addEventListener("click", toggleModal))

function toggleModal() {
  modalContainer.classList.toggle("active")
}
body {
  height: 100vh;
  background: var(--body-color);
}

.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  height: 100%;
  width: 78px;
  background: var(--primary-color);
  padding: 6px 14px;
  transition: all 0.5s ease;
}

.sidebar.active {
  width: 240px
}

.sidebar .logo_content .logo {
  color: #FFF;
  display: flex;
  height: 50px;
  width: 100%;
  align-items: center;
  opacity: 0;
  pointer-events: none;
}

.sidebar.active .logo_content .logo {
  opacity: 1;
  pointer-events: none;
}

.logo_content .logo i {
  font-size: 28px;
  margin-right: 5px;
}

.logo_content .logo .logo_name {
  font-size: 20px;
  font-weight: 400;
}

.sidebar #btn {
  position: absolute;
  color: #FFF;
  top: 6px;
  left: 50%;
  font-size: 20px;
  height: 50px;
  width: 50px;
  text-align: center;
  line-height: 50px;
  cursor: pointer;
  transform: translateX(-50%);
}

.sidebar.active #btn {
  left: 90%;
}

.sidebar ul {
  margin-top: 20px;
}

.sidebar li {
  position: relative;
  height: 50px;
  width: 100%;
  margin: 0 0px;
  list-style: none;
  line-height: 50px;
}

.sidebar li a {
  color: #FFF;
  display: flex;
  align-items: center;
  text-decoration: none;
  transition: all 0.4s ease;
  border-radius: 12px;
  white-space: nowrap;
}

.sidebar li a:hover {
  color: #11101d;
  background: #FFF;
}

.sidebar li a i {
  height: 50px;
  min-width: 50px;
  border-radius: 12px;
  line-height: 50px;
  text-align: center;
}

.sidebar .links_name {
  opacity: 0;
  pointer-events: none;
}

.sidebar.active .links_name {
  opacity: 1;
  pointer-events: auto;
}

.sidebar .menu-bar {
  height: calc(100% - 50px);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.home {
  position: relative;
  height: 100vh;
  left: 78px;
  width: calc(100% - 78px);
  background: var(--body-color);
  transition: var(--tran-05);
}

.home .text {
  font-size: 30px;
  font-weight: 500;
  color: var(--text-color);
  padding: 8px 40px;
}

.sidebar.active~.home {
  left: 240px;
  width: calc(100% - 78px);
}

.modal-container {
  display: none;
  position: fixed;
  top: 0;
  width: 100vw;
  height: 100vh;
}

.modal-container.active {
  display: flex;
}

.overlay {
  position: absolute;
  width: 100%;
  height: 100%;
  background: #333333d3;
}

.modal {
  width: 95%;
  border-radius: 5px;
  max-width: 500px;
  min-width: 300px;
  padding: 30px;
  background: #FFF;
  position: absolute;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.close-modal {
  padding: 8px 10px;
  border: none;
  border-radius: 5px;
  font-size: 18px;
  position: absolute;
  top: 10px;
  right: 10px;
  cursor: pointer;
  background: #ff365e;
  color: #FFF;
}

.modal h1 {
  margin-top: 0px;
  font-family: Montserrat, sans-serif;
  font-weight: 500;
}
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <!-- <link rel="stylesheet" href="style.css"> -->
  <link href='https://unpkg.com/[email protected]/css/boxicons.min.css' rel='stylesheet'>
</head>

<body>
  <div class="sidebar">
    <div class="logo_content">
      <div class="logo">
        <i class='bx bx1-c-plus-plus'></i>
        <div class="logo_name">SailVision</div>
      </div>
      <i class='bx bx-menu' id="btn"></i>
    </div>
    <div class="menu-bar">
      <ul class="dash_list">
        <li>
          <a href="#">
            <i class='bx bx-windows'></i>
            <span class="links_name">Dashboard n°1</span>
          </a>
        </li>
        <li>
          <a href="#">
            <i class='bx bx-windows'></i>
            <span class="links_name">Dashboard n°2</span>
          </a>
        </li>
        <li>
          <a href="#">
            <i class='bx bx-plus'></i>
            <span class="links_name">Ajouter</span>
          </a>
        </li>
        <li>
          <a href="#">
            <i class='bx bx-customize modal-trigger'></i>
            <span class="links_name">Template</span>
          </a>
        </li>
      </ul>
      <div class="bottom_content">
        <li>
          <a href="#">
            <i class='bx bx-cog'></i>
            <span class="links_name">Paramètre</span>
          </a>
        </li>
      </div>
    </div>
  </div>
  <div class="home">
    <div class="template">Template n°1</div>
  </div>
  <div class="modal-container">
    <div class="overlay modal-trigger">
      <div class="modal">
        <button class="close-modal modal-trigger">X</button>
        <h1>Choix des templates</h1>
      </div>
    </div>
  </div>
  <!-- <script src="script.js"></script> -->
</body>
Franco Gabriel
2022-11-11