Reputation: 51
I am working with a slideshow with the problem that parentNode not get the slides for the NEXT/PREV buttons function if I move the controls from its location. The working code:
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');
}
}
}
}
})();
The problem comes when I change the HTML and move the "gallery-controls" out.
HTML NO WORKING
<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>
I know I must change the JS code in this line but I don't know how:
current = this.parentNode;
Upvotes: 1
Views: 89
Reputation: 1384
You should climb one level more and it should work:
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');
}
}
}
}
})();
Upvotes: 1