Home > Mobile >  How can I make a list of images clickable (modal images) in html?
How can I make a list of images clickable (modal images) in html?

Time:12-01

I want to make a section of modal images but I don't know how to combine one modal image with another, and it doesn't seem to work when I copy-paste everything for every modal image and do it all separately either, or it's me who couldn't do it (I only just started learning html 2 days ago, sorry if this is common knowledge.)

This is what I tried, but it only works when the bold (**) codes are out, making it impossible for me to make multiple modal images in a page.

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";
}
body {font-family: century;}

#myImg {
  border-radius: 10px;
  cursor: pointer;
  transition: 0.3s;
}

#myImg:hover {opacity: 0.9;}

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

.modal-content {
  margin: auto;
  display: block;
  width: 50%;
  max-width: 400px;
  align-content: center;
}

#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%;
  }
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<ul>
<li><img id="myImg" src="images/Husmand.jpg" alt="Husmand, 1900" style="width:100%;max-width:300px"> </li>
<li> <img id="myImg" src="images/Black Death, 1900.jpg" alt="Black Death, 1900" style="width:100%;max-width:300px"> </li>
</ul>

<div id="myModal" >
  <span >&times;</span>
  <img  id="img01">
  <div id="caption"></div>
</div>




</body>
</html>

CodePudding user response:

Fixed it by using classes instead of Ids.
I gave all your images an myImg class
You should use a forEach loop for this so every item get his own onclick event.

var modal = document.getElementById("myModal");

document.querySelectorAll('.myImg').forEach(function(image) {
    var modalImg = document.getElementById("img01");
    var captionText = document.getElementById("caption");
    image.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";
    }
});
body {font-family: century;}

.myImg {
  border-radius: 10px;
  cursor: pointer;
  transition: 0.3s;
}

.myImg:hover {opacity: 0.9;}

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

.modal-content {
  margin: auto;
  display: block;
  width: 50%;
  max-width: 400px;
  align-content: center;
}

#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%;
  }
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<ul>
<li><img  src="images/Husmand.jpg" alt="Husmand, 1900" style="width:100%;max-width:300px"> </li>
<li> <img  src="images/Black Death, 1900.jpg" alt="Black Death, 1900" style="width:100%;max-width:300px"> </li>
</ul>

<div id="myModal" >
  <span >&times;</span>
  <img  id="img01">
  <div id="caption"></div>
</div>




</body>
</html>

  • Related