2017-10-09 75 views
0

我有一个脚本,我根据单选按钮选择循环各种图像。这很好地工作,并循环所有这些都没有问题。基于收音机选择的图像显示和模式弹出

我的问题是,我想有一个模式弹出所有的图像。这些图像名称更改基于上显示的项目,所以我保存图像的名字在正大小的数组(阵列

这里是无线电代码:。

<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 得到这个开始。

该模式虽然并没有弹出窗口,我需要它弹出打开选定的图像从收音机选择。

任何帮助,将不胜感激。

回答

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并显示您的目标图像。

+0

感谢几乎,只要我现在单击一个收音机选择,模式立即打开,经文有鼠标滚动点击图像,此外模式中的图像丢失。 – Peter

+0

我不确定你的意思。如果你可以用一些示例代码设置一个[JS小提琴](https://jsfiddle.net/),我相信我会更有能力帮助你解决错误的观点。 –

+0

试试吧,从来没有用过jsfiddle .. – Peter

0

尝试删除:

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

我认为这是防止触发模式的图像。

+0

是的,这不行。谢谢。 – Peter