有没有人知道如何从w3schools example做到这一点,但有3个缩略图
到目前为止,这是我的代码:
<div class="row">
<div class="col-md-4 ">
<!-- Trigger the Modal -->
<div class="polaroid">
<img id="myImg" src="home-page-feature-thumbnail-image-front-featured-films-slider-2.jpg" alt="Caption1" width="auto;" height="auto;">
<div class="info">
<p style="color:#971604; font-weight:bold;">This is a text.</p>
<h4>Caption 1</h4>
</div>
</div>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- The Close Button -->
<span class="close" >×</span>
<!-- Modal Content (The Image) -->
<img class="modal-content" id="img01" src="img.jpg">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>
</div>
<div class="col-md-4">
<!-- Trigger the Modal -->
<div class="polaroid">
<img id="myImg2" src="home-page-feature-thumbnail-image-front-featured-films-slider-4.jpg" alt="Caption2" width="auto;" height="auto;">
<div class="info">
<p style="color:#971604; font-weight:bold;">This is another text.</p>
<h4>Caption 2</h4>
</div>
</div
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- The Close Button -->
<span class="close" >×</span>
<!-- Modal Content (The Image) -->
<img class="modal-content" id="img02" src="img2.jpg">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>
</div>
<div class="col-md-4">
<!-- Trigger the Modal -->
<div class="polaroid">
<img id="myImg" src="home-page-feature-thumbnail-image-front-featured-films-slider-2.jpg" alt="Caption3" width="auto;" height="auto">
<div class="info">
<p style="color:#971604; font-weight:bold;">This is the 3rd text.</p>
<h4>Caption 3</h4>
</div>
</div>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- The Close Button -->
<span class="close" onclick="document.getElementById('myModal').style.display='none'">×</span>
<!-- Modal Content (The Image) -->
<img class="modal-content" id="img03" src="img3.jpg">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>
</div>
</div>
这就是我的javascript
var modal = document.getElementById('myModal');
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById('myImg');
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";
}
这是我的css文件中的内容:
/* Style the Image Used to Trigger the Modal */
div.polaroid {
width: 100%;
background-color: white;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
margin-bottom: 25px;
}
div.info {
padding: 10px 20px;
}
/* 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.9); /* Black w/ opacity */
}
/* Modal Content (Image) */
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}
/* Caption of Modal Image (Image Text) - Same Width as the Image */
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}
/* Add 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 Close Button */
.close {
position: absolute;
top: 35px;
right: 25px;
color: #ffffff;
font-size: 150px;
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: 700px){
.modal-content {
width: 100%;
}
}
第一张图片的工作正常,但对于其他两张图片,没有出现任何模态。任何帮助,将不胜感激。
答案 0 :(得分:2)
您不需要为每个图像添加模态,只需一个模态即可。 在您的代码中,模态仅适用于第一个图像,因为您的js代码仅处理第一个图像。找到适用于三个图像的以下代码。
function handleImageClick(c){
var modal = document.getElementById('myModal');
var modalImg = document.getElementById("modalImage");
var captionText = document.getElementById("caption");
modal.style.display = "block";
modalImg.src = c.src;
captionText.innerHTML = c.alt;
}
div.polaroid {
width: 100%;
background-color: white;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
margin-bottom: 25px;
}
div.info {
padding: 10px 20px;
}
/* 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.9); /* Black w/ opacity */
}
/* Modal Content (Image) */
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}
/* Caption of Modal Image (Image Text) - Same Width as the Image */
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}
/* Add 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 Close Button */
.close {
position: absolute;
top: 35px;
right: 25px;
color: #ffffff;
font-size: 150px;
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: 700px){
.modal-content {
width: 100%;
}
}
<div class="row">
<div class="col-md-4 ">
<div class="polaroid">
<img id="myImg" onClick="handleImageClick(this);" src="http://www.w3schools.com/howto/img_fjords.jpg" alt="Caption1" width="auto;" height="auto;">
<div class="info">
<p style="color:#971604; font-weight:bold;">This is a text.</p>
<h4>Caption 1</h4>
</div>
</div>
</div>
<div class="col-md-4">
<div class="polaroid">
<img id="myImg2" onClick="handleImageClick(this);" src="http://www.w3schools.com/howto/img_fjords.jpg" alt="Caption2" width="auto;" height="auto;">
<div class="info">
<p style="color:#971604; font-weight:bold;">This is another text.</p>
<h4>Caption 2</h4>
</div>
</div>
</div>
<div class="col-md-4">
<div class="polaroid">
<img id="myImg" onClick="handleImageClick(this);" src="http://www.w3schools.com/howto/img_fjords.jpg" alt="Caption3" width="auto;" height="auto">
<div class="info">
<p style="color:#971604; font-weight:bold;">This is the 3rd text.</p>
<h4>Caption 3</h4>
</div>
</div>
</div>
</div>
<div id="myModal" class="modal">
<!-- The Close Button -->
<span class="close" onclick="document.getElementById('myModal').style.display='none'">×</span>
<!-- Modal Content (The Image) -->
<img class="modal-content" id="modalImage" src="">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>