[英]Display PHP images from directory with modal popup viewer
id如何呈现到页面:
下面的代码将从“ resources / php / disImg”目录中按顺序显示所有照片。
我的目标是单击图像,并在目录中以图像模式显示“ onclick”图像。
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
</div>
<!-- Displays all photos from folder -->
<div class="containerPhotos">
<?php
$dirname = "resources/php/disImg/";
$images = glob($dirname."*.{jpg,jpeg,png}",GLOB_BRACE);
natcasesort($images);
foreach($images as $randomImage) {
echo '<img id="myImg" src="'.$randomImage.'" class="photo" />';
}
?>
</div>
目前,我能够使用PHP / image目录中的(id =“ myImg”)定位第一个图像,并将其显示在模式中。
单击第一张照片后的模态弹出窗口:
<script>
// Get the modal
var modal = document.getElementById('myModal');
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];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
</script>
任何人都知道如何制作它,所以无论我用id =“ myImg”单击的图像都将弹出并显示在模态内部?
我尚无法使用:nth select解决方案,但我认为这可行。
以下是模式弹出窗口的.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: 100%;
max-width: 1000px;
}
/* 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%;
}
}
有几种不同的方法可以完成此操作,但第一个方法是在PHP foreach循环中为每个img HTML元素添加一个onclick事件。
foreach($images as $randomImage) {
echo '<img id="myImg" onclick="showImage(this)" src="'.$randomImage.'" class="photo" />';
}
然后,您将需要一个同名的javascript函数,并可以像这样获得源字符串。
function showImage(imgElement) {
var src = imgElement.getAttribute("src");
/* Do the stuff with your modal */}
感谢您的帮助,这是最终的构建!
Javascript /
var modal = document.getElementById('myModal');
var modalImg = document.getElementById('img01');
function showImage(imgElement) {
var src = imgElement.getAttribute("src");
modal.style.display = "block";
modalImg.src = src;
}
// 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";
}
PHP /
foreach($images as $randomImage) {
echo '<img class="photo" onclick="showImage(this)" id="myImg" src="'.$randomImage.'" />';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.