我是JS的新手,我试图在这个代码的同一页面上使用2个模态图像:
https://www.w3schools.com/howto/howto_css_modal_images.asp
使用此HTML,JS和CSS视图大小调整仅适用于一个图像,不知道如何使其适用于同一页面上的两个图像:
我为每张图片首次提供两次HTML代码:
HTML:
<img id="myImg" src="img_fjords.jpg" alt="Trolltunga, Norway" width="300" height="200">
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>
big-picture.css中的CSS:
#myImg {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}
#myImg:hover {opacity: 0.7;}
.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 {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}
.modal-content, #caption {
animation-name: zoom;
animation-duration: 0.6s;
}
@keyframes zoom {
from {transform:scale(0)}
to {transform:scale(1)}
}
.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;
}
@media only screen and (max-width: 700px){
.modal-content {
width: 100%;
}
}
和JS:
var modal = document.getElementById('myModal');
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;
}
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}
答案 0 :(得分:1)
您所拥有的代码主要是有效的,但您具体指的是单个图像。通过使用document.getElementById(),您无法获得第二个图像。我稍微改变了你的代码 - HTML现在有一些图片用于示例目的,JS现在使用document.getElementsByClassName()来允许多个图像。此外,我迭代图像列表,并将该事件处理程序附加到每个图像。 See it as a fiddle ,或者在这里:
// create references to the modal...
var modal = document.getElementById('myModal');
// to all images -- note I'm using a class!
var images = document.getElementsByClassName('myImages');
// the image in the modal
var modalImg = document.getElementById("img01");
// and the caption in the modal
var captionText = document.getElementById("caption");
// Go through all of the images with our custom class
for (var i = 0; i < images.length; i++) {
var img = images[i];
// and attach our click listener for this image.
img.onclick = function(evt) {
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
}
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}
&#13;
#myImg {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}
#myImg:hover {
opacity: 0.7;
}
.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 {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}
.modal-content,
#caption {
animation-name: zoom;
animation-duration: 0.6s;
}
@keyframes zoom {
from {
transform: scale(0)
}
to {
transform: scale(1)
}
}
.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;
}
@media only screen and (max-width: 700px) {
.modal-content {
width: 100%;
}
}
&#13;
<img class="myImages" id="myImg" src="http://onebigphoto.com/uploads/2012/10/midnight-sun-in-lofoten-norway.jpg" alt="Midnight sun in Lofoten, Norway" width="300" height="200">
<img class="myImages" id="myImg" src="http://cdn-image.travelandleisure.com/sites/default/files/styles/1600x1000/public/1490029386/fisherman-cabin-hamnoy-lofoten-islands-norway-NORWAY0320.jpg?itok=cpPuUjh1" alt="Fishermen's cabins in Lofoten, Norway"
width="300" height="200">
<img class="myImages" id="myImg" src="http://fjordtours.blob.core.windows.net/fjordtours-umbraco/1199/gerirangerfjord-per-ottar-walderhaug-fjordnorway.jpg" alt="Gerirangerfjord, Norway" width="300" height="200">
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>
&#13;
答案 1 :(得分:0)
也不需要复制模式div,您可以使用类名通过getElementsByClassName
选择器选择两个图像,然后将click事件绑定到每个图像:
<强> HTML 强>
<img class="myImg" src="img_fjords.jpg" alt="Trolltunga, Norway">
<img class="myImg" src="img_mountains_wide.jpg" alt="Mountain, Norway">
<强> JS 强>
// select images by class name
var img = document.getElementsByClassName('myImg');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
// show modal function
var showModal = function(){
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
// bind click event to each img
for (var i = 0; i < img.length; i++) {
img[i].addEventListener('click', showModal);
}
这是一个有效的例子:
// 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.getElementsByClassName('myImg');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
var showModal = function(){
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
for (var i = 0; i < img.length; i++) {
img[i].addEventListener('click', showModal);
}
// 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";
}
#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%;
}
}
<img class="myImg" src="https://www.w3schools.com/howto/img_fjords.jpg" alt="Trolltunga, Norway" width="300" height="200">
<img class="myImg" src="https://www.w3schools.com/howto/img_mountains_wide.jpg" alt="Mountain, Norway" width="300" height="200">
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>
答案 2 :(得分:0)
你必须申请课程名称&#34; .modal-content&#34;也是你的第二张照片。 动画是因为CSS Key Frames而发生的。
或作为替代方案 - 如果您的第二张图片有不同的类名或ID,请将其添加到以下CSS代码
.modal-content, #caption,<YOUR NEW IMAGE CLASS or ID> {
animation-name: zoom;
animation-duration: 0.6s;
}
答案 3 :(得分:0)
这是一个老问题,但我想出了自己的解决方案,使用 data-modal
HTML 属性。
document.querySelectorAll("img[data-modal]").forEach((img) => {
var content = document.createElement("div"); //background
content.className = "modal-content";
content.appendChild(new Image()).src = img.src; //modal image
img.insertAdjacentElement("afterend", content); //insert invisible content after image
img.addEventListener("click", () => { //show modal on click
content.style.opacity = "1";
content.style.zIndex = "100";
document.documentElement.style.overflow = "hidden"; //prevent scrolling while modal is shown
});
content.addEventListener("click", () => { //hide modal
content.style.opacity = "";
content.style.zIndex = "";
document.documentElement.style.overflow = "";
});
});
img[data-modal] {
cursor: zoom-in;
}
.modal-content {
z-index: -100; /* behind everything */
opacity: 0; /* invisible */
position: fixed;
top: 0;
left: 0;
background: rgba(0, 0, 0, 0.8); /* black, transparent */
width: 100vw; /* 100% width */
height: 100vh; /* 100% height */
display: flex;
justify-content: center; /* horizontal align */
align-items: center; /* vertical align */
transition: 0.5s; /* animate */
cursor: zoom-out;
}
.modal-content img {
min-width: 50vw; /* at least 50% of the viewport width */
height: auto;
}
<h1>Press "Full Page" to see effect</h1>
<img
src="https://icatcare.org/app/uploads/2018/06/Layer-1704-1200x630.jpg"
data-modal
/>
<img
src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/golden-retriever-royalty-free-image-506756303-1560962726.jpg?crop=0.672xw:1.00xh;0.166xw,0&resize=640:*"
data-modal
/>
注意:这不包括包含标题的方法,但如果您需要,可以将行 content.appendChild(new Image()).src = img.src;
替换为:
var fig = document.createElement("figure");
fig.appendChild(new Image()).src = img.src;
fig.appendChild(document.createElement("figcaption")).textContent = img.alt;
content.appendChild(fig);
然后您可以添加一些 CSS 来设置图形样式,也许使用 text-align: center
。