我希望有人可以帮助我,因为我是一名业余编码员,并且我花了最近3或4个小时来尝试解决这个问题。与其单击窗口以模态启动我的图像,不如单击该链接以启动它,如何制作它?谢谢您的帮助!这是javascript,CSS和html:
// Get the modal
var modal = document.getElementById('myModal');
modal.addEventListener('click',function(){
this.style.display="none";
})
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// Get all images and insert the clicked image inside the modal
// Get the content of the image description and insert it inside the modal image caption
var images = document.getElementsByTagName('img');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
var i;
for (i = 0; i < images.length; i++) {
images[i].onclick = function() {
modal.style.display = "block";
modalImg.src = this.src;
modalImg.alt = this.alt;
captionText.innerHTML = this.nextElementSibling.innerHTML;
}
}
img.responsive {
float: left;
width: 32%;
margin: 30px;
float: left;
}
img.responsive:hover {
border: 2px solid hotpink;
cursor: pointer;
}
/* 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: rgba(0, 0, 0, 0.9);
/* Black w/ opacity */
}
/* Modal Content (image) */
.modal-content {
margin: auto;
display: block;
}
/* Add Animation */
.modal-content,
#caption {
-webkit-animation-name: zoom;
-webkit-animation-duration: 0.6s;
animation-name: zoom;
animation-duration: 0.6s;
}
@-webkit-keyframes zoom {
from {
transform: scale(0)
}
to {
transform: scale(1)
}
}
@keyframes zoom {
from {
transform: scale(0.1)
}
to {
transform: scale(1)
}
}
/* The Close Button */
.close {
position: absolute;
top: 75px;
right: 35px;
color: white;
font-size: 60px;
font-weight: bold;
transition: 0.3s;
}
.close:hover,
.close:focus {
color: white;
text-decoration: none;
cursor: pointer;
}
/* Clear Floats */
.clearfix:after {
content: "";
display: table;
clear: both;
}
<img class="responsive" src="https://i.imgur.com/cQy7Yg.jpg">
<div class="clearfix"></div>
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>
答案 0 :(得分:1)
您需要对代码进行以下更改才能做到这一点。希望对您有帮助!
// Get the modal
var modal = document.getElementById('myModal');
modal.addEventListener('click', function() {
this.style.display = "none";
})
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
function launchModal(element) {
// Get all images and insert the clicked image inside the modal
// Get the content of the image description and insert it inside the modal image caption
//var images = document.getElementsByTagName('img');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
//var i;
//for (i = 0; i < images.length; i++) {
//images[i].onclick = function() {
modalImg.src = element.getAttribute('data-src');
modalImg.alt = this.alt;
captionText.innerHTML = element.parentElement.children[1].innerHTML;
modal.style.display = "block";
//}
//}
}
img.responsive {
float: left;
width: 32%;
margin: 30px;
float: left;
}
img.responsive:hover {
border: 2px solid hotpink;
cursor: pointer;
}
/* 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: rgba(0, 0, 0, 0.9);
/* Black w/ opacity */
}
/* Modal Content (image) */
.modal-content {
margin: auto;
display: block;
}
/* Add Animation */
.modal-content,
#caption {
-webkit-animation-name: zoom;
-webkit-animation-duration: 0.6s;
animation-name: zoom;
animation-duration: 0.6s;
}
@-webkit-keyframes zoom {
from {
transform: scale(0)
}
to {
transform: scale(1)
}
}
@keyframes zoom {
from {
transform: scale(0.1)
}
to {
transform: scale(1)
}
}
/* The Close Button */
.close {
position: absolute;
top: 75px;
right: 35px;
color: white;
font-size: 60px;
font-weight: bold;
transition: 0.3s;
}
.close:hover,
.close:focus {
color: white;
text-decoration: none;
cursor: pointer;
}
/* Clear Floats */
.clearfix:after {
content: "";
display: table;
clear: both;
}
<div>
<a onclick="launchModal(this)" href="#" data-src="https://i.imgur.com/cQy7Yg.jpg"> Launch Modal </a>
<div class="clearfix"></div>
</div>
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>