基于无线电选择的图像显示和模态弹出

时间:2017-10-09 18:13:03

标签: javascript jquery css

我有一个脚本,我根据单选按钮选择循环各种图像。这很好用,可以毫无问题地循环使用它们。

我的问题是我想为所有图像弹出一个模态。这些图像名称会根据显示的项目而更改,因此我将图像名称保存在一个数组中(n个数组的数组。

这是无线电代码:

<ul>
<li class="radio-attribute">
<label for="2" class="radio-attribute">
<input type="radio" id="2" name="id[27]" value="175" onClick="change_image(this.id)">
<img src="images/blue_swatch.png" alt="GBM0911B Blue" title=" GBM0911B Blue " width="45" height="40" />
</label>
</li>

<li class="radio-attribute">
<label for="3" class="radio-attribute">
<input type="radio" id="3" name="id[27]" value="173" onClick="change_image(this.id)">
<img src="images/black_swatch.png" alt="GBM0911BK Black" title=" GBM0911BK Black " width="41" height="40" />
</label>
</li>

</ul>   

这些按钮工作正常。

javascript如下:

var images = ["GBM0911PL.png","GBM0911R.png"];
var modal = document.getElementById('myModal');
var img = document.getElementById('myImg');
var modalImg = document.getElementById("piGal");
var captionText = document.getElementById("caption");

    function change_image(radioID) {
        document.getElementById("piGal").innerHTML = "<img id=\"myImg\" src=\"images/"+ images[radioID] +"\" />";
    }

// Get the modal

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

    // 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";
    }

模态标签如下:

<!-- The Modal -->
<div id="myModal" class="modal">
  <!-- The Close Button -->
  <span class="close" onclick="document.getElementById('piGal').style.display='none'">&times;</span>
  <!-- Modal Content (The Image) -->
  <img class="modal-content" id="piGal">
  <!-- Modal Caption (Image Text) -->
  <div id="caption"></div>
</div>

我显示如下图像:

    <div id="piGal" style="float: left;">

      <a href="images/GLAMOURFRONT.jpg"><img src="images/GLAMOURFRONT.jpg" alt="Glamour Bubble " title=" Glamour " width="225" height="184"  id="myImg"  /></a>    
</div>

最后用于模态的css:

 /* Style the Image Used to Trigger the Modal */
#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: 80%;
    max-width: 700px;
}

/* Caption of Modal Image (Image Text) - Same Width as the Image */
#caption {
    margin: auto;
    display: block;
    width: 80%;
    max-width: 700px;
    text-align: center;
    color: #ccc;
    padding: 10px 0;
    height: 150px;
}

/* Add Animation - Zoom in the Modal */
.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%;
    }
} 

这只是我从网站https://www.w3schools.com/howto/howto_css_modal_images.asp复制的一种模式 开始这个。

模态虽然根本没有弹出,我需要它从收音机选择中弹出选定的图像。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

这是因为您的onclick事件侦听器设置为仅在您单击ID为#myImg的项目时触发。您希望每次单击每个单选框时都执行代码,因此我建议循环使用这些单选按钮:

document.querySelectorAll('label.radio-attribute').forEach(function (radioClick) {
  radioClick.querySelector('img').addEventListener("click", function () {
    modal.style.display = "block";
    img.src = this.src;
    captionText.innerHTML = this.alt;
  });
});

基本上,这会循环遍历每个无线电img并添加一个事件监听器以便点击它,然后设置SRC并在全屏视图中显示目标图像。

答案 1 :(得分:0)

尝试删除:

<a href="images/GLAMOURFRONT.jpg"></a>

我认为这会阻止您的图像触发模态。