0

I have been following this post to allow multiple images to use a modal to display them once clicked on. I am using bootstrap primarily to design the page so I tried to change the names of them to not mix up with the already made bootstrap modal classes. However when being clicked on still is not working.

I am also not using jQuery but if it would help just let me know.

// create references to the section
var howItWorks = document.getElementById('howitworks');
// create references to the modal...
var modal = howItWorks.getElementById('myModal');
// to all images -- note I'm using a class!
var images = howItWorks.getElementsByClassName('mImgs');
// the image in the modal
var modalImg = document.getElementById("img01");


// 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;
  }
}

var span = document.getElementsByClassName("closeit")[0];

span.onclick = function() {
  modal.style.display = "none";
}
/* Style the Image Used to Trigger the Modal */
.mImgs:hover {
box-shadow: 0 1rem 1rem rgba(0, 0, 0, 0.35);
}

/* The Modal (background) */
.js-modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  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) */
.js-modal-content {
  margin: auto;
  display: block;
  /* width: 80%;
  max-width: 700px; */
  width: auto;
  max-height: 100vh;
  max-width: 100%;
}


/* Add Animation - Zoom in the Modal */
.js-modal-content {
  animation-name: zoom;
  animation-duration: 0.6s;
}

@keyframes zoom {
  from {transform:scale(0)}
  to {transform:scale(1)}
}

/* The Close Button */
.closeit {
  position: absolute;
  float: right;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;
}

.closeit:hover,
.closeit: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%;
  }
}

/* Market Solutions all Images
____________________________________________*/
.mImgs {
height: 30%;
width: 30%;
box-shadow: 0 1rem 1rem rgba(0, 0, 0, 0.15);
transition: box-shadow 0.2s;
}
       <div id="howitworks">
          <h2>How It Works</h2>
          <p>
            Our custom designed marketplace solution provides the following customer experience:
          </p>

          <p>1) Customer searches for parts.</p>
          <img src="images/marketplace-solutions/HowItWorks_1.jpeg" class="img-fluid mImgs pt-1">

          <img src="images/marketplace-solutions/HowItWorks_2.jpeg" class="img-fluid mImgs pt-1">


          <br>
          <p class="pt-5">2) Customer select items to be included in quote.</p>
          <!-- 1 photo goes here -->
          <img src="images/marketplace-solutions/HowItWorks_3.jpeg" class="img-fluid mImgs pt-1">
          <br>

          <p class="pt-5">3) Customer selects shipping options.</p>
          <img src="images/marketplace-solutions/HowItWorks_4.jpeg" class="img-fluid mImgs pt-1">
          <br>

          <p class="pt-5">4) Customer requests a quote.</p>
          <!-- 1 photo goes here -->
          <img src="images/marketplace-solutions/HowItWorks_5.jpeg" class="img-fluid mImgs pt-1">
          <br>

          <p class="pt-5">5) Seller responds to quote with confirmed prices and shipping costs of order.</p>
          <!-- 1 photo goes here -->
          <img src="images/marketplace-solutions/HowItWorks_6.jpeg" class="img-fluid mImgs pt-1">
          <br>

          <p class="pt-5">6a) Customer pays for order.</p>
          <br>

          <p class="pt-5">6b) Seller updates order as paid.</p>
          <!-- 1 photo goes here -->
          <img src="images/marketplace-solutions/HowItWorks_7.jpeg" class="img-fluid mImgs pt-1">
          <br>

          <p class="pt-5">7) Seller updates customer with shipping details including tracking numbers.</p>
          <!-- 1 photo goes here -->
          <img src="images/marketplace-solutions/HowItWorks_8.jpeg" class="img-fluid mImgs pt-1">
          <br>


          <!-- The Modal -->
          <div id="myModal" class="js-modal">

            <!-- The Close Button -->
            <span class="closeit">&times;</span>

            <!-- Modal Content (The Image) -->
            <img class="js-modal-content" id="img01">
          </div>

        </div> <!-- END OF HOW IT WORKS -->
Cade
  • 11
  • 3
  • 2
    Just check the console error though. Why are you using `getElementByid` on `howItWorks`? Do it on `document`. – Siddharth Bhansali Apr 27 '22 at 14:43
  • ^@SiddharthBhansali got it. Those lines of codes produce errors as they're not valid. They should be: `var modal = document.getElementById('myModal');` and `var images = document.getElementsByClassName('mImgs');` – AStombaugh Apr 27 '22 at 14:45
  • In VS code it is not even outputting a console error. I have yet changed it to not use the section but to get the images by document.getElementsByClassName and i am using document. getElementById for the other ones and still I have no progress. – Cade Apr 27 '22 at 14:48
  • @AStombaugh Even when changing it to those tags I am still facing errors. – Cade Apr 27 '22 at 14:50
  • @Cade What errors are you getting? Can you copy and paste the error text so we can see what else is happening? – AStombaugh Apr 27 '22 at 14:51
  • @AStombaugh I am not receiving any errors. Id share a screenshot of my screen but yet to see any in the problems tab, output, or debug console in vs code. – Cade Apr 27 '22 at 14:54
  • @Cade When you say "not loading images" do you mean you're not seeing the images at all? Because the code itself works fine and is loading but because your example is using local paths we just see a broken image icon. See: https://jsfiddle.net/astombaugh/3vn02wjc/17/ If you're not seeing images on your end, check your file pathing and make sure it's the correct syntax: https://www.w3schools.com/html/html_filepaths.asp – AStombaugh Apr 27 '22 at 15:02

0 Answers0