2017-02-11 7 views
0

私は画像のグリッドを備えたウェブサイトを作成しています。私はクリックしたときに各画像をモーダルに展開したい。私は1つのイメージで作業するようにしましたが、各イメージに対して新しいモーダルと新しいmyImgと新しいスクリプトを作成する必要があります。とにかく私はちょうど1つのモーダルと1つのmyImgを持つことができますし、各イメージに対して新しいモーダルとmyImgを作成する必要がないように、一般的に各イメージに対して動作させるためのスクリプトを作成します。グリッドのモーダル画像のグリッドを作成する方法は?

例:

ソースコード:

<!DOCTYPE html> 

<html> 
    <?php 
     $myfile = fopen("header.html", "r") or die("Unable to open file!"); 
     echo fread($myfile,filesize("header.html")); 
     fclose($myfile); 
    ?> 
    <style> 
     .img { 
      font-size: 0; 
     } 

     a1 { 
      font-size: 16px; 
      display: inline-block; 
      margin-bottom: 8px; 
      width: calc(50% - 4px); 
      margin-right: 8px; 
     } 

     a1:nth-of-type(2n) { 
      margin-right: 0; 
     } 

     @media screen and (min-width: 50em) { 
      a1 { 
       width: calc(25% - 6px); 
      } 

      a1:nth-of-type(2n) { 
       margin-right: 8px; 
      } 

      a1:nth-of-type(4n) { 
       margin-right: 0; 
      } 
     } 

     img { 
      border: none; 
      max-width: 100%; 
      height: auto; 
      display: block; 
      background: #ccc; 
      transition: transform .2s ease-in-out; 
     } 

     figure { 
      margin: 0; 
      overflow: hidden; 
     } 

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

     /* 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%; 
      } 
     } 


     #myImg { 
      border-radius: 5px; 
      cursor: pointer; 
      transition: 0.3s; 
     } 

     #myImg:hover {opacity: 0.7;} 

     #myImg2 { 
      border-radius: 5px; 
      cursor: pointer; 
      transition: 0.3s; 
     } 

     #myImg2:hover {opacity: 0.7;} 

    </style> 
    <body> 

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

     <div id="myModal2" class="modal"> 
      <span class="close">&times;</span> 
      <img class="modal-content" id="img02"> 
      <div id="caption"></div> 
     </div> 

     <div class="img"> 
      <a1 href="abe_lincoln_riding_a_grizzly.png"> 
       <figure> 
        <img id="myImg" src="abe_lincoln_riding_a_grizzly.png" alt=""> 
       </figure> 
      </a1> 

      <a1 href="abe_lincoln_riding_a_grizzly.png"> 
       <figure> 
        <img id="myImg2" src="abe_lincoln_riding_a_grizzly.png" alt=""> 
       </figure> 
      </a1> 

      <a1 href="abe_lincoln_riding_a_grizzly.png"> 
       <figure> 
        <img src="abe_lincoln_riding_a_grizzly.png" alt=""> 
       </figure> 
      </a1> 

      <a1 href="abe_lincoln_riding_a_grizzly.png"> 
       <figure> 
        <img src="abe_lincoln_riding_a_grizzly.png" alt=""> 
       </figure> 
      </a1> 

      <a1 href="abe_lincoln_riding_a_grizzly.png"> 
       <figure> 
        <img src="abe_lincoln_riding_a_grizzly.png" alt=""> 
       </figure> 
      </a1> 

      <a1 href="abe_lincoln_riding_a_grizzly.png"> 
       <figure> 
        <img src="abe_lincoln_riding_a_grizzly.png" alt=""> 
       </figure> 
      </a1> 

      <a1 href="abe_lincoln_riding_a_grizzly.png"> 
       <figure> 
        <img src="abe_lincoln_riding_a_grizzly.png" alt=""> 
       </figure> 
      </a1> 

      <a1 href="abe_lincoln_riding_a_grizzly.png"> 
       <figure> 
        <img src="abe_lincoln_riding_a_grizzly.png" alt=""> 
       </figure> 
      </a1> 

      <a1 href="abe_lincoln_riding_a_grizzly.png"> 
       <figure> 
        <img src="abe_lincoln_riding_a_grizzly.png" alt=""> 
       </figure> 
      </a1> 
     </div> 

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

      // Get the image and insert it inside the modal - use its "alt" text as a caption 
      var img = document.getElementById('myImg2'); 
      var modalImg = document.getElementById("img02"); 
      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> 

     <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> 
    </body> 

</html> 

答えて

0

使用jQueryのクリックされたIMGのIDおよび情報を取得し、idと情報パラメータでモーダルを作成し、idとの情報を渡すために機能させますimgをクリックして機能させてからvarsに追加してください。例:

var modalImg = document.getElementById("img" + imgIdNumber); 
etc... 

このように動的になります。

関連する問題