简体   繁体   English

如何使用 JavaScript 获得多个 model 图像弹出窗口。 我正在使用 W3School model 图像

[英]How I can I get multiple model image popups using JavaScript. I'm using W3School model image

I was trying to get multiple image models, but I am getting only one Pop up.我试图获得多个图像模型,但我只得到一个弹出窗口。 Please help to get the multiple pop-ups.请帮助获取多个弹出窗口。

// 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("img");
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";
}
#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%;
  }
}
<h2>Image Modal</h2>
<p>In this example, we use CSS to create a modal (dialog box) that is hidden by default.</p>
<p>We use JavaScript to trigger the modal and to display the current image inside the modal when it is clicked on. Also note that we use the value from the image's "alt" attribute as an image caption text inside the modal.</p>
<img id="myImg" src="https://www.w3schools.com/howto/img_snow.jpg" alt="Snow" style="width:100%;max-width:300px">
<img id="myImg" src="https://www.w3schools.com/howto/img_snow.jpg" alt="Snow" style="width:100%;max-width:300px">

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

IMROVEMENT START ========>改进开始========>

i agree the comment of @s.kuznetsov I also not recommend declaring js events inside html tags, so i am adding a simple code without declaring js events in html tag, just use class instead of id for every image and place this JavaScript code. i agree the comment of @s.kuznetsov I also not recommend declaring js events inside html tags, so i am adding a simple code without declaring js events in html tag, just use class instead of id for every image and place this JavaScript code.

Image add class gallery图片添加 class 画廊

<img class="gallery" src="https://www.w3schools.com/howto/img_snow.jpg" alt="Snow1">

JavaScript code JavaScript代码

var modal = document.getElementById("myModal");
var span = document.getElementsByClassName("close")[0];
document.querySelectorAll('.gallery').forEach(function(gallery) {
    gallery.onclick = function(){
        var modalImg = document.getElementById("img");
        var captionText = document.getElementById("caption");
        modal.style.display = "block";
        modalImg.src = gallery.src;
        captionText.innerHTML = gallery.alt;
    }
});
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

<======= END IMPROVEMENT <=======结束改进

You are using same id in both images so this is not working, use function on click without using id check below JavaScript code.您在两个图像中使用相同的 id,所以这不起作用,请在点击时使用 function,而不使用 JavaScript 代码下方的 id 检查。

 // Get the modal var modal = document.getElementById("myModal"); function popupImage(image){ // Get the image and insert it inside the modal - use its "alt" text as a caption var modalImg = document.getElementById("img"); var captionText = document.getElementById("caption"); modal.style.display = "block"; modalImg.src = image.src; captionText.innerHTML = image.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"; }
 #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%; } }
 <h2>Image Modal</h2> <p>In this example, we use CSS to create a modal (dialog box) that is hidden by default.</p> <p>We use JavaScript to trigger the modal and to display the current image inside the modal when it is clicked on. Also note that we use the value from the image's "alt" attribute as an image caption text inside the modal.</p> <img onclick="popupImage(this)" src="https://www.w3schools.com/howto/img_snow.jpg" alt="Snow" style="width:100%;max-width:300px"> <img onclick="popupImage(this)" src="https://www.w3schools.com/howto/img_snow.jpg" alt="Snow" style="width:100%;max-width:300px"> <;-- The Modal --> <div id="myModal" class="modal"> <span class="close">&times;</span> <img class="modal-content" id="img"> <div id="caption"></div> </div>

Also give you jquery idea if you like.

 var modal = $("#myModal"); $(document).on("click", "img.gallery", function(){ $("img#img").attr('src',$(this).attr('src')); $("#caption").innerHTML = $(this).attr('alt'); modal.show(); }); $(document).on("click","span.close",function(){modal.hide();});
 #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%; } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h2>Image Modal</h2> <p>In this example, we use CSS to create a modal (dialog box) that is hidden by default.</p> <p>We use JavaScript to trigger the modal and to display the current image inside the modal when it is clicked on. Also note that we use the value from the image's "alt" attribute as an image caption text inside the modal.</p> <img class="gallery" src="https://www.w3schools.com/howto/img_snow.jpg" alt="Snow" style="width:100%;max-width:300px"> <img class="gallery" src="https://www.w3schools.com/howto/img_snow.jpg" alt="Snow" style="width:100%;max-width:300px"> <;-- The Modal --> <div id="myModal" class="modal"> <span class="close">&times;</span> <img class="modal-content" id="img"> <div id="caption"></div> </div>

Try using class because id must be unique for only one tag but class can be same in one more tag.尝试使用 class 因为id必须仅对一个标签是唯一的,但 class 可以在另一个标签中相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何自动播放w3school javascript报价幻灯片? - How to autoplay w3school javascript Quotes Slideshow? 如何使用Javascript操作图像? - How can I manipulate an image using Javascript? 你如何使用钩子将 w3school 幻灯片包含到 reactJS 组件中? - how do you include w3school slideshow into reactJS component using hooks? w3school包括HTML,JavaScript无法正常工作 - w3school include HTML, javascript not working 如何获取一张图片以更改为我使用javascript单击的图片 - How to get one image to change to the image i click on using javascript 我想使用JavaScript在点击图片(上下箭头.PNG)时增加和减少数量标签文本。 - I want to increase and decrease quantity label text on click on image (up down arrow .PNG) using JavaScript. 我想知道如何使用 JavaScript 更改图像的来源 - I was wondering how I can change the source of an image using JavaScript 我有一个字符数组,我想使用 javascript 在 html 中以 4 行的形式显示名称、房屋和图像。 我是foreach吗? - I have a array of characters, I want to display the name, house and image in rows of 4 in html using javascript. Do I foreach? 在 JavaScript 中对 JSON 数据执行聚合。 我必须得到每所学校的记录总数和学校记录的总分 - Perform an aggregation on JSON data in JavaScript. I have to get the total number of Records for each school and total Marks of School Records 我正在尝试使用jquery和javascript将用户输入限制为有效价格。 如何防止第二个小数点 - I'm trying to limt user input to a valid price using jquery & javascript. How do i prevent second decimal point
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM