页面上的几个模态图像

问题描述 投票:0回答:5

我是 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">&times;</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";
} 
javascript jquery html css bootstrap-modal
5个回答
11
投票

您拥有的大部分代码都有效,但您具体引用的是单个图像。通过使用 document.getElementById(),您不允许使用第二张图像。我已经稍微更改了您的代码——HTML 现在有一些图像用于示例目的,而 JS 现在使用 document.getElementsByClassName() 来允许多个图像。此外,我遍历图像列表,并将该事件处理程序附加到每个图像。 将其视为小提琴,或者在这里:

// 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";
}
#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%;
  }
}
<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">&times;</span>
  <img class="modal-content" id="img01">
  <div id="caption"></div>
</div>


0
投票

也不需要复制模态div,您可以使用类名通过

getElementsByClassName
选择器选择两个图像,然后为每个图像绑定一个点击事件:

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">&times;</span>
  <img class="modal-content" id="img01">
  <div id="caption"></div>
</div>


0
投票

您还必须将类名“.modal-content”应用于您的第二张图片。 动画的发生是因为 CSS 关键帧。

或者作为替代方案——如果您的第二张图片具有不同的类名或 ID,请将其添加到以下 CSS 代码中

.modal-content, #caption,<YOUR NEW IMAGE CLASS or ID> {
animation-name: zoom;
animation-duration: 0.6s;

}


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
.


0
投票

这非常有帮助!有没有办法在模式中显示不同的图像?例如,为了在网页上创建统一的外观,我希望将所有图像裁剪为相同大小,但是当单击图像时,会出现全尺寸图像。

© www.soinside.com 2019 - 2024. All rights reserved.