我需要一些帮助。我正在尝试在我正在设计的网站上使用模态图像,但是当我尝试它时没有任何反应。理想的行为是我想要点击图像,然后会出现模态。下面,我发布了代码。我已经包含了与此功能相关的HTML,CSS和JavaScript部分的代码。任何帮助将不胜感激,欢呼!
// Get the modal
var modal = document.getElementById("picModal");
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById("proImg");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function() {
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
// 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";
}
#proImg {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}
#proImg:hover {
opacity: 0.7;
}
.modal {
display: none;
/*Hides by default*/
position: fixed;
/*Keeps in place*/
z-index: 1;
/*Displays over everything*/
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
/*Enable scroll if needed*/
background-color: rgb(0, 0, 0);
background-color: rgba(0, 0, 0, 0.9);
/*Black w.opacity*/
}
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 600px;
}
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 600px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}
/*Animation - Zoom in the Modal*/
.modal-content,
#caption {
-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)
}
}
/* The Modal Close Button*/
.close {
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 40px;
font-weight: bold;
transition: 0.3s;
}
.close:hover,
.close:focus {
color: #bbb;
text-decoration: none;
cursor: pointer;
}
/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 600px) {
.modal-content {
width: 100%;
}
}
<img src="https://placeimg.com/100/100/any" id="proImg" class="product-image" alt="This is an image">
<!-- Modal Div -->
<div id="picModal" class="modal">
<!-- Close Button -->
<span class="close">X</span>
<!-- Modal Image -->
<img class="modal-content" id="img01">
<!-- Modal Caption -->
<div id="caption"></div>
</div>