【问题标题】:How to open a popup image for completely different image using javascript?如何使用javascript为完全不同的图像打开弹出图像?
【发布时间】:2017-05-19 17:30:29
【问题描述】:

我有两张图片可以使用,例如rose.jpg 和mary.jpg。单击rose.jpg后,我需要弹出mary.jpg。我正在尝试在这里使用模态。如果弹出图像与要单击的图像相同,我知道该怎么做,但在这种情况下,我更大的图像具有完全不同的名称。我已经看到了多个其他示例,这些示例描述了单击图像时弹出窗口或表单而不是图像本身。 以下是我到目前为止所做的。它仅适用于相同的图像:

<html>
<head>
<style>
#myImg {
    border-radius: 5px;
    cursor: pointer;
    transition: 0.3s;
}

#myImg:hover {opacity: 0.7;}

.modal {
    display: none; 
    position: fixed; 
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    left: 0;
    top: 0;
    width: 100%; 
    height: 100%; 
    overflow: auto; 
    background-color: rgb(0,0,0);
    background-color: rgba(0,0,0,0.9); 
}

/* Modal Content (image) */
.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;
}


.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;
}
</style>
</head>
<body>
<img id="myImg" src="rose.jpg" alt="flower" >


<div id="myModal" class="modal">
  <span class="close">&times;</span>
  <img class="modal-content" src="mary.jpg" alt="mary" id="img01">
  <div id="caption"></div>
</div>
<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>
</body>
</html>

在此示例中,modalImg.src = this.src; 是弹出原始图像而不是其他图像的原因。我需要显示 mary.jpg,我已将其提供为 &lt;img class="modal-content" src="mary.jpg" alt="mary" id="img01"&gt;。请帮忙。

【问题讨论】:

    标签: javascript jquery html css image


    【解决方案1】:

    在您的 onclick 函数中,您将 img src 设置为等于 this.src

    img.onclick = function(){
        modal.style.display = "block";
        modalImg.src = this.src;
        captionText.innerHTML = this.alt;
    }
    

    通过这样做,在此用例中,每次单击图像时,都会将 img src 设置为 rose.jpg。

    如果您删除 modalImg.src = this.src;,那么您的模式应该只是简单地“打开”,通过显示 block onclick,并显示您的 HTML,它已经定义了 img src。

    <div id="myModal" class="modal">
      <span class="close">&times;</span>
      <img class="modal-content" src="mary.jpg" alt="mary" id="img01">
      <div id="caption"></div>
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-23
      • 2020-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      相关资源
      最近更新 更多