我如何更改模态代码以使其与 class 而不是 href 一起使用?

How do I change this code for a modal so that it works with class instead of href?

我正在制作一页作品集。单击任何一个框时,将弹出一个照片库。

.wrapper{
    margin-top:125px;
}
img {
    display:block;
    width:100%;
    height:auto;
}
.portfolio-box {
    display:flex;
    flex-wrap:wrap;
    flex:1;
    flex-direction:row;
    justify-content:center;
}
.portfolio-box > div {
    display:flex;
    justify-content:flex-start;
    flex:1;
    flex-direction:column;
    margin:5px;
    margin-bottom: 40px;
}
.thumb-outer{
    /*Just a  placeholder*/
}
.thumbgrid{
    position:relative;
    display:inline-block;
}
.thumbgrid .inner_box{
    position: absolute;
    width:100.1%;
    height:100%;
    background:rgba(80, 145, 144, 0.64);
    top:50%;
    left:50%;
    transform:translate(-50%,-50%);
    border:1px solid #000;
    border-radius:2px;
}
.thumbgrid .inner_box span{
    color:#4f9190;
    font-family:bonvenocf-light;
    font-weight:bold;
    font-size:16px;
    text-align: center;
    background:#fff;
    position:absolute;
    top:50%;
    left:50%;
    transform:translate(-50%,-50%);
    width:101.2%;
}
.synopsis{
    vertical-align:top;
    text-align:justify;
    margin:0 3px 0 3px;
    display: inline-block;
}
<div class="wrapper">

<section>
<!--Start of Portfolio-->
<div class="portfolio-box">
<!--Thumb 1-->
<div class="thumb-outer">
<div class="thumbgrid">
  <img src="https://i0.wp.com/ukhumanrightsblog.com/wp-content/uploads/2013/06/seo-marketing-320x200.jpg">
  <div class="inner_box">
  <span>Title 1</span>
</div>
</div>
   <div class="synopsis">
   Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse placerat diam eget magna dignissim malesuada.
     </div>
</div>
<!--End Of Thumb-->

<!--Thumb 2-->
<div class="thumb-outer">
<div class="thumbgrid">
  <img src="https://i0.wp.com/ukhumanrightsblog.com/wp-content/uploads/2013/06/seo-marketing-320x200.jpg">
  <div class="inner_box">
  <span>Title 2</span>
</div>
</div>
   <div class="synopsis">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse placerat diam eget magna dignissim malesuada.
     </div>
</div>
<!--End Of Thumb-->

<!--Thumb 3-->
<div class="thumb-outer">
<div class="thumbgrid">
  <img src="https://i0.wp.com/ukhumanrightsblog.com/wp-content/uploads/2013/06/seo-marketing-320x200.jpg">
  <div class="inner_box">
  <span>Title 3</span>
</div>
</div>
   <div class="synopsis">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse placerat diam eget magna dignissim malesuada.
     </div>
</div>
<!--End Of Thumb-->

<!--Thumb 4-->
<div class="thumb-outer">
<div class="thumbgrid">
  <img src="https://i0.wp.com/ukhumanrightsblog.com/wp-content/uploads/2013/06/seo-marketing-320x200.jpg">
  <div class="inner_box">
  <span>Title 4</span>
</div>
</div>
   <div class="synopsis">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse placerat diam eget magna dignissim malesuada.
     </div>
</div>
<!--End Of Thumb-->

</div><!--End of Portfolio-->
</div>

我找到了 2 个可用于以模式打开画廊的代码,但都没有给我想要的结果。下面的代码通过 class 打开模式,但由于某些原因,关闭 X 按钮不起作用,当您单击图像外部时模式也不会关闭。

// Get the modal
var modal = document.getElementsByClassName('modal');

// Get the button that opens the modal
var btn = document.getElementsByClassName("myBtn");


// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close");

// When the user clicks the button, open the modal 
btn[0].onclick = function() {
    modal[0].style.display = "block";
}

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

span[1].onclick = function() {
    modal[1].style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
body {font-family: Arial, Helvetica, sans-serif;}

/* The Modal (background) */
.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.4); /* Black w/ opacity */
}
img{
width:100%;
  box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
  -webkit-animation-name: animatetop;
  -webkit-animation-duration: 0.4s;
  animation-name: animatetop;
  animation-duration: 0.4s;
}
/* Modal Content */
.modal-content {
  position: relative;
  margin: auto;
  padding: 0;
  width: 80%;
}

