javascript - 一键获取模态图像

标签 javascript php html css

我正在尝试制作脚本,例如: 我从 sql 表的 id 获取图像,我得到列表图像

图片1 |图片2 |图3 ......

我尝试在单击 Image1 时以模态显示此图像。我有示例脚本,但是当我单击 image2 时,我无法获取图像并单击。只为 Image1 工作 有什么帮助吗?

我的示例脚本:

调用函数:->

<img src="<?php echo $product_array[$key]["image"]; ?>" style="max- 
           width:100px;max-height:75px;" id="myImg"></div>
        <div id="myModal" class="modal">
        <span class="close">&times;</span>
        <img class="modal-content" id="img01">
        <div id="caption"></div>
        </div>

Javascript 函数 ->

<script>
var modal = document.getElementById('myModal');
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;
}
var span = document.getElementsByClassName("close")[0];
span.onclick = function() { 
    modal.style.display = "none";
}
</script>

CSS 样式:

<style>

#myImg {
    border-radius: 5px;
    cursor: pointer;
    transition: 0.3s;
}

#myImg:hover {opacity: 0.7;
}

.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 */

}

.modal-content {
    margin: auto;
    display: block;
    width: 80%;
    max-width: 700px;
}

#caption {
    margin: auto;
    display: block;
    width: 80%;
    max-width: 700px;
    text-align: center;
    color: #ccc;
    padding: 10px 0;
    height: 150px;
}

.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)}
}

.close {
    background:red;
    position: absolute;
    top: 63px;
    right: 275px;
    color: #f1f1f1;
    font-size: 40px;
    font-weight: bold;
    transition: 0.3s;
}

.close:hover,
.close:focus {
    color: #bbb;
    text-decoration: none;
    cursor: pointer;
}

@media only screen and (max-width: 700px){
    .modal-content {
        width: 100%;
    }
}
</style>

以及我如何将另一张图片放到同一个 id 中,例如:

图片1 |图片2 |图3

我单击 Image1 并在模态中获得模态和下侧显示此 Image1 的更多图像 (1),2,3 并且当我单击 (2) 时,只需保持模态并将此图像更改为 id Image1 和第二个图像这……也许有人懂我的意思。对不起我的语言。

最佳答案

如果您想在每个图像中打开相同的模型,请单击然后请使用 class 而不是使用 id。 或者,如果您想在每个图像上打开不同的模型,请单击,那么您必须创建单独的模型,其中包含 id,如 myModal1、myModal2、.. 和图像 id,如 myImg1、myImg2、.. 之后,您必须编写代码来检测图像点击次数,根据您必须打开模型的次数。

关于javascript - 一键获取模态图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49911234/

相关文章:

javascript - 使用 Javascript 检查是否仍连接到主机

javascript - 将两个段落并排放置?

php - 使用 PHP 在 MySQL 中查找 "id"行

javascript - switchMap 返回一项

javascript - 动画调整 jquery 对话框的内容

php - 如何使用 PHP echo 函数在 JavaScript 中定义变量?

php - 搜索并比较两个表的行

javascript - 如何从鼠标指针创建 "aura"效果?

javascript - 如何在jquery中使用相同的类将子div同等地附加到父div?

javascript - jquery 验证检查 3 个复选框的状态均已选中,然后启用按钮