Bootstrap图像模式

Bootstrap Image Modal

本文关键字:模式 图像 Bootstrap      更新时间:2023-09-26

有没有人知道如何从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>