HTML + Javascript 模态多图

HTML + Javascript Modal multiple images

我正在处理一个包含图片库的页面。每次我按下图像,我都会 运行 模态效果并使图像全屏,右上角有一个关闭的底部。我按照这个教程https://www.w3schools.com/howto/howto_css_modal_images.asp

现在,我卡住了,因为模态效果没有 运行。我在控制台上没有收到任何错误消息。所以我有点盲目错误。我一直在做的是检查我的 classes/ids,但它们对我来说看起来还不错。任何人都可以告诉我这里缺少什么?

 <div id="imagesMain">
        <div class="row">
            <div class="column">
                <img src="/images/placeholder.png" class="galleryimage">
                <img src="/images/placeholder.png" class="galleryimage">
                <img src="/images/placeholder.png" class="galleryimage">
            </div>
            <div class="column">
                <img src="/images/placeholder2.jpg" class="galleryimage">
                <img src="/images/placeholder.png" class="galleryimage">  
            </div>
            <div class="column">
                <img src="/images/placeholder.png" class="galleryimage">
                <img src="/images/placeholder.png" class="galleryimage">
                <img src="/images/placeholder.png" class="galleryimage">
            </div>
            <div class="column">
                <img src="/images/placeholder.png" class="galleryimage">
                <img src="/images/placeholder2.jpg" class="galleryimage">
                <img src="/images/placeholder.png" class="galleryimage">
            </div>
            <div class="column">
                <img src="/images/placeholder.png" class="galleryimage">
                <img src="/images/placeholder2.jpg" class="galleryimage">
                <img src="/images/placeholder.png" class="galleryimage">
            </div>
            <div class="column">
                <img src="/images/placeholder2.jpg" class="galleryimage">
                <img src="/images/placeholder.png" class="galleryimage">
                <img src="/images/placeholder.png" class="galleryimage">
            </div>
        </div>
    </div>
    <!-- DIV placeholder for the Modal-->
    <div id="myModal" class="modal">
        <!-- CLose Button span-->
        <span class="close">&times;</span>
        <!-- Modal content "the image"-->
        <img class="modal-content" id="img01">
        <!-- Modal caption for text image-->
        <div id="caption">TestingModal</div>
    </div>

CSS:

    body {
    font-family: Helvetica;
    min-height: 100vh;
    background-color: #fafafa; 
    color: black;
    padding-bottom: 10px;
}

#container {
    max-width: 850px;
    margin: 0 auto;
    padding: 0 2rem;
    width: 100%;
}

#headContainer {
    text-align: center;
    display: inline-block; 
    width: 100%;
}

#headContainer img{
    margin: 15px;
    padding: 10px;
    width: 170px;
    border-radius: 50%;

}

#profileImage{
    float: left;
    width: 48%
} 

#menu {
    text-align: center;
    padding: 1px;
    margin: 1px;
}

#btn-menu {
    /* text-decoration: none; */
    border: none;
    background-color: transparent;
    font-display: inherit;
    padding: 10px;
    margin: 10px;
    cursor: pointer;
    /* display: inline-flex; */
    justify-content: center;
    background-color:#3e68ff;
    color: #fff;
    border-radius: 8px;
    box-shadow: 0 3px 5px rgba(0, 0, 0, 0.18);
    min-width: 15ch;
    min-height: 15px;
} 



/* control image sizes as i cant figure out way to dynamically adjust size  */
img {
    width: 105%;
    border-radius: 5px;
    cursor: pointer; 
}

.galleryimage hover{
    opacity: 0.7;
}

#imagesMain {
    display: flex;
    flex-wrap: wrap;
}
.row {
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    margin: 10px;
}

.column {
    float: left;
    flex: 25%;
    padding: 10px;
}

/* .column img {
    margin-top: 10px;
    vertical-align: middle;
} */

/* MODAL EFFECT */

.galleryimage {
    border-radius: 5px;
    cursor: pointer;
    transition: 0.3s;
}

.galleryimage:hover {opacity: 0.7;};

.modal {
    display: none;
    position: fixed;
    z-index: 1;
    padding-top: 100px;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: black;
}


.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 {
    -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)}
}

.close {
    position: absolute;
    top: 15px;
    right: 35px;
    color: #f1f1f1;
    font-size: 40px;
    font-weight: bold;
    transition: 0.3s;
}

.close:hover,
.close:focus {
    color: grey;
    text-decoration: none;
    cursor: pointer;
}

@media only screen and (max-width: 700px){
    .modal-content {
        width: 100%;
    }
} 

js:

let modal = document.getElementById('myModal');
let img = document.getElementsByClassName('galleryimage');
let modalImg = document.getElementById('img01');
let captionText = document.getElementById('caption');
let span = document.getElementsByClassName('close')[0];


