我试图在点击每张照片时在Javascript / CSS灯箱中打开我的每张照片。
目前,我的照片列表中只有第一张照片在灯箱中打开。其他照片无法在灯箱中打开。
请有人解释/告诉我为什么会这样,并解释/告诉我这样做的正确方法?
这是我的HTML / PHP:
<?php $result4 = $mysqli->query("SELECT * FROM user_data WHERE user_id = $p_id");
if($result4->num_rows > 0) {
echo '<div class="profile_gallery_image"><img id="myImg" src='.$image.' height="100%" width="100%" /></div>';
echo '<div class="profile_gallery_image"><img id="myImg" src='.$image.' height="100%" width="100%" /></div>';
echo '<div class="profile_gallery_image"><img id="myImg" src='.$image.' height="100%" width="100%" /></div>';
echo '<div class="profile_gallery_image"><img id="myImg" src='.$image.' height="100%" width="100%" /></div>';
echo '<div class="profile_gallery_image"><img id="myImg" src='.$image.' height="100%" width="100%" /></div>';
echo '<div class="profile_gallery_image"><img id="myImg" src='.$image.' height="100%" width="100%" /></div>';
} ?>
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
</div>
CSS:
<style>
#myImg {
cursor: pointer;
transition: 0.3s;
}
#myImg:hover {
transform: scale(1.09);
border: 1px solid #a5a5a5;
}
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 10; /* 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: 100%;
max-width: 500px;
border: 1px solid #f1f1f1;
border-radius: 0px;
}
/* Caption of Modal Image */
/* Add Animation */
.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: 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: 700px){
.modal-content {
width: 100%;
}
}
</style>
使用Javascript:
<script>
// Get the modal
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");
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
var span2 = document.getElementsByClassName("modal")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
span2.onclick = function() {
modal.style.display = "none";
}
</script>
提前致谢。
您的代码包含多个id,而不是尝试使用类并通过document.getElementsByClassName
获取您的图像类,然后在循环内的每个图像上绑定click事件,尝试将您的php代码添加到下面的修订代码中
// Get the modal
var modal = document.getElementById('myModal');
// Get the image and insert it inside the modal - use its "alt" text as a caption
var imgs = document.getElementsByClassName("myImg");
var modalImg = document.getElementById("img01");
for(var i = 0; i < imgs.length; i++)
{
imgs[i].onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
}
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
var span2 = document.getElementsByClassName("modal")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
span2.onclick = function() {
modal.style.display = "none";
}
#myImg {
cursor: pointer;
transition: 0.3s;
}
#myImg:hover {
transform: scale(1.09);
border: 1px solid #a5a5a5;
}
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 10; /* 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: 100%;
max-width: 500px;
border: 1px solid #f1f1f1;
border-radius: 0px;
}
/* Caption of Modal Image */
/* Add Animation */
.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: 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: 700px){
.modal-content {
width: 100%;
}
}
<div class="profile_gallery_image"><img class="myImg" src='https://via.placeholder.com/150' height="100%" width="100%" /></div>
<div class="profile_gallery_image"><img class="myImg" src='https://via.placeholder.com/150' height="100%" width="100%" /></div>
<div class="profile_gallery_image"><img class="myImg" src='https://via.placeholder.com/150' height="100%" width="100%" /></div>
<div class="profile_gallery_image"><img class="myImg" src='https://via.placeholder.com/150' height="100%" width="100%" /></div>
<div class="profile_gallery_image"><img class="myImg" src='https://via.placeholder.com/150' height="100%" width="100%" /></div>
<div class="profile_gallery_image"><img class="myImg" src='https://via.placeholder.com/150' height="100%" width="100%" /></div>
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
</div>
好消息是你的图像上既不需要id
s也不需要class
。
n
事件侦听器,我们只在图像的容器上添加一个侦听器src
元素的alt
和target
并将其传递给showModal
函数。用传递的值填充模态图像的src
和alt
看来你/你正计划在你的模态中显示一个基于传入的alt
值的标题。考虑在模态中使用figcaption
。
HTML或Figure Caption元素表示描述其父元素的其余内容的标题或图例。
这是一个让你入门的例子。我将把模态/模态内容的样式留给你。我在模态中使用了figure
元素来获取语义值。
var modal = document.getElementById('myModal');
var imgContainer = document.querySelector('.imageContainer');
var modalContentImage = modal.querySelector('.modal-content-image');
imgContainer.addEventListener('click', function(e) {
if (e.target.nodeName === "IMG") {
showModal({src: e.target.src, alt: e.target.alt})
}
});
function showModal(opts) {
modal.style.display = "block";
modalContentImage.setAttribute('src', opts.src);
modalContentImage.setAttribute('alt', opts.alt);
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
var span2 = document.getElementsByClassName("modal")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
span2.onclick = function() {
modal.style.display = "none";
}
#myImg {
cursor: pointer;
transition: 0.3s;
}
#myImg:hover {
transform: scale(1.09);
border: 1px solid #a5a5a5;
}
/* The Modal (background) */
.modal {
display: none;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 10;
/* 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-image {
margin: auto;
display: block;
width: 100%;
max-width: 500px;
border: 1px solid #f1f1f1;
border-radius: 0px;
}
/* Caption of Modal Image */
/* Add Animation */
.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: 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: 700px) {
.modal-content {
width: 100%;
}
}
/* Presentation only */
.imageContainer {
min-height: 100vh;
display: flex;
align-items: flex-end;
overflow-x: auto;
}
.imageContainer img {
width: 100px;
flex-shrink: 0;
}
html,
body {
margin: 0;
padding: 0;
}
<div id="myModal" class="modal">
<span class="close">×</span>
<figure class="modal-content">
<img class="modal-content-image" alt>
</figure>
</div>
<div class="imageContainer">
<img src="https://picsum.photos/300/300/?image=0" alt="CPU 1">
<img src="https://picsum.photos/300/300/?image=1" alt="CPU 2">
<img src="https://picsum.photos/300/300/?image=2" alt="CPU 3">
<img src="https://picsum.photos/300/300/?image=3" alt="CPU 4">
<img src="https://picsum.photos/300/300/?image=4" alt="CPU 5">
<img src="https://picsum.photos/300/300/?image=5" alt="CPU 6">
<img src="https://picsum.photos/300/300/?image=6" alt="CPU 7">
<img src="https://picsum.photos/300/300/?image=7" alt="CPU 8">
<img src="https://picsum.photos/300/300/?image=8" alt="CPU 9">
<img src="https://picsum.photos/300/300/?image=9" alt="CPU 10">
<img src="https://picsum.photos/300/300/?image=10" alt="CPU 11">
<img src="https://picsum.photos/300/300/?image=11" alt="CPU 12">
</div>