gpt4 book ai didi

javascript - 使用模式弹出查看器显示目录中的 PHP 图像

转载 作者:行者123 更新时间:2023-12-03 03:30:10 24 4
gpt4 key购买 nike

id 如何呈现到页面:

enter image description here

下面的代码将按顺序显示“resources/php/disImg”目录中的所有照片。

我的目标是单击图像并在目录中的图像模式中显示“onclick”图像。

<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">&times;</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") 定位第一张图像,并将其显示在模式中。

点击第一张照片后弹出模态窗口:

enter image description here

<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 - 使用模式弹出查看器显示目录中的 PHP 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46135991/

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