簡體   English   中英

引導映像模態

[英]Bootstrap Image Modal

有誰知道如何從w3schools進行此示例 ,但帶有3個縮略圖

到目前為止,這是我的代碼:

<div class="row">
    <div class="col-md-4 ">

      <!-- Trigger the Modal -->
      <div class="polaroid">
        <img id="myImg" src="home-page-feature-thumbnail-image-front-featured-films-slider-2.jpg" alt="Caption1" width="auto;" height="auto;">

        <div class="info">
          <p style="color:#971604; font-weight:bold;">This is a text.</p>
          <h4>Caption 1</h4>
        </div>
      </div>

      <!-- The Modal -->
      <div id="myModal" class="modal">

        <!-- The Close Button -->
        <span class="close" >×</span>


        <!-- Modal Content (The Image) -->
        <img class="modal-content" id="img01" src="img.jpg">

        <!-- Modal Caption (Image Text) -->
        <div id="caption"></div>
      </div>
    </div>

    <div class="col-md-4">

      <!-- Trigger the Modal -->
      <div class="polaroid">
        <img id="myImg2" src="home-page-feature-thumbnail-image-front-featured-films-slider-4.jpg" alt="Caption2" width="auto;" height="auto;">
        <div class="info">
          <p style="color:#971604; font-weight:bold;">This is another text.</p>
          <h4>Caption 2</h4>
        </div>

      </div

      <!-- The Modal -->
      <div id="myModal" class="modal">

        <!-- The Close Button -->
        <span class="close" >×</span>

        <!-- Modal Content (The Image) -->
        <img class="modal-content" id="img02" src="img2.jpg">

        <!-- Modal Caption (Image Text) -->
        <div id="caption"></div>
      </div>
    </div>

    <div class="col-md-4">
      <!-- Trigger the Modal -->
       <div class="polaroid">
        <img id="myImg" src="home-page-feature-thumbnail-image-front-featured-films-slider-2.jpg" alt="Caption3" width="auto;" height="auto">

        <div class="info">
          <p style="color:#971604; font-weight:bold;">This is the 3rd text.</p>
          <h4>Caption 3</h4>
        </div>
      </div>

      <!-- The Modal -->
      <div id="myModal" class="modal">

        <!-- The Close Button -->
        <span class="close" onclick="document.getElementById('myModal').style.display='none'">&times;</span>

        <!-- Modal Content (The Image) -->
        <img class="modal-content" id="img03" src="img3.jpg">

        <!-- Modal Caption (Image Text) -->
        <div id="caption"></div>
      </div>
    </div>

  </div>

這就是我的JavaScript所擁有的

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

這是我的css文件中的內容:

/* Style the Image Used to Trigger the Modal */

div.polaroid {
  width: 100%;
  background-color: white;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  margin-bottom: 25px;
}

div.info {
 padding: 10px 20px;
} 

/* 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: 35px;
     right: 25px;
     color: #ffffff;
     font-size: 150px;
     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%;
         }
     }

對於第一個圖像,一切正常,但對於其他兩個圖像,則沒有模態出現。 任何幫助,將不勝感激。

您無需為每個圖像添加模態,只需一個模態就足夠了。 在您的代碼中,模態僅適用於第一張圖片,因為您的js代碼僅處理第一張圖片。 查找以下適用於三張圖片的代碼。

  function handleImageClick(c){ var modal = document.getElementById('myModal'); var modalImg = document.getElementById("modalImage"); var captionText = document.getElementById("caption"); modal.style.display = "block"; modalImg.src = c.src; captionText.innerHTML = c.alt; } 
 div.polaroid { width: 100%; background-color: white; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); margin-bottom: 25px; } div.info { padding: 10px 20px; } /* 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: 35px; right: 25px; color: #ffffff; font-size: 150px; 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%; } } 
 <div class="row"> <div class="col-md-4 "> <div class="polaroid"> <img id="myImg" onClick="handleImageClick(this);" src="http://www.w3schools.com/howto/img_fjords.jpg" alt="Caption1" width="auto;" height="auto;"> <div class="info"> <p style="color:#971604; font-weight:bold;">This is a text.</p> <h4>Caption 1</h4> </div> </div> </div> <div class="col-md-4"> <div class="polaroid"> <img id="myImg2" onClick="handleImageClick(this);" src="http://www.w3schools.com/howto/img_fjords.jpg" alt="Caption2" width="auto;" height="auto;"> <div class="info"> <p style="color:#971604; font-weight:bold;">This is another text.</p> <h4>Caption 2</h4> </div> </div> </div> <div class="col-md-4"> <div class="polaroid"> <img id="myImg" onClick="handleImageClick(this);" src="http://www.w3schools.com/howto/img_fjords.jpg" alt="Caption3" width="auto;" height="auto"> <div class="info"> <p style="color:#971604; font-weight:bold;">This is the 3rd text.</p> <h4>Caption 3</h4> </div> </div> </div> </div> <div id="myModal" class="modal"> <!-- The Close Button --> <span class="close" onclick="document.getElementById('myModal').style.display='none'">&times;</span> <!-- Modal Content (The Image) --> <img class="modal-content" id="modalImage" src=""> <!-- Modal Caption (Image Text) --> <div id="caption"></div> </div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM