作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建模态图像,以便当您单击该图像时,它会在弹出窗口中打开更大的图像。
我的代码(见下文)只允许我在弹出窗口中只打开一张图片。它不允许我打开其余的图像,
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>
请帮忙。谢谢
最佳答案
正如 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;
}
})();
#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;
}
<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>
关于javascript - 使用 JS 的模态图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45238267/
我来自 Asp.Net 世界,试图理解 Angular State 的含义。 什么是 Angular 状态?它类似于Asp.Net中的ascx组件吗?是子页面吗?它类似于工作流程状态吗? 我听到很多人
我一直在寻找 3 态拨动开关,但运气不佳。 基本上我需要一个具有以下状态的开关: |开 |不适用 |关 | slider 默认从中间开始,一旦用户向左或向右滑动,就无法回到N/A(未回答)状态。 有人
我是一名优秀的程序员,十分优秀!