如何为模态更改此代码,以便它可以与类而不是href一起使用?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何为模态更改此代码,以便它可以与类而不是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>
我找到了两个代码,我可以用它来打开一个模态的画廊,但都没有给我我想要的结果。下面的代码通过类打开模态,但由于某种原因,当您单击图像外部时,关闭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,但无法正确使用。
我希望能得到一些代码的帮助。使用第一个模态我想关闭按钮并通过单击模态外部来关闭。使用第二个代码,我希望能够使用类打开模态而不是锚标记,这样当用户点击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>
以上是关于如何为模态更改此代码,以便它可以与类而不是href一起使用?的主要内容,如果未能解决你的问题,请参考以下文章