img.onClick = function () {
    modal.style.display = "block";
    modalImg.src = this.src;
    captionText.innerHTML = this.alt;
}

span.onClick = function () {
    modal.style.display = "none";
}

document.getElementByClassName returns 包含给定 class.. 的所有元素的 Html 集合 即使 Dom 中只有一个元素,您也需要像数组一样处理它(即 img[0]),您会发现您的脚本处理包含给定 [=19= 的第一个元素] :)

尝试:

 img.forEach( e => e.onClick = function () {
     modal.style.display = "block";
     modalImg.src = this.src;
     captionText.innerHTML = this.alt;
 }) 

我相信你会得到不同的结果..

getElementsByClassName() returns 类似数组的元素对象。您需要遍历每个元素以添加事件处理程序。

此外,您可能是指 onclick,而不是 onClick

这是一个例子:

let modal = document.getElementById('myModal');
let img = document.getElementsByClassName('galleryimage');
let modalImg = document.getElementById('img01');
let captionText = document.getElementById('caption');
let span = document.getElementsByClassName('close')[0];

let clickHandler = function(e) {
  modal.style.display = "block";
  modalImg.src = e.srcElement.src;
  captionText.innerHTML = e.srcElement.alt;
}

for (var i = 0; i < img.length; i++) {
  img[i].addEventListener("click", clickHandler, false);
}

span.addEventListener("click", function() {
  modal.style.display = "none";
});
body {
  font-family: Helvetica;
  min-height: 100vh;
  background-color: #fafafa;
  color: black;
  padding-bottom: 10px;
}

#container {
  max-width: 850px;
  margin: 0 auto;
  padding: 0 2rem;
  width: 100%;
}

#headContainer {
  text-align: center;
  display: inline-block;
  width: 100%;
}

#headContainer img {
  margin: 15px;
  padding: 10px;
  width: 170px;
  border-radius: 50%;
}

#profileImage {
  float: left;
  width: 48%
}

#menu {
  text-align: center;
  padding: 1px;
  margin: 1px;
}

#btn-menu {
  /* text-decoration: none; */
  border: none;
  background-color: transparent;
  font-display: inherit;
  padding: 10px;
  margin: 10px;
  cursor: pointer;
  /* display: inline-flex; */
  justify-content: center;
  background-color: #3e68ff;
  color: #fff;
  border-radius: 8px;
  box-shadow: 0 3px 5px rgba(0, 0, 0, 0.18);
  min-width: 15ch;
  min-height: 15px;
}


/* control image sizes as i cant figure out way to dynamically adjust size  */

img {
  width: 105%;
  border-radius: 5px;
  cursor: pointer;
}

.galleryimage hover {
  opacity: 0.7;
}

#imagesMain {
  display: flex;
  flex-wrap: wrap;
}

.row {
  display: flex;
  flex-wrap: wrap;
  padding: 10px;
  margin: 10px;
}

.column {
  float: left;
  flex: 25%;
  padding: 10px;
}


/* .column img {
    margin-top: 10px;
    vertical-align: middle;
} */


/* MODAL EFFECT */

.galleryimage {
  border-radius: 5px;
  cursor: pointer;
  transition: 0.3s;
}

.galleryimage:hover {
  opacity: 0.7;
}

;
.modal {
  display: none;
  position: fixed;
  z-index: 1;
  padding-top: 100px;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: black;
}

.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 {
  -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)
  }
}

.close {
  position: absolute;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;
}

.close:hover,
.close:focus {
  color: grey;
  text-decoration: none;
  cursor: pointer;
}

@media only screen and (max-width: 700px) {
  .modal-content {
    width: 100%;
  }
}
<div id="imagesMain">
  <div class="row">
    <div class="column">
      <img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" class="galleryimage">
      <img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" class="galleryimage">
      <img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" class="galleryimage">
    </div>
    <div class="column">
      <img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" class="galleryimage">
      <img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" class="galleryimage">
    </div>
    <div class="column">
      <img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" class="galleryimage">
      <img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" class="galleryimage">
      <img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" class="galleryimage">
    </div>
    <div class="column">
      <img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" class="galleryimage">
      <img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" class="galleryimage">
      <img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" class="galleryimage">
    </div>
    <div class="column">
      <img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" class="galleryimage">
      <img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" class="galleryimage">
      <img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" class="galleryimage">
    </div>
    <div class="column">
      <img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" class="galleryimage">
      <img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" class="galleryimage">
      <img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" class="galleryimage">
    </div>
  </div>
</div>
<!-- DIV placeholder for the Modal-->
<div id="myModal" class="modal">
  <!-- CLose Button span-->
  <span class="close">&times;</span>
  <!-- Modal content "the image"-->
  <img class="modal-content" id="img01">
  <!-- Modal caption for text image-->
  <div id="caption">TestingModal</div>
</div>