/* Add Animation */
@-webkit-keyframes animatetop {
  from {top:-300px; opacity:0} 
  to {top:0; opacity:1}
}

@keyframes animatetop {
  from {top:-300px; opacity:0}
  to {top:0; opacity:1}
}

/* The Close Button */
.close {
  color: red;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}
<button class="myBtn">Open Modal</button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
      <span class="close">×</span>
<img src="https://cdn-images-1.medium.com/max/2000/1*6cpWeq1_VMYtd1LgHwxKug.jpeg">
  </div>

</div>

下面的代码运行良好,但它通过 href 打开模式。从我读过的 div 不能制作成链接。我试着弄乱这两种代码,希望我能让一个代码正常工作,但我仍在学习 Javascript 并且无法正确使用。

我希望在这两个代码方面得到一些帮助。对于第一个模态,我希望关闭按钮并通过单击模态外部来关闭。使用第二个代码,我希望能够使用 class 打开模态而不是锚标记,以便当用户单击 div 时它会打开图库模态。

// Get the button that opens the modal
var btn = document.querySelectorAll("button.modal-button");

// All page modals
var modals = document.querySelectorAll('.modal');

// Get the <span> element that closes the modal
var spans = document.getElementsByClassName("close");

// When the user clicks the button, open the modal
for (var i = 0; i < btn.length; i++) {
 btn[i].onclick = function(e) {
    e.preventDefault();
    modal = document.querySelector(e.target.getAttribute("href"));
    modal.style.display = "block";
 }
}

// When the user clicks on <span> (x), close the modal
for (var i = 0; i < spans.length; i++) {
 spans[i].onclick = function() {
    for (var index in modals) {
      if (typeof modals[index].style !== 'undefined') modals[index].style.display = "none";    
    }
 }
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target.classList.contains('modal')) {
     for (var index in modals) {
      if (typeof modals[index].style !== 'undefined') modals[index].style.display = "none";    
     }
    }
}
/* The Modal (background) */
.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.4); /* Black w/ opacity */
}
img{
width:100%;
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
    -webkit-animation-name: animatetop;
    -webkit-animation-duration: 0.4s;
    animation-name: animatetop;
    animation-duration: 0.4s;
}
/* Modal Content */
.modal-content {
    position: relative;
    margin: auto;
    padding: 0;
    width: 80%;
}

/* Add Animation */
@-webkit-keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
}

@keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
}

/* The Close Button */
.close {
    color: white;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}
<button class="modal-button" href="#myModal1">Open Modal</button>

<!-- The Modal -->
<div id="myModal1" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
      <span class="close">×</span>
<img src="https://cdn-images-1.medium.com/max/1000/1*6cpWeq1_VMYtd1LgHwxKug.jpeg">
  </div>

</div>

您的 window.click 正在取消所有内容。

编辑:已修复以确保模态外部正确关闭。 注意:删除了不需要的 window.click。

document.addEventListener('DOMContentLoaded', function() {

  let modal = document.querySelector('.modal');
  let modalContent = document.querySelector('.modal-content');
  let btn = document.querySelector('.myBtn');
  let closeSpan = document.querySelector('.close');
  
  btn.addEventListener('click', function() {
    console.log('btn click');
    console.log(modal);
    modal.style.display = 'block';    
  });
  
  closeSpan.addEventListener('click', function() {
    modal.style.display = 'none';  
  });
  
  modal.addEventListener('click', function(event) {
    console.log(event.target.id);
    if(event.target.id === 'myModal') }
      modal.style.display = 'none';
    } 
  });  
});
body {
  font-family: Arial, Helvetica, sans-serif;
}


/* The Modal (background) */

.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.4);
  /* Black w/ opacity */
}

img {
  width: 100%;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  -webkit-animation-name: animatetop;
  -webkit-animation-duration: 0.4s;
  animation-name: animatetop;
  animation-duration: 0.4s;
}


/* Modal Content */

.modal-content {
  position: relative;
  margin: auto;
  padding: 0;
  width: 80%;
}


/* Add Animation */

@-webkit-keyframes animatetop {
  from {
    top: -300px;
    opacity: 0
  }
  to {
    top: 0;
    opacity: 1
  }
}

@keyframes animatetop {
  from {
    top: -300px;
    opacity: 0
  }
  to {
    top: 0;
    opacity: 1
  }
}


/* The Close Button */

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

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}
<button class="myBtn">Open Modal</button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">×</span>
    <img src="https://cdn-images-1.medium.com/max/2000/1*6cpWeq1_VMYtd1LgHwxKug.jpeg">
  </div>

</div>