Bootstrap图像模式
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'">×</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'">×</span>
<!-- Modal Content (The Image) -->
<img class="modal-content" id="modalImage" src="">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>
相关文章:
- Javascript:为页面上的所有图像制作模式弹出窗口
- 将图像文件存储在猫鼬模式中的二进制数据中,并以html形式显示图像
- 模式上的后退和前进按钮位于图像库上(可滚动浏览)
- 缩略图单击时图像未以模式显示
- 使用模式在 HTML5/JS 中绘制图像
- 单击按钮并将其下载为图像格式时,如何使用引导模式弹出窗口显示D3图表
- 尝试单击图像并打开模式ajax
- TinyMCE图像调整大小模式
- 如何在模式窗口中从html表单文件类型打开图像
- Regex将标记图像模式与给定的文件名相匹配
- 如何使用 Javascript 在引导模式中显示图像
- 响应式设计:纵向/横向模式下的图像
- 用于编写用于使用 node.js 上传图像的筛选器列表的设计模式
- 使用引导模式弹出的图像
- 如何防止在响应模式下图像换行
- 严格模式下的图像数据数据分配
- 在“JQuery 模式”对话框中指定图像单击侦听器
- 在生产模式下运行时,图像不会显示在 yeoman 应用中
- 响应式文件管理器.防止在选择图像时模式关闭
- preserveAspectRatio模式图像在SVG中,如css背景图像覆盖