如何更改此代码的模式,使其可用于类而不是href?

时间:2019-01-07 02:18:57

标签: javascript modal-dialog gallery

我正在一页纸上工作。单击任何一个框时,将弹出一个相册。

std::vector<std::vector<char>> board {{'5','3','.','.','7','.','.','.','.'},{'6','.','.','1','9','5','.','.','.'},{'.','9','8','.','.','.','.','6','.'},{'8','.','.','.','6','.','.','.','3'},{'4','.','.','8','.','3','.','.','1'},{'7','.','.','.','2','.','.','.','6'},{'.','6','.','.','.','.','2','8','.'},{'.','.','.','4','1','9','.','.','5'},{'.','.','.','.','8','.','.','7','9'}};
.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;
}

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

<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>
// 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;
}

以下代码可完美运行,但可通过href打开模式。从我读过的内容来看,divs不能成为链接。我试图弄乱这两个代码,希望我能找到一个正确的代码,但是我仍在学习Javascript,却无法正确地编写两个代码。

我希望在这两个代码中都能获得帮助。对于第一个模态,我想要关闭按钮,然后单击模态外部以进行关闭。使用第二个代码,我希望能够使用class打开模式,而不是锚标记,以便当用户单击div时,它将打开画廊模式。

<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>
// 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;
}

1 个答案:

答案 0 :(得分:1)

您的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>