如何单独为所有循环图像启用“图像”视图

问题描述:

我有用于获取图像的代码,如果我点击该图像,它就会变成图像视图,问题是第一个图像,图像视图只能处理其他图像,图像视图无效我知道我的JavaScript代码有问题但是我不知道如何解决这个问题,请帮我解决这个问题并使其有效。

I have code for fetching the images and if I clicked that image it's become in a images view the issue is the first image, image view only working other images, image view not working I know I have issues with my JavaScript code but I don't how to fix that please help me how to fix this and make it work.

注意:

每个帖子都有不同的图片,像是不同的ID一样

Each Post have different images and same like different id's like

<?php echo $p_id; ?> this id will change for all images.

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

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("closeimage")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}

/*--- Images Model View Box ---*/
.newsize {
  width: 118%;
  height: 25%;
  margin-left: -45.4px;
  cursor: pointer;
}

/* The Modal (background) */
.modalimage {
  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) */
.modalimage-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 */
.modalimage-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 */
.closeimage {
  position: absolute;
  top: 75px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;
}

.closeimage:hover,
.closeimage:focus {
  color: #bbb;
  text-decoration: none;
  cursor: pointer;
}

/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px) {
  .modalimage-content {
    width: 100%;
  }
}

/*--- Images Model View Box ---*/

<?php
while ($mLibary = mysqli_fetch_assoc($getMlibary)) {
  extract($mLibary);
  ?>
  <span>
    <img src="<?php echo $u_pimg;?>" alt="" class="newsize" id="myImg">
  </span>

  <div id="myModal" class="modalimage">
    <span class="closeimage">&times;</span>
    <img class="modalimage-content" id="img01">
    <div id="caption"></div>
  </div>
<?php } ?>

这应该做的工作:

PHP(把模型放在循环之外)

This should do the work:
PHP (put the model outside the loop)

 <?php
while ($mLibary = mysqli_fetch_assoc($getMlibary)) {
  extract($mLibary);
  ?>
  <span>
    <img src="<?php echo $u_pimg;?>" alt="" class="newsize" id="myImg" onclick="showModel(event);">
  </span>


<?php } ?>

<div id="myModal" class="modalimage">
    <span class="closeimage">&times;</span>
    <img class="modalimage-content" id="img01">
    <div id="caption"></div>
  </div>


Javascript


Javascript

<script>
  function showModel(ev){
    var modal = document.getElementById('myModal');

    // Get the image and insert it inside the modal - use its "alt" text as a caption
    var img = ev.target;
    var modalImg = document.getElementById("img01");
    var captionText = document.getElementById("caption");
    img.onclick = function() {
      modal.style.display = "block";
      modalImg.src = img.src;
      captionText.innerHTML = img.alt;
    }

    // Get the <span> element that closes the modal
    var span = document.getElementsByClassName("closeimage")[0];

    // When the user clicks on <span> (x), close the modal
    span.onclick = function() {
      modal.style.display = "none";
    }
   }
 }
</script>