css 页面上的几个模态图像

nhn9ugyo  于 2023-03-05  发布在  其他
关注(0)|答案(4)|浏览(128)

我是新的JS,我正试图使用2模态图像在同一页上与此代码:
https://www.w3schools.com/howto/howto_css_modal_images.asp
有了这个HTML,JS和CSS视图大小调整只适用于一个图像,不知道如何使它在同一页上的两个图像:
我有第一个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>

大图片. 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";
}
mbyulnm0

mbyulnm01#

您所拥有的代码大部分都可以工作,但是您专门引用了一个图像。(),您不允许使用第二个图像。我对代码做了一点修改--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";
}
#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>
z9ju0rcb

z9ju0rcb2#

也不需要复制modal div,您可以使用getElementsByClassName选择器使用类名来选择两个图像,然后将click事件绑定到每个图像:

    • 超文本标记语言**
<img class="myImg" src="img_fjords.jpg" alt="Trolltunga, Norway">
<img class="myImg" src="img_mountains_wide.jpg" alt="Mountain, Norway">
    • 日本**
// 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);
}

下面是一个工作示例:
一个一个二个一个一个一个三个一个一个一个一个一个四个一个

66bbxpm5

66bbxpm53#

你必须应用类名“.modal-content”到你的第二张图片上。动画的发生是因为CSS关键帧。
或者作为替代-如果你的第二个图像有一个不同的类名或ID添加到下面的CSS代码

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

}

dwbf0jvd

dwbf0jvd4#

这是一个老问题,但我提出了自己的解决方案,使用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;行替换为maybe:

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

相关问题