Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

javascript - Modal not closing when I click X or outside

The modals I have set up for my website open but do not close when I click the X button or outside.

When I run my code in a browser I can click the button and it works. But then it will not close. I am not sure what the error is or why this does not work.

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>
question from:https://stackoverflow.com/questions/65891771/modal-not-closing-when-i-click-x-or-outside

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've fixed some problems:

  • height of the modal, taking the 100% of the space so influencing the click
  • put a listener on the document, which will trigger the modal if the status is block or if it's the button.

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...