我正在尝试创建一个模态图像,这样当您点击图像时,它会弹出一个更大的图像。
我的代码(见下文)只允许我在弹出窗口中只打开一个图像。它不允许我打开剩下的图像,
HTML
<section id="responsive">
<div class="container">
<div class="row text-center">
<h2 class="os-animation" data-os-animation="zoomIn" data-os-animation-delay="0.3s">HOW DOES IT WORK</h2>
<p style="text-align: left">Exam Portal, enhanced with QFrency SA voices, provides educators with a Quick and Easy to use solution saving them time recording audio, saving parents and schools money hiring Human readers, and enabling learners to bridge a gap where there is nepotism, and an unfair advantage under already stressful testing conditions.</p>
<video class="innerfour os-animation" data-os-animation="zoomInLeft" data-os-animation-delay="0.6s" width="50%" height="auto" controls>
<source src="Videos/main.mp4" type="video/mp4">
Your browser does not support the video tag.
</video> <br>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 col-xxs-12">
<div class="testimonial-content">
<h2 style="color:#37cdff" class="feature-content-title blue-text"><span class="blue">1</span>Drag & Drop exam</h2>
<img class="img-responsive" id="myImg" src="img/Link Students.png" alt="Trolltunga, Norway" >
<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="img01">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>
<p class="feature-content-description">Capture an exam quickly by using the Drag & Drop functionality. The user would drag the exam and drop it in the
upload section. The exam will get displayed and it will keep it's original formatting.</p>
<br>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 col-xxs-12">
<div class="testimonial-content">
<h2 style="color:#37cdff" class="feature-content-title blue-text"><span class="blue">1</span>Drag & Drop exam</h2>
<img class="img-responsive" id="myImg" src="img/Link Students.png" alt="Trolltunga, Norway" >
<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="img01">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>
<p class="feature-content-description">Capture an exam quickly by using the Drag & Drop functionality. The user would drag the exam and drop it in the
upload section. The exam will get displayed and it will keep it's original formatting.</p>
<br>
</div>
</div>
</div>
</div>
</section>
CSS
#myImg {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}
#myImg:hover {opacity: 0.7;}
/* 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: 1000px;
}
/* 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;
}
JS
<script>
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";
}
</script>
请协助。谢谢
答案 0 :(得分:0)
正如tech2017建议的那样,您可以使用类而不是重复无效的ID。使用getElementsByClassName()
获取具有相同类的所有元素的列表,然后遍历列表并将onclick
分配给具有您的代码的函数。您只需要一个myModal
。这是修复的代码:
function enlargeImage() {
document.getElementById("img01").src = this.src;
document.getElementById("caption").innerHTML = this.alt;
document.getElementById('myModal').style.display = "block";
}
(function() {
var images = document.getElementsByClassName("myImg");
for (i = 0; i < images.length; i++) {
images[i].onclick = enlargeImage;
}
})();
&#13;
#myImg {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}
#myImg:hover {
opacity: 0.7;
}
/* 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 */
color: #fff;
}
/* Modal Content (Image) */
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 1000px;
}
/* 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;
}
&#13;
<section id="responsive">
<div class="container">
<div class="row text-center">
<h2 class="os-animation" data-os-animation="zoomIn" data-os-animation-delay="0.3s">HOW DOES IT WORK</h2>
<p style="text-align: left">Exam Portal, enhanced with QFrency SA voices, provides educators with a Quick and Easy to use solution saving them time recording audio, saving parents and schools money hiring Human readers, and enabling learners to bridge a gap where there is
nepotism, and an unfair advantage under already stressful testing conditions.</p>
<video class="innerfour os-animation" data-os-animation="zoomInLeft" data-os-animation-delay="0.6s" width="50%" height="auto" controls>
<source src="Videos/main.mp4" type="video/mp4">
Your browser does not support the video tag.
</video> <br>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 col-xxs-12">
<div class="testimonial-content">
<h2 style="color:#37cdff" class="feature-content-title blue-text"><span class="blue">1</span>Drag & Drop exam</h2>
<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="img01">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>
<img class="img-responsive myImg" src="img/Link Students.png" alt="Trolltunga, Norway">
<p class="feature-content-description">Capture an exam quickly by using the Drag & Drop functionality. The user would drag the exam and drop it in the upload section. The exam will get displayed and it will keep it's original formatting.</p>
<br>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 col-xxs-12">
<div class="testimonial-content">
<h2 style="color:#37cdff" class="feature-content-title blue-text"><span class="blue">1</span>Drag & Drop exam</h2>
<img class="img-responsive myImg" src="img/Link Students.png" alt="Trolltunga, Norway">
<p class="feature-content-description">Capture an exam quickly by using the Drag & Drop functionality. The user would drag the exam and drop it in the upload section. The exam will get displayed and it will keep it's original formatting.</p>
<br>
</div>
</div>
</div>
</div>
</section>
&#13;