gpt4 book ai didi

javascript - 使用 JS 的模态图像

转载 作者:行者123 更新时间:2023-11-28 02:39:38 25 4
gpt4 key购买 nike

我正在尝试创建模态图像,以便当您单击该图像时,它会在弹出窗口中打开更大的图像。

我的代码(见下文)只允许我在弹出窗口中只打开一张图片。它不允许我打开其余的图像,

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'">&times;</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'">&times;</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'">&times;</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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com