爬上parentNode

Climb up parentNode

我正在处理幻灯片,但如果我将控件从其位置移开,parentNode 无法获得 NEXT/PREV 按钮功能的幻灯片。 工作代码:

HTML

<section class="allgalleries-latest t8">    
<div class="gallery" id="gallery-001">
        <div class="gallery-frame">
            <div class="slides">
                <img class="gallery-picture" src="https://images.unsplash.com/photo-1465935343323-d742334bcbda?crop=entropy&fit=crop&fm=jpg&h=975&ixjsv=2.1.0&ixlib=rb-0.3.5&q=80&w=1925">
                <p class="gallery-caption">Slide 1</p>
            </div>
            <div class="slides">
                <img class="gallery-picture" src="https://images.unsplash.com/photo-1443890923422-7819ed4101c0?crop=entropy&fit=crop&fm=jpg&h=975&ixjsv=2.1.0&ixlib=rb-0.3.5&q=80&w=1925">
                <p class="gallery-caption">Slide 2</p>
            </div>
            <div class="slides">
                <img class="gallery-picture" src="https://images.unsplash.com/photo-1474861644511-0f2775ae97cc?crop=entropy&fit=crop&fm=jpg&h=975&ixjsv=2.1.0&ixlib=rb-0.3.5&q=80&w=1925">
                <p class="gallery-caption">Slide 3</p>
            </div>
            <div class="gallery-controls-before"></div>
            <div class="gallery-controls-next"></div>
        </div>

    </div>
</section>

CSS

.gallery {
    width: 100vw;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
}

.gallery-frame {
    width: 90%;
    height: 80%;
    margin: auto;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

.slides {
      display:none;
}
.active-slide {
      display:block;
}

.gallery-picture {
    max-width: 100%;
    max-height: 100%;
    margin: auto;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

.gallery-caption {
    width: 100%;
    height: 1%;
    position: fixed;
    bottom: 0;
    left: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    color: black;
}


.gallery-controls-before {
    position: fixed;
    top: 10%;
    left: 0%;
    width: 50%;
    height: 80%;
    cursor:w-resize;
}

.gallery-controls-next {
    position: fixed;
    top: 10%;
    left: 50%;
    width: 50%;
    height: 80%;
    cursor:e-resize;

}

JS

(function() {   
  init(); //on page load - show first slide, hide the rest
  function init() {
    parents = document.getElementsByClassName('gallery-frame');
    for (j = 0; j < parents.length; j++) {
      var slides = parents[j].getElementsByClassName("slides");
      slides[0].classList.add('active-slide');
    }
  }
//prev/next functionality
  links = document.querySelectorAll('.gallery-controls-before, .gallery-controls-next');
  for (i = 0; i < links.length; i++) {
    links[i].onclick = function() {
      current = this.parentNode;
      var slides = current.getElementsByClassName("slides");
      curr_slide = current.getElementsByClassName('active-slide')[0];
      curr_slide.classList.remove('active-slide');
      if (this.className == 'gallery-controls-next') {

        if (curr_slide.nextElementSibling.classList.contains('slides')) {
          curr_slide.nextElementSibling.classList.add('active-slide');
        } else {
          slides[0].classList.add('active-slide');
        }
      }
      if (this.className == 'gallery-controls-before') {
        if (curr_slide.previousElementSibling) {
          curr_slide.previousElementSibling.classList.add('active-slide');
        } else {
          slides[slides.length - 1].classList.add('active-slide');
        }
      }
    }
  }
})();

WORKING CODEPEN

当我更改 HTML 并将 "gallery-controls" 移出时出现问题。

HTML 没有工作

<div class="gallery" id="gallery-001">
        <div class="gallery-frame">
            <div class="slides">
                <img class="gallery-picture" src="https://images.unsplash.com/photo-1465935343323-d742334bcbda?crop=entropy&fit=crop&fm=jpg&h=975&ixjsv=2.1.0&ixlib=rb-0.3.5&q=80&w=1925">
                <p class="gallery-caption">Slide 1</p>
            </div>
            <div class="slides">
                <img class="gallery-picture" src="https://images.unsplash.com/photo-1443890923422-7819ed4101c0?crop=entropy&fit=crop&fm=jpg&h=975&ixjsv=2.1.0&ixlib=rb-0.3.5&q=80&w=1925">
                <p class="gallery-caption">Slide 2</p>
            </div>
            <div class="slides">
                <img class="gallery-picture" src="https://images.unsplash.com/photo-1474861644511-0f2775ae97cc?crop=entropy&fit=crop&fm=jpg&h=975&ixjsv=2.1.0&ixlib=rb-0.3.5&q=80&w=1925">
                <p class="gallery-caption">Slide 3</p>
            </div>

        </div>
        <div class="gallery-controls">
            <div class="gallery-controls-before"></div>
            <div class="gallery-controls-next"></div>
        </div>

</div>

NO WORKING CODEPEN

我知道我必须更改此行中的 JS 代码,但我不知道如何更改:

          current = this.parentNode;

你应该再爬一级,它应该会起作用:

JS:

(function() {   
  init(); //on page load - show first slide, hide the rest
  function init() {
    parents = document.getElementsByClassName('gallery-frame');
    for (j = 0; j < parents.length; j++) {
      var slides = parents[j].getElementsByClassName("slides");
      slides[0].classList.add('active-slide');
    }
  }
//prev/next functionality
  links = document.querySelectorAll('.gallery-controls-before, .gallery-controls-next');
  for (i = 0; i < links.length; i++) {
    links[i].onclick = function() {
      current = this.parentNode.parentNode;
      var slides = current.getElementsByClassName("slides");
      curr_slide = current.getElementsByClassName('active-slide')[0];
      curr_slide.classList.remove('active-slide');
      if (this.className == 'gallery-controls-next') {

        if (curr_slide.nextElementSibling) {
          curr_slide.nextElementSibling.classList.add('active-slide');
        } else {
          slides[0].classList.add('active-slide');
        }
      }
      if (this.className == 'gallery-controls-before') {
        if (curr_slide.previousElementSibling) {
          curr_slide.previousElementSibling.classList.add('active-slide');
        } else {
          slides[slides.length - 1].classList.add('active-slide');
        }
      }
    }
  }
})();