简体   繁体   English

离开后如何重置我的 html 图像库以从第一张图像开始?

[英]How can I reset my html image gallery to start with first image after leaving it?

I have a website with multiple image galleries on different parts of the website.我有一个网站,在网站的不同部分有多个图片库。 When you click on the image of specific gallery it changes to next one of that gallery and so on.当您单击特定画廊的图像时,它会更改为该画廊的下一个,依此类推。 What I am trying to achieve is to reset the previous gallery to image 1 when you start clicking on a different gallery.我想要实现的是在您开始单击不同的画廊时将以前的画廊重置为图像 1。 So when the user goes back to the previous gallery, it would start from the first image.因此,当用户返回上一个画廊时,它将从第一张图像开始。

Code used for the galleries:用于画廊的代码:

let projectIndexes = {
 project1: 1,
 project2: 1,

} }

showDivs("project1", projectIndexes.project1);
showDivs("project2", projectIndexes.project2);

function plusDivs(project, n) {
  showDivs(project, projectIndexes[project] += n);

} }

function showDivs(project, index) {
  let i;
  let x = document.getElementById(project).getElementsByClassName("slidess");
  if (index > x.length) { index = 1 }
  if (index < 1) { index = x.length }
  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";
 }
  x[index - 1].style.display = "block";
  projectIndexes[project] = index;
  let elements = document.getElementById(project).querySelector('.imgslide').children;
  let imgNames = [];
  for (let i = 0; i < elements.length; i++) {
    imgNames.push(elements[i].children[0].children[0].alt);
}

} }

<div class="slide">
          <div class="slide-content">
            <div class="nextt" onclick="plusDivs('project1', 1)"></div>
          </div>
          <div class="image-container container prjct">
            <div class="projects" id="project1">
              <div class="imgslide noselect">
                <div class="content-container slidess">
                  <div class="style-3 style-3-left">
                    <img class="imageName" alt="Img" src="">
                  </div>
                  <div class="style-3 style-3-middle">
                    <img class="imageName" alt="Img" src="">
                  </div>
                  <div class="style-3 style-3-right">
                    <img class="imageName" alt="Img" src="">
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="slide">
            <div class="slide-content">
              <div class="nextt" onclick="plusDivs('project2', 1)"></div>
            </div>
            <div class="image-container container prjct">
              <div class="projects" id="project2">
                <div class="imgslide noselect">
                  <div class="content-container slidess">
                    <div class="style-3 style-3-left">
                      <img class="imageName" alt="Img" src="">
                    </div>
                    <div class="style-3 style-3-middle">
                      <img class="imageName" alt="Img" src="">
                    </div>
                    <div class="style-3 style-3-right">
                      <img class="imageName" alt="Img" src="">
                    </div>
                  </div>
                  <!-- <div class="img-name"></div> -->
                </div>
              </div>
            </div>
          </div>

I know a way to force show the first element, but it turns out it does that for all projects.我知道一种强制显示第一个元素的方法,但事实证明它适用于所有项目。 What I was not able to find is a way to recognise when clicking on a new project, that the previous and only previous project needs to reset to first image.我找不到的是一种在单击新项目时识别的方法,即前一个和唯一的前一个项目需要重置为第一个图像。 I have been struggling with this for a while now and cannot make it work, so any help would be highly appreciated.我已经为此苦苦挣扎了一段时间,无法使其发挥作用,因此我们将不胜感激任何帮助。 And if something is not clear, let me know and I will clarify things.如果有什么不清楚的地方,请告诉我,我会澄清的。

If I'm following you correctly, the following should do it.如果我正确地跟随你,下面应该做到这一点。

function resetPriorGalleries(currentProject) {
    var projectDivs = document.getElementsByClassName("projects");

    for (let i = 0; i < projectDivs.length; i++) {
        if (projectDivs[i].id === currentProject)
            return;
        showDivs(projectDivs[1].id, 1);
    }
}

The best place to call it would probably be in the onclicks.调用它的最佳位置可能是在 onclicks 中。

onclick="plusDivs('project2', 1); resetPriorGalleries('project2');"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM