为模态灯箱 (Javascript) 添加上一张/下一张幻灯片事件侦听器

我正在为一位朋友建立一个展示画作的作品集网站。所有绘画都显示在索引页面上,我创建了一个 for 循环,以便单击每幅画打开显示该画的模式:

// create references to the modal...
const modal = document.getElementById('myModal');
// to all images -- note I'm using a class!
const images = document.getElementsByClassname('myImages');
// the image in the modal
const modalImg = document.getElementById("img01");
// and the caption in the modal
const captionText = document.getElementById("caption");


// Go through all of the images with our custom class
for (let i = 0; i < images.length; i++) {
  let img = images[i];
  // and attach our click listener for this image.
  img.addEventListener("click",openmodal)
}

// Function to open modal
function openmodal() {
  modal.style.display = "block";
  modalImg.src = this.src;
  captionText.innerHTML = this.alt;
}

我已将上一个和下一个按钮附加到模态,并且一旦模态打开,我希望能够在绘画之间来回切换:

<!-- The Modal -->
  <div id="myModal" class="modal">
  <!-- The Close Button -->
  <span class="close" onclick="document.getElementById('myModal').style.display='none'">&times; 
  </span>
  <!-- Modal Content (The Image) -->
  <img class="modal-content" id="img01">
  <!-- Modal Caption (Image Text) -->
  <div id="caption"></div>

  <a class="prev">&#10094;</a>
  <a class="next">&#10095;</a>
</div>

我正在尝试将事件侦听器添加到上一个/下一个按钮,但是遇到了问题 - 我不知道如何从模态访问所选图像,因此它知道在上一个/下一个按钮时打开上一个/下一个/单击下一步按钮。

// selecting the prev and next buttons
const prev = document.querySelector(".prev")
const next = document.querySelector(".next")

prev.addEventListener("click",previousSlide);

function previousSlide() {
  // Not sure what to put here...
}

任何人都可以帮助我或至少指出我正确的方向吗?谢谢

iCMS 回答:为模态灯箱 (Javascript) 添加上一张/下一张幻灯片事件侦听器

确实能够找到您需要了解的上一张和/或下一张图片。我建议通过将图像的索引传递给 openModal 并使用 data-*“保存”它来做到这一点。

一旦完成,当点击 .prev.next 时,您只需要在索引中添加或删除 1 并调用 openModal

在下面的片段中,我试图“猜测”缺少的 HTML 和 CSS

// create references to the modal...
const modal = document.getElementById('myModal');
// to all images -- note I'm using a class!
const images = document.getElementsByClassName('myImages');
// the image in the modal
const modalImg = document.getElementById("img01");
// and the caption in the modal
const captionText = document.getElementById("caption");


// Go through all of the images with our custom class
for (let i = 0; i < images.length; i++) {
  let img = images[i];
  // and attach our click listener for this image.
  img.addEventListener("click",event => {
    openModal(i)
  });
}

// Function to open modal
function openModal(index) {
  console.log('index',index);
  modal.dataset.index = index;
  modal.classList.add("open");
  modalImg.src = images[index].src;
  captionText.innerHTML = images[index].alt;
}

document.querySelector(".prev").addEventListener('click',event => {
  const index = modal.dataset.index === '0' ? images.length - 1 : +modal.dataset.index - 1
  openModal(index);
})
document.querySelector(".next").addEventListener('click',event => {
  openModal((+modal.dataset.index + 1) % images.length);
})
#myModal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  backdrop-filter: blur(2px);
  width: 100%;
  height: 100%;
  padding: 30px;
  place-content: center;
  box-sizing: border-box;
  background-color: #e8e8e880;
}

#myModal.open {
  display: grid;
}

.prev,.next,.close {
  position: absolute;
  cursor: pointer;
}

.close {
  top: 20px;
  right: 20px;
}

.prev,.next {
  top: 50%;
  transform: translateY(-50%);
}

.prev {
  left: 20px;
}

.next {
  right: 20px;
}
<img src="https://picsum.photos/200/300?random=1" alt="" class="myImages">
<img src="https://picsum.photos/200/300?random=2" alt="" class="myImages">
<img src="https://picsum.photos/200/300?random=3" alt="" class="myImages">
<img src="https://picsum.photos/200/300?random=4" alt="" class="myImages">
<img src="https://picsum.photos/200/300?random=5" alt="" class="myImages">
<img src="https://picsum.photos/200/300?random=6" alt="" class="myImages">
<img src="https://picsum.photos/200/300?random=7" alt="" class="myImages">
<img src="https://picsum.photos/200/300?random=8" alt="" class="myImages">
<img src="https://picsum.photos/200/300?random=9" alt="" class="myImages">
<img src="https://picsum.photos/200/300?random=10" alt="" class="myImages">
<img src="https://picsum.photos/200/300?random=11" alt="" class="myImages">
<img src="https://picsum.photos/200/300?random=12" alt="" class="myImages">

<!-- The Modal -->
<div id="myModal" class="modal">
  <!-- The Close Button -->
  <span class="close" onclick="document.getElementById('myModal').classList.remove('open')">&times; 
  </span>
  <!-- Modal Content (The Image) -->
  <img class="modal-content" id="img01">
  <!-- Modal Caption (Image Text) -->
  <div id="caption"></div>

  <a class="prev">&#10094;</a>
  <a class="next">&#10095;</a>
</div>

本文链接:https://www.f2er.com/400487.html

大家都在问