首先,您不能将事件侦听器设置为元素集合,在这种情况下为 document.getElementsByClass。为此,您可以按以下方式遍历它们。其次,您在 onClick 方法上有类型错误。应该是onclick

let modal = document.getElementById('myModal');
let images = document.getElementsByClassName('galleryimage');
let modalImg = document.getElementById('img01');
let captionText = document.getElementById('caption');
let span = document.getElementsByClassName('close')[0];
Array.from(images).forEach(function (img) {
    img.onclick = function () {
    modal.style.display = "block";
    modalImg.src = this.src;
    captionText.innerHTML = this.alt;
  }
})

span.onclick = function () {
    modal.style.display = "none";
}
body {
    font-family: Helvetica;
    min-height: 100vh;
    background-color: #fafafa; 
    color: black;
    padding-bottom: 10px;
}

#container {
    max-width: 850px;
    margin: 0 auto;
    padding: 0 2rem;
    width: 100%;
}

#headContainer {
    text-align: center;
    display: inline-block; 
    width: 100%;
}

#headContainer img{
    margin: 15px;
    padding: 10px;
    width: 170px;
    border-radius: 50%;

}

#profileImage{
    float: left;
    width: 48%
} 

#menu {
    text-align: center;
    padding: 1px;
    margin: 1px;
}

#btn-menu {
    /* text-decoration: none; */
    border: none;
    background-color: transparent;
    font-display: inherit;
    padding: 10px;
    margin: 10px;
    cursor: pointer;
    /* display: inline-flex; */
    justify-content: center;
    background-color:#3e68ff;
    color: #fff;
    border-radius: 8px;
    box-shadow: 0 3px 5px rgba(0, 0, 0, 0.18);
    min-width: 15ch;
    min-height: 15px;
} 



/* control image sizes as i cant figure out way to dynamically adjust size  */
img {
    width: 105%;
    border-radius: 5px;
    cursor: pointer; 
}

.galleryimage hover{
    opacity: 0.7;
}

#imagesMain {
    display: flex;
    flex-wrap: wrap;
}
.row {
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    margin: 10px;
}

.column {
    float: left;
    flex: 25%;
    padding: 10px;
}

/* .column img {
    margin-top: 10px;
    vertical-align: middle;
} */

/* MODAL EFFECT */

.galleryimage {
    border-radius: 5px;
    cursor: pointer;
    transition: 0.3s;
}

.galleryimage:hover {opacity: 0.7;};

.modal {
    display: none;
    position: fixed;
    z-index: 1;
    padding-top: 100px;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: black;
}


.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 {
    -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)}
}

.close {
    position: absolute;
    top: 15px;
    right: 35px;
    color: #f1f1f1;
    font-size: 40px;
    font-weight: bold;
    transition: 0.3s;
}

.close:hover,
.close:focus {
    color: grey;
    text-decoration: none;
    cursor: pointer;
}

@media only screen and (max-width: 700px){
    .modal-content {
        width: 100%;
    }
} 
 <div id="imagesMain">
        <div class="row">
            <div class="column">
                <img src="https://via.placeholder.com/140x100" class="galleryimage">
                <img src="https://via.placeholder.com/140x100" class="galleryimage">
                <img src="https://via.placeholder.com/140x100" class="galleryimage">
            </div>
            <div class="column">
                <img src="https://via.placeholder.com/140x100" class="galleryimage">
                <img src="https://via.placeholder.com/140x100" class="galleryimage">  
            </div>
            <div class="column">
                <img src="https://via.placeholder.com/140x100" class="galleryimage">
                <img src="https://via.placeholder.com/140x100" class="galleryimage">
                <img src="https://via.placeholder.com/140x100" class="galleryimage">
            </div>
            <div class="column">
                <img src="https://via.placeholder.com/140x100" class="galleryimage">
                <img src="https://via.placeholder.com/140x100" class="galleryimage">
                <img src="https://via.placeholder.com/140x100" class="galleryimage">
            </div>
            <div class="column">
                <img src="https://via.placeholder.com/140x100" class="galleryimage">
                <img src="https://via.placeholder.com/140x100" class="galleryimage">
                <img src="https://via.placeholder.com/140x100" class="galleryimage">
            </div>
            <div class="column">
                <img src="https://via.placeholder.com/140x100" class="galleryimage">
                <img src="https://via.placeholder.com/140x100" class="galleryimage">
                <img src="https://via.placeholder.com/140x100" class="galleryimage">
            </div>
        </div>
    </div>
    <!-- DIV placeholder for the Modal-->
    <div id="myModal" class="modal">
        <!-- CLose Button span-->
        <span class="close">&times;</span>
        <!-- Modal content "the image"-->
        <img class="modal-content" id="img01">
        <!-- Modal caption for text image-->
        <div id="caption">TestingModal</div>
    </div>