单击图像时图像模态未打开。 JavaScript 不工作

Image modal not opening while clicking images. JavaScript not working

我制作了我想在点击时打开的画廊模态图像,但它没有发生。基本上,我想在单击每个图像时将它们的图像 ID 传递给 JavaScript。并且脚本必须打开相应的图像作为弹出窗口或模式以及关闭按钮。

下面是CSS

<style>
/* The grid: Four equal columns that floats next to each other */
.column {
  float: left;
  width: 25%;
  padding: 10px;
}

/* Style the images inside the grid */
.column img {
  opacity: 0.8; 
  cursor: pointer; 
}

.column img:hover {
  opacity: 1;
}

/* Clear floats after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}

/* The expanding image container */
.imgcontainer {
  position: relative;
  display: none;
}



/* Closable button inside the expanded image */
.closebtn {
   position: absolute;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;
}

.closebtn:hover,
.closebtn:focus {
  color: #bbb;
  text-decoration: none;
  cursor: pointer;
}


.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  padding-top: 100px; /* Location of the box */
  left: 0;
  top: 0;
  width: 100%; /* 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.9); /* Black w/ opacity */
}
.modal-content {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
}


/* Add Animation */
.modal-content {  
  -webkit-animation-name: zoom;
  -webkit-animation-duration: 0.6s;
  animation-name: zoom;
  animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
  from {-webkit-transform:scale(0)} 
  to {-webkit-transform:scale(1)}
}

@keyframes zoom {
  from {transform:scale(0)} 
  to {transform:scale(1)}
}

</style>

下面是HTML

<div id="myModal" class="modal">
  <span class="closebtn">&times;</span>
  <img class="modal-content" id="expandedImg">
</div>

<!-- The four columns -->
<div class="row">
  <div class="column">
    <img src="img/content/services/serv-71.jpg" alt="Nature" style="width:100%" onclick="myFunction(this);">
  </div>
  <div class="column">
    <img src="img/content/services/serv-72.jpg" alt="Snow" style="width:100%" onclick="myFunction(this);">
  </div>
  <div class="column">
    <img src="img/content/services/serv-73.jpg" alt="Mountains" style="width:100%" onclick="myFunction(this);">
  </div>
  <div class="column">
    <img src="img/content/services/serv-74.jpg" alt="Lights" style="width:100%" onclick="myFunction(this);">
  </div>
  
</div>    

</div>
</div>

这是JavaScript

<script>
function myFunction(imgs) {
// Get the modal
var modal = document.getElementById("myModal");

// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById("imgs");
var modalImg = document.getElementById("img");
img.onclick = function(){
  modal.style.display = "block";
  modalImg.src = this.src;
}

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("closebtn")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() { 
  modal.style.display = "none";
}}

</script>

这修复了您的代码,我添加了两个单独的函数,一个用于相应地打开和设置模态的 src 属性,另一个用于关闭模态。

function myFunction(event) {
  var modal = document.getElementById("myModal");
  modal.style.display = "block";
  document.getElementById("expandedImg").setAttribute('src', event.target.getAttribute("src"));

}

function closeModal(event) {
  const modal = document.getElementById("myModal");
  modal.style.display = "none";
}
/* The grid: Four equal columns that floats next to each other */
.column {
  float: left;
  width: 25%;
  padding: 10px;
}

/* Style the images inside the grid */
.column img {
  opacity: 0.8;
  cursor: pointer;
}

.column img:hover {
  opacity: 1;
}

/* Clear floats after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}

/* The expanding image container */
.imgcontainer {
  position: relative;
  display: none;
}



/* Closable button inside the expanded image */
.closebtn {
  position: absolute;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;
}

.closebtn:hover,
.closebtn:focus {
  color: #bbb;
  text-decoration: none;
  cursor: pointer;
}


.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Sit on top */
  padding-top: 100px;
  /* Location of the box */
  left: 0;
  top: 0;
  width: 100%;
  /* 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.9);
  /* Black w/ opacity */
}

.modal-content {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
}


/* Add Animation */
.modal-content {
  -webkit-animation-name: zoom;
  -webkit-animation-duration: 0.6s;
  animation-name: zoom;
  animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
  from {
    -webkit-transform: scale(0)
  }

  to {
    -webkit-transform: scale(1)
  }
}

@keyframes zoom {
  from {
    transform: scale(0)
  }

  to {
    transform: scale(1)
  }
}
<!-- Put your HTML here  -->
<div id="myModal" class="modal">
  <span class="closebtn" onclick="closeModal(event)">&times;</span>
  <img class="modal-content" id="expandedImg">
</div>

<!-- The four columns -->
<div class="row">
  <div class="column">
    <img src="img/content/services/serv-71.jpg" alt="Nature" style="width:100%" onclick="myFunction(event);">
  </div>
  <div class="column">
    <img src="img/content/services/serv-72.jpg" alt="Snow" style="width:100%" onclick="myFunction(event);">
  </div>
  <div class="column">
    <img src="img/content/services/serv-73.jpg" alt="Mountains" style="width:100%" onclick="myFunction(event);">
  </div>
  <div class="column">
    <img src="img/content/services/serv-74.jpg" alt="Lights" style="width:100%" onclick="myFunction(event);">
  </div>

</div>