【问题标题】:Modal not closing when I click X or outside当我单击 X 或外部时,模态不会关闭
【发布时间】:2021-01-25 20:12:23
【问题描述】:

我为我的网站设置的模式打开,但当我点击 X 按钮或外部时不关闭。

当我在浏览器中运行我的代码时,我可以单击该按钮并且它可以工作。但随后它不会关闭。我不确定错误是什么或为什么这不起作用。

var modal = document.getElementById("WhiteSedan1")
var btn1 = document.getElementById("BtnWhiteSedan")
var span = document.getElementsByClassName("close")[0];

btn1.onclick = function() {
  modal.style.display = "block";
}
span.onclick = function() {
  modal.style.display = "none";
}
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Sit on top */
  padding-top: 50x;
  /* Location of the box */
  left: 0;
  top: 0;
  width: 50%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0);
  /* Black w/ opacity */
}


/* Modal Content */

.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}


/* The Close Button */

.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}
<div class="desc">
  <a href="#" class="btn btn-outline-primary" id="BtnWhiteSedan">White Sedan</a>
</div>

<div id="WhiteSedan1" class="modal">
  <div class="modal-content">
    <span class="close">&times;</span>
    <p class="text-center">
      Model: Toyota<br> Mileage: 28,000 km <br> Transmission: Auto<br> Cost: $10,000
    </p>
  </div>

</div>

我的目标是当我点击 X 或模态框外时,模态框关闭。

【问题讨论】:

标签: javascript html css


【解决方案1】:

我已经解决了一些问题:

  • 模态框的高度,占据 100% 的空间从而影响点击
  • 在文档上放置一个监听器,如果状态为block或者是按钮,它将触发模态。

var modal = document.getElementById("WhiteSedan1")
var btn1 = document.getElementById("BtnWhiteSedan")
var span = document.getElementsByClassName("close")[0];

window.onclick = function(event) {
  if(btn1.contains(event.target)){
    modal.style.display = "block";
  }else{
    if (!modal.contains(event.target) && modal.style.display === "block" ||  span.contains(event.target)) {
      modal.style.display = "none";
    }
  }
}
.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Sit on top */
  padding-top: 50x;
  /* Location of the box */
  left: 0;
  top: 0;
  width: 50%;
  overflow: auto;
  height: auto;
  z-index: 1px;
  /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0);
  /* Black w/ opacity */
}


/* Modal Content */

.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}


/* The Close Button */

.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}
<div class="desc">
  <a href="#" class="btn btn-outline-primary" id="BtnWhiteSedan">White Sedan</a>
</div>

<div id="WhiteSedan1" class="modal">
  <div class="modal-content">
    <span class="close">&times;</span>
    <p class="text-center">
      Model: Toyota<br> Mileage: 28,000 km <br> Transmission: Auto<br> Cost: $10,000
    </p>
  </div>

</div>

【讨论】:

  • 现在在模态框内点击也会关闭
  • 你来了!努力做个好人,我们正在努力互相帮助;)
【解决方案2】:

我建议将所有打开/关闭模式事件侦听器合并为一个,否则多个事件侦听器会连续运行,但您只希望 一个 动作发生。

实现此行为的一种方法是检查event.target:如果单击BtnWhiteSedan 元素,则打开模态;否则,如果除了× 按钮之外,没有单击模式或模式内部 的任何内容,则关闭模式。见Node.prototype.contains

由于event只用于event.target,所以使用destructuring直接获取target属性。

const modal = document.getElementById("WhiteSedan1"),
  openButton = document.getElementById("BtnWhiteSedan"),
  [
    closeButton
  ] = document.getElementsByClassName("close");

addEventListener("click", ({target}) => {
  if (target === openButton) {
    modal.hidden = false;
  }
  else if(target !== modal && !modal.contains(target) || target === closeButton){
    modal.hidden = true;
  }
});
.modal {
  position: fixed;
  z-index: 1;
  padding-top: 50px;
  left: 0;
  top: 0;
  width: 50%;
  height: 100%;
  overflow: auto;
  background-color: rgb(0, 0, 0);
  background-color: rgba(0, 0, 0, 0);
}
.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}
.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}
.close:hover, .close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}
<div class="desc">
  <a id="BtnWhiteSedan" class="btn btn-outline-primary" href="#">White Sedan</a>
</div>
<div id="WhiteSedan1" class="modal" hidden>
  <div class="modal-content">
    <span class="close">&times;</span>
    <p class="text-center">Model: Toyota<br> Mileage: 28,000 km<br>Transmission: Auto<br> Cost: $10,000</p>
  </div>
</div>

它是not always robust to check for CSS properties。请改用hidden attribute,或在CSS 中使用类名和modal.classList.has("hidden")modal.classList.add("hidden")modal.classList.remove("hidden").hidden { display: none; }。见Element.prototpye.classList。如果您确实使用了 hidden 属性,请删除 CSS 默认值,然后像我在上面的代码中所做的那样,将 hidden 属性添加到您的模式中。

我还替换了var by constonclick by addEventListenerabstract equality by strict equality。我还使用了更多语义变量名称(例如 closeButton 而不是 span)。

您的 CSS 中还有一个拼写错误:padding-top: 50x; 而不是 padding-top: 50px;

【讨论】:

    猜你喜欢
    • 2021-12-28
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多