2017-12-14 7 views
1

2番目の画像をクリックしたときにポップアップ表示されるようにする方法を解明しようとしていました。第2の画像は第1の画像と全く同じコードを有し、その場合でも同じポップアップ効果を発揮しない。私は悪い英語のために申し訳ありません。私は日本から来ました。私は助けてくれた人に感謝します。2番目の画像をクリックすると、最初の画像としてポップアップしないのはなぜですか?

#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 */ 
 
#caption { 
 
    margin: auto; 
 
    display: block; 
 
    width: 80%; 
 
    max-width: 700px; 
 
    text-align: center; 
 
    color: #ccc; 
 
    padding: 10px 0; 
 
    height: 150px; 
 
} 
 

 
/* Add Animation */ 
 
.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%; 
 
    } 
 
}
<h2>Image Modal</h2> 
 
<p>In this example, we use CSS to create a modal (dialog box) that is hidden by default.</p> 
 
<p>We use JavaScript to trigger the modal and to display the current image inside the modal when it is clicked on. Also note that we use the value from the image's "alt" attribute as an image caption text inside the modal.</p> 
 

 
<img id="myImg" src="https://cdn.theatlantic.com/assets/media/img/mt/2017/06/GettyImages_675371680/lead_960.jpg?1498239007" alt="Trolltunga, Norway" width="300" height="200"> 
 

 
<img id="myImg" src="https://cdn.theatlantic.com/assets/media/img/mt/2017/06/GettyImages_675371680/lead_960.jpg?1498239007" alt="Trolltunga, Norway" width="300" height="200"> 
 

 
<!-- The Modal --> 
 
<div id="myModal" class="modal"> 
 
    <span class="close">&times;</span> 
 
    <img class="modal-content" id="img01"> 
 
    <div id="caption"></div> 
 
</div> 
 

 

 
<script> 
 
// Get the modal 
 
var modal = document.getElementById('myModal'); 
 

 
// Get the image and insert it inside the modal - use its "alt" text as a caption 
 
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; 
 
} 
 

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

+0

の代わりにdocument.querySelectorAllを使用する必要があります間違っているmyImgであることid同じを持っていますAnswer –

答えて

2

document.getElementById一度に一つの要素を選択することができ、あなたは、要素の一意のIDを使用する必要があります。

この場合、試したいことの1つはimg要素にclass="imgs"を追加し、document.querySelectorAllまたはdocument.getElementsByClassNameのようなセレクタ機能を使用することです。次に、forループによって各画像にスタイルを適用することができます。

+0

私はクラスのid "myImg"を変更しましたが、document.getElementsByClassNameのセレクタ関数を変更してもまだ動作しません。 –

+0

@ numero1私の答えは更新されました。 –

3

同じIDを異なる要素に設定しないでください。 IDは一意でなければなりません。 var img = document.getElementById('myImg');を使用している場合最初の要素のみを選択しています。あなたがそれを行うことができます

ソリューション

一つの方法は、両方の要素に同じクラスを設定しています。

<img class="images" id="myImg" src="https://cdn.theatlantic.com/assets/media/img/mt/2017/06/GettyImages_675371680/lead_960.jpg?1498239007" alt="Trolltunga, Norway" width="300" height="200"> 
<img class="images" id="myImg" src="https://cdn.theatlantic.com/assets/media/img/mt/2017/06/GettyImages_675371680/lead_960.jpg?1498239007" alt="Trolltunga, Norway" width="300" height="200"> 

getElementsByClassNameで両方の要素を取得できるようになりました。

HTMLコレクションを取得して、それを配列に解析する必要があります。

var imagesCollection = document.getElementsByClassName('images'); 
var imagesArray = Array.prototype.slice.call(imagesCollection); 

最後のステップは、あなたがmap

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

imagesArray.map(function (img){ 
    img.onclick = imageOnClickHandler; 
}) 

完全なソースコード

#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 */ 
 
#caption { 
 
    margin: auto; 
 
    display: block; 
 
    width: 80%; 
 
    max-width: 700px; 
 
    text-align: center; 
 
    color: #ccc; 
 
    padding: 10px 0; 
 
    height: 150px; 
 
} 
 

 
/* Add Animation */ 
 
.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%; 
 
    } 
 
}
<h2>Image Modal</h2> 
 
<p>In this example, we use CSS to create a modal (dialog box) that is hidden by default.</p> 
 
<p>We use JavaScript to trigger the modal and to display the current image inside the modal when it is clicked on. Also note that we use the value from the image's "alt" attribute as an image caption text inside the modal.</p> 
 

 
<img class="images" id="myImg" src="https://cdn.theatlantic.com/assets/media/img/mt/2017/06/GettyImages_675371680/lead_960.jpg?1498239007" alt="Trolltunga, Norway" width="300" height="200"> 
 

 
<img class="images" id="myImg" src="https://cdn.theatlantic.com/assets/media/img/mt/2017/06/GettyImages_675371680/lead_960.jpg?1498239007" alt="Trolltunga, Norway" width="300" height="200"> 
 
<link rel="stylesheet" href="/playground/modal.css"> 
 
<!-- The Modal --> 
 
<div id="myModal" class="modal"> 
 
    <span class="close">&times;</span> 
 
    <img class="modal-content" id="img01"> 
 
    <div id="caption"></div> 
 
</div> 
 

 

 
<script> 
 
// Get the modal 
 
var modal = document.getElementById('myModal'); 
 

 
// Get the image and insert it inside the modal - use its "alt" text as a caption 
 
var imagesCollection = document.getElementsByClassName('images'); 
 
var imagesArray = Array.prototype.slice.call(imagesCollection) 
 

 
var modalImg = document.getElementById("img01"); 
 
var captionText = document.getElementById("caption"); 
 

 
imagesArray.map(function (img){ 
 
    img.onclick = imageOnClickHandler; 
 
}) 
 

 

 
function imageOnClickHandler(){ 
 
    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"; 
 
} 
 
</script>

との両方の要素でそれを設定することができるようにonclickのハンドラに名前を付けています
+0

@ColinCline - この答えは同じではありません問題の2つの要素を引用し、問題の原因を説明します。 (問題を解決するための具体的なコードは提供していませんが、その説明がそれをカバーしていれば本質的に悪いことではありません)。 – nnnnnn

+0

@nnnnnnあなたは正しいですが、ヘルプセンターに基づいていますコメントの中で最も言及されたものを診断する。彼は次のような見出しのない問題をコピー/ペーストします:これは問題またはw/eです。これは混乱を招く。 –

+0

@ColinCline完全な解決策が含まれるようになりました。 –

1

両方i魔術師はidあなたは `img`タグの両方に同じIDを使用しますが、修正が必要な場合は、私はそれを投稿することができ、クラスに固有の変更を可能と

document.getElementById 
関連する問題