2017-10-09 16 views
0

ラジオボタンの選択に基づいてさまざまな画像を循環させるスクリプトがあります。これはうまく動作し、それらをすべて問題なく循環します。ラジオの選択に基づいて画像表示とモーダルポップアップ

私の問題は、すべての画像に対してモーダルポップアップを表示したいということです。これらの画像名が表示された項目に基づいて、私は、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> 

これらのボタンは正常に動作

ジャバスクリプトは以下の通りである:

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

モーダルタグは以下の通りである:

私はこのような画像を表示します。

<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

これは、IDが#myImgのアイテムをクリックすると、onclickイベントリスナーがトリガーするように設定されているためです。あなたは、あなたのラジオボックスのそれぞれをクリックすると、コードが実行したいので、私は、これらのラジオボタンをループをお勧めします:

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 Fiddle](https://jsfiddle.net/)を設定できたら、誤ってレンダリングされたビューを手助けできると確信しています。 –

+0

前にjsfiddleを使ったことがありません。 – Peter

0

は削除してください:

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

私は、これは、モーダルをトリガからあなたのイメージを妨げていると思います。

+0

を持っている可能性があります。そうではありませんでした。しかし、ありがとう。 – Peter

関連する